graphlcd-base/tools/convpic/tiff.c
changeset 4 df6a40031aa5
equal deleted inserted replaced
3:d0e62fc47285 4:df6a40031aa5
       
     1 /**
       
     2  *  GraphLCD plugin for the Video Disk Recorder
       
     3  *
       
     4  *  tiff.c  -  tiff logo class
       
     5  *
       
     6  *  (c) 2004 Andreas Brachold <vdr04 AT deltab de>
       
     7  *  (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online de>
       
     8  **/
       
     9 
       
    10 /***************************************************************************
       
    11  *                                                                         *
       
    12  *   This program is free software; you can redistribute it and/or modify  *
       
    13  *   it under the terms of the GNU General Public License as published by  *
       
    14  *   the Free Software Foundation; either version 2 of the License, or     *
       
    15  *   (at your option) any later version.                                   *
       
    16  *                                                                         *
       
    17  *   This program is distributed in the hope that it will be useful,       *
       
    18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
       
    19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
       
    20  *   GNU General Public License for more details.                          *
       
    21  *                                                                         *
       
    22  *   You should have received a copy of the GNU General Public License     *
       
    23  *   along with this program;                                              *
       
    24  *   if not, write to the Free Software Foundation, Inc.,                  *
       
    25  *   59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *
       
    26  *                                                                         *
       
    27  ***************************************************************************/
       
    28 
       
    29 #include <stdio.h>
       
    30 #include <string.h>
       
    31 
       
    32 #include <string>
       
    33 
       
    34 #include <glcdgraphics/bitmap.h>
       
    35 #include <glcdgraphics/image.h>
       
    36 
       
    37 #include "tiff.h"
       
    38 
       
    39 
       
    40 #pragma pack(1)
       
    41 typedef struct TIFFT{
       
    42     unsigned short  tag;
       
    43     unsigned short  type;
       
    44     unsigned long   length;
       
    45     /*     1 = BYTE.      8-bit unsigned integer.                                                                  */
       
    46     /*     2 = ASCII.     8-bit bytes that store ASCII codes; the last byte must be null.                          */
       
    47     /*     3 = SHORT.     A 16-bit (2-byte) unsigned integer.                                                      */
       
    48     /*     4 = LONG.      A 32-bit (4-byte) unsigned integer.                                                      */
       
    49     /*     5 = RATIONAL.  Two LONGs: the first represents the numerator of a fraction, the second the denominator. */
       
    50     unsigned long   off_val;
       
    51 } TIFFTAG;
       
    52 #pragma pack()
       
    53 
       
    54 #define GETANDCHECK { t=fgetc(fIN);if(t==EOF) {fclose(fIN);return false;};}
       
    55 
       
    56 cTIFFFile::cTIFFFile()
       
    57 {
       
    58 }
       
    59 
       
    60 cTIFFFile::~cTIFFFile()
       
    61 {
       
    62 }
       
    63 
       
    64 bool cTIFFFile::Load(GLCD::cImage & image, const std::string & fileName)
       
    65 {
       
    66     FILE         *fIN;
       
    67     TIFFTAG       tifftag;
       
    68     unsigned int  tiff_header, tiff_anztags, tiff_data;
       
    69     unsigned char cl,ch,y,i;
       
    70     unsigned char height, width, strip, invert;
       
    71     unsigned char fLittleEndian=0;
       
    72     int j;
       
    73     int t;
       
    74     unsigned char *bitmap = NULL;
       
    75     bool  bInvert = false;
       
    76 
       
    77     if (fileName.length() > 0)
       
    78     {
       
    79         fIN = fopen(fileName.c_str(), "rb");
       
    80         if (fIN)
       
    81         {
       
    82             //    isyslog("graphlcd plugin: try to load logo %s.", szFileName);
       
    83             if (fseek(fIN, 0, SEEK_SET)==EOF)
       
    84             {
       
    85                 fclose(fIN);
       
    86                 return false;
       
    87             }
       
    88             GETANDCHECK; cl=(unsigned char)t;
       
    89             GETANDCHECK; ch=(unsigned char)t;
       
    90             if ((cl==0x49) && (ch==0x49))
       
    91             {
       
    92                 fLittleEndian=1;
       
    93             }
       
    94 
       
    95             if (fseek(fIN, 4, SEEK_SET)==EOF)
       
    96             {
       
    97                 fclose(fIN);
       
    98                 return false;
       
    99             }
       
   100             GETANDCHECK; cl=(unsigned char)t;
       
   101             GETANDCHECK; ch=(unsigned char)t;
       
   102             tiff_header = cl+256*ch;
       
   103             //printf("tiff_header:%d %x\n", tiff_header, tiff_header);
       
   104 
       
   105             if (fseek(fIN, tiff_header, SEEK_SET)==EOF)
       
   106             {
       
   107                 fclose(fIN);
       
   108                 return false;
       
   109             }
       
   110 
       
   111             GETANDCHECK; cl=(unsigned char)t;
       
   112             GETANDCHECK; ch=(unsigned char)t;
       
   113             tiff_anztags = cl+256*ch;
       
   114             //printf("tiff_anztags:%d %x\n", tiff_anztags, tiff_anztags);
       
   115 
       
   116             height=0;
       
   117             width=0;
       
   118             strip=0;
       
   119             invert=0;
       
   120             for (i=0; (i<tiff_anztags)&&(!height||!width||!strip||!invert); i++)
       
   121             {
       
   122                 if (fread(&tifftag, sizeof(tifftag), 1, fIN)!=1)
       
   123                 {
       
   124                     fclose(fIN);
       
   125                     return false;
       
   126                 }
       
   127                 if (tifftag.tag==0x0100) width=tifftag.off_val;
       
   128                 if (tifftag.tag==0x0101) height=tifftag.off_val;
       
   129                 if (tifftag.tag==0x0111) strip=tifftag.off_val;
       
   130                 if (tifftag.tag==0x0106) invert=tifftag.off_val+1;
       
   131                 //printf("tag%d: %d %d %ld %ld\n", i,tifftag.tag, tifftag.type, tifftag.length, tifftag.off_val );
       
   132             }
       
   133 
       
   134             if (fseek(fIN,strip, SEEK_SET)==EOF)
       
   135             {
       
   136                 fclose(fIN);
       
   137                 return false;
       
   138             }
       
   139             GETANDCHECK; cl=(unsigned char)t;
       
   140             GETANDCHECK; ch=(unsigned char)t;
       
   141             tiff_data = cl+256*ch;
       
   142             //printf("tiff_data:%d %x\n", tiff_data, tiff_data);
       
   143 
       
   144             if (fseek(fIN, tiff_data, SEEK_SET)==EOF)
       
   145             {
       
   146                 fclose(fIN);
       
   147                 return false;
       
   148             }
       
   149 
       
   150 
       
   151             image.Clear();
       
   152             image.SetWidth(width);
       
   153             image.SetHeight(height);
       
   154             image.SetDelay(100);
       
   155             bitmap = new unsigned char[height * ((width + 7) / 8)];
       
   156             if (bitmap)
       
   157             {
       
   158                 if (fread(bitmap, height*((width+7)/8), 1, fIN)!=1)
       
   159                 {
       
   160                     delete [] bitmap;
       
   161                     fclose(fIN);
       
   162                     image.Clear();
       
   163                     return false;
       
   164                 }
       
   165 
       
   166                 if (invert-1==1) bInvert = !bInvert; // 'Black is zero'
       
   167                 if (bInvert)
       
   168                 {
       
   169                     for (j=0; j < height * ((width+7)/8); j++)
       
   170                     {
       
   171                         (*(bitmap+j)) = (*(bitmap+j))^0xff;
       
   172                     }
       
   173                 }
       
   174 
       
   175                 // cut the rest of the line
       
   176                 if (width%8)
       
   177                 {
       
   178                     for (y=1; y<=height; y++) {
       
   179                         j=y*((width+7)/8)-1;
       
   180                         (*(bitmap+j)) = ((*(bitmap+j))>>(8-width%8))<<(8-width%8);
       
   181                     }
       
   182                 }
       
   183                 image.AddBitmap(new GLCD::cBitmap(width, height, bitmap));
       
   184             }
       
   185             else
       
   186             {
       
   187                 fprintf(stderr, "ERROR: cannot allocate memory\n");
       
   188             }
       
   189             fclose(fIN);
       
   190         }
       
   191         else
       
   192         {
       
   193             fprintf(stderr, "ERROR: cannot open picture %s\n", fileName.c_str());
       
   194         }
       
   195     }
       
   196     else
       
   197     {
       
   198         fprintf(stderr, "ERROR: no szFileName given!\n");
       
   199     }
       
   200     return true;
       
   201 }