graphlcd-base/tools/convpic/tiff.c
changeset 4 df6a40031aa5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graphlcd-base/tools/convpic/tiff.c	Wed Feb 06 17:32:55 2008 +0000
     1.3 @@ -0,0 +1,201 @@
     1.4 +/**
     1.5 + *  GraphLCD plugin for the Video Disk Recorder
     1.6 + *
     1.7 + *  tiff.c  -  tiff logo class
     1.8 + *
     1.9 + *  (c) 2004 Andreas Brachold <vdr04 AT deltab de>
    1.10 + *  (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online de>
    1.11 + **/
    1.12 +
    1.13 +/***************************************************************************
    1.14 + *                                                                         *
    1.15 + *   This program is free software; you can redistribute it and/or modify  *
    1.16 + *   it under the terms of the GNU General Public License as published by  *
    1.17 + *   the Free Software Foundation; either version 2 of the License, or     *
    1.18 + *   (at your option) any later version.                                   *
    1.19 + *                                                                         *
    1.20 + *   This program is distributed in the hope that it will be useful,       *
    1.21 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    1.22 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    1.23 + *   GNU General Public License for more details.                          *
    1.24 + *                                                                         *
    1.25 + *   You should have received a copy of the GNU General Public License     *
    1.26 + *   along with this program;                                              *
    1.27 + *   if not, write to the Free Software Foundation, Inc.,                  *
    1.28 + *   59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *
    1.29 + *                                                                         *
    1.30 + ***************************************************************************/
    1.31 +
    1.32 +#include <stdio.h>
    1.33 +#include <string.h>
    1.34 +
    1.35 +#include <string>
    1.36 +
    1.37 +#include <glcdgraphics/bitmap.h>
    1.38 +#include <glcdgraphics/image.h>
    1.39 +
    1.40 +#include "tiff.h"
    1.41 +
    1.42 +
    1.43 +#pragma pack(1)
    1.44 +typedef struct TIFFT{
    1.45 +    unsigned short  tag;
    1.46 +    unsigned short  type;
    1.47 +    unsigned long   length;
    1.48 +    /*     1 = BYTE.      8-bit unsigned integer.                                                                  */
    1.49 +    /*     2 = ASCII.     8-bit bytes that store ASCII codes; the last byte must be null.                          */
    1.50 +    /*     3 = SHORT.     A 16-bit (2-byte) unsigned integer.                                                      */
    1.51 +    /*     4 = LONG.      A 32-bit (4-byte) unsigned integer.                                                      */
    1.52 +    /*     5 = RATIONAL.  Two LONGs: the first represents the numerator of a fraction, the second the denominator. */
    1.53 +    unsigned long   off_val;
    1.54 +} TIFFTAG;
    1.55 +#pragma pack()
    1.56 +
    1.57 +#define GETANDCHECK { t=fgetc(fIN);if(t==EOF) {fclose(fIN);return false;};}
    1.58 +
    1.59 +cTIFFFile::cTIFFFile()
    1.60 +{
    1.61 +}
    1.62 +
    1.63 +cTIFFFile::~cTIFFFile()
    1.64 +{
    1.65 +}
    1.66 +
    1.67 +bool cTIFFFile::Load(GLCD::cImage & image, const std::string & fileName)
    1.68 +{
    1.69 +    FILE         *fIN;
    1.70 +    TIFFTAG       tifftag;
    1.71 +    unsigned int  tiff_header, tiff_anztags, tiff_data;
    1.72 +    unsigned char cl,ch,y,i;
    1.73 +    unsigned char height, width, strip, invert;
    1.74 +    unsigned char fLittleEndian=0;
    1.75 +    int j;
    1.76 +    int t;
    1.77 +    unsigned char *bitmap = NULL;
    1.78 +    bool  bInvert = false;
    1.79 +
    1.80 +    if (fileName.length() > 0)
    1.81 +    {
    1.82 +        fIN = fopen(fileName.c_str(), "rb");
    1.83 +        if (fIN)
    1.84 +        {
    1.85 +            //    isyslog("graphlcd plugin: try to load logo %s.", szFileName);
    1.86 +            if (fseek(fIN, 0, SEEK_SET)==EOF)
    1.87 +            {
    1.88 +                fclose(fIN);
    1.89 +                return false;
    1.90 +            }
    1.91 +            GETANDCHECK; cl=(unsigned char)t;
    1.92 +            GETANDCHECK; ch=(unsigned char)t;
    1.93 +            if ((cl==0x49) && (ch==0x49))
    1.94 +            {
    1.95 +                fLittleEndian=1;
    1.96 +            }
    1.97 +
    1.98 +            if (fseek(fIN, 4, SEEK_SET)==EOF)
    1.99 +            {
   1.100 +                fclose(fIN);
   1.101 +                return false;
   1.102 +            }
   1.103 +            GETANDCHECK; cl=(unsigned char)t;
   1.104 +            GETANDCHECK; ch=(unsigned char)t;
   1.105 +            tiff_header = cl+256*ch;
   1.106 +            //printf("tiff_header:%d %x\n", tiff_header, tiff_header);
   1.107 +
   1.108 +            if (fseek(fIN, tiff_header, SEEK_SET)==EOF)
   1.109 +            {
   1.110 +                fclose(fIN);
   1.111 +                return false;
   1.112 +            }
   1.113 +
   1.114 +            GETANDCHECK; cl=(unsigned char)t;
   1.115 +            GETANDCHECK; ch=(unsigned char)t;
   1.116 +            tiff_anztags = cl+256*ch;
   1.117 +            //printf("tiff_anztags:%d %x\n", tiff_anztags, tiff_anztags);
   1.118 +
   1.119 +            height=0;
   1.120 +            width=0;
   1.121 +            strip=0;
   1.122 +            invert=0;
   1.123 +            for (i=0; (i<tiff_anztags)&&(!height||!width||!strip||!invert); i++)
   1.124 +            {
   1.125 +                if (fread(&tifftag, sizeof(tifftag), 1, fIN)!=1)
   1.126 +                {
   1.127 +                    fclose(fIN);
   1.128 +                    return false;
   1.129 +                }
   1.130 +                if (tifftag.tag==0x0100) width=tifftag.off_val;
   1.131 +                if (tifftag.tag==0x0101) height=tifftag.off_val;
   1.132 +                if (tifftag.tag==0x0111) strip=tifftag.off_val;
   1.133 +                if (tifftag.tag==0x0106) invert=tifftag.off_val+1;
   1.134 +                //printf("tag%d: %d %d %ld %ld\n", i,tifftag.tag, tifftag.type, tifftag.length, tifftag.off_val );
   1.135 +            }
   1.136 +
   1.137 +            if (fseek(fIN,strip, SEEK_SET)==EOF)
   1.138 +            {
   1.139 +                fclose(fIN);
   1.140 +                return false;
   1.141 +            }
   1.142 +            GETANDCHECK; cl=(unsigned char)t;
   1.143 +            GETANDCHECK; ch=(unsigned char)t;
   1.144 +            tiff_data = cl+256*ch;
   1.145 +            //printf("tiff_data:%d %x\n", tiff_data, tiff_data);
   1.146 +
   1.147 +            if (fseek(fIN, tiff_data, SEEK_SET)==EOF)
   1.148 +            {
   1.149 +                fclose(fIN);
   1.150 +                return false;
   1.151 +            }
   1.152 +
   1.153 +
   1.154 +            image.Clear();
   1.155 +            image.SetWidth(width);
   1.156 +            image.SetHeight(height);
   1.157 +            image.SetDelay(100);
   1.158 +            bitmap = new unsigned char[height * ((width + 7) / 8)];
   1.159 +            if (bitmap)
   1.160 +            {
   1.161 +                if (fread(bitmap, height*((width+7)/8), 1, fIN)!=1)
   1.162 +                {
   1.163 +                    delete [] bitmap;
   1.164 +                    fclose(fIN);
   1.165 +                    image.Clear();
   1.166 +                    return false;
   1.167 +                }
   1.168 +
   1.169 +                if (invert-1==1) bInvert = !bInvert; // 'Black is zero'
   1.170 +                if (bInvert)
   1.171 +                {
   1.172 +                    for (j=0; j < height * ((width+7)/8); j++)
   1.173 +                    {
   1.174 +                        (*(bitmap+j)) = (*(bitmap+j))^0xff;
   1.175 +                    }
   1.176 +                }
   1.177 +
   1.178 +                // cut the rest of the line
   1.179 +                if (width%8)
   1.180 +                {
   1.181 +                    for (y=1; y<=height; y++) {
   1.182 +                        j=y*((width+7)/8)-1;
   1.183 +                        (*(bitmap+j)) = ((*(bitmap+j))>>(8-width%8))<<(8-width%8);
   1.184 +                    }
   1.185 +                }
   1.186 +                image.AddBitmap(new GLCD::cBitmap(width, height, bitmap));
   1.187 +            }
   1.188 +            else
   1.189 +            {
   1.190 +                fprintf(stderr, "ERROR: cannot allocate memory\n");
   1.191 +            }
   1.192 +            fclose(fIN);
   1.193 +        }
   1.194 +        else
   1.195 +        {
   1.196 +            fprintf(stderr, "ERROR: cannot open picture %s\n", fileName.c_str());
   1.197 +        }
   1.198 +    }
   1.199 +    else
   1.200 +    {
   1.201 +        fprintf(stderr, "ERROR: no szFileName given!\n");
   1.202 +    }
   1.203 +    return true;
   1.204 +}