| 1 | /* Copyright (C) 2000-2012 by George Williams */ |
| 2 | /* 2013feb15, added file and mem error checks, Jose Da Silva */ |
| 3 | /* |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | |
| 14 | * The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <fontforge-config.h> |
| 30 | |
| 31 | #ifdef _NO_LIBTIFF |
| 32 | |
| 33 | static int a_file_must_define_something=0; /* ANSI says so */ |
| 34 | |
| 35 | #else |
| 36 | |
| 37 | #include <tiffio.h> |
| 38 | |
| 39 | #include "gimage.h" |
| 40 | |
| 41 | GImage *GImageReadTiff(char *filename) { |
| 42 | /* Import a TIF image, else return NULL if error */ |
| 43 | TIFF* tif; |
| 44 | uint32_t w,h,i,j; |
| 45 | uint32_t *ipt,*fpt,*raster=NULL; |
| 46 | GImage *ret=NULL; |
| 47 | struct _GImage *base; |
| 48 | |
| 49 | if ( (tif=TIFFOpen(filename,"rb"))==NULL ) { |
| 50 | /* if error, then report "built-in" error message and then exit */ |
| 51 | return( NULL ); |
| 52 | } |
| 53 | |
| 54 | /* Get width and height of TIF image, exit if error */ |
| 55 | if ( TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&w)!=1 || \ |
| 56 | TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&h)!=1 ) |
| 57 | goto errorGImageReadTiff; |
| 58 | |
| 59 | /* Create memory to hold image & raster, exit if not enough memory */ |
| 60 | if ( (ret=GImageCreate(it_true,w,h))==NULL ) |
| 61 | goto errorGImageReadTiffMem; |
| 62 | if ( (raster=(uint32_t*) malloc(w*h*sizeof(uint32_t)))==NULL ) { |
| 63 | NoMoreMemMessage(); |
| 64 | goto errorGImageReadTiffMem; |
| 65 | } |
| 66 | |
| 67 | /* Read TIF image and process it into an internal FF usable format */ |
| 68 | if ( TIFFReadRGBAImage(tif,w,h,raster,0) ) { |
| 69 | TIFFClose(tif); |
| 70 | base=ret->u.image; |
| 71 | for ( i=0; i<h; ++i ) { |
| 72 | ipt=(uint32_t *)(base->data+i*base->bytes_per_line); |
| 73 | fpt=raster+(h-1-i)*w; |
| 74 | for ( j=0; j<w; ++j ) |
| 75 | *ipt++ =COLOR_CREATE( |
| 76 | TIFFGetR(fpt[j]),TIFFGetG(fpt[j]),TIFFGetB(fpt[j])); |
| 77 | } |
| 78 | free(raster); |
| 79 | return( ret ); |
| 80 | } |
| 81 | |
| 82 | errorGImageReadTiff: |
| 83 | fprintf(stderr,"Bad input file \"%s\"\n",filename ); |
| 84 | errorGImageReadTiffMem: |
| 85 | free(raster); free(ret); |
| 86 | TIFFClose(tif); |
| 87 | return( NULL ); |
| 88 | } |
| 89 | |
| 90 | #endif |