3 * Purpose: Platform Independent TIFF Image Class Loader and Writer
\r
4 * 07/Aug/2001 Davide Pizzolato - www.xdp.it
\r
5 * CxImage version 6.0.0 02/Feb/2008
\r
10 #if CXIMAGE_SUPPORT_TIF
\r
12 #define FIX_16BPP_DARKIMG // + VK: if uncomment, dark 16bpp images are fixed
\r
14 #include "../tiff/tiffio.h"
\r
16 #define CVT(x) (((x) * 255L) / ((1L<<16)-1))
\r
17 #define SCALE(x) (((x)*((1L<<16)-1))/255)
\r
18 #define CalculateLine(width,bitdepth) (((width * bitdepth) + 7) / 8)
\r
19 #define CalculatePitch(line) (line + 3 & ~3)
\r
21 extern "C" TIFF* _TIFFOpenEx(CxFile* stream, const char* mode);
\r
23 ////////////////////////////////////////////////////////////////////////////////
\r
24 CxImageTIF::~CxImageTIF()
\r
26 if (m_tif2) TIFFClose(m_tif2);
\r
28 ////////////////////////////////////////////////////////////////////////////////
\r
29 #if CXIMAGE_SUPPORT_DECODE
\r
30 ////////////////////////////////////////////////////////////////////////////////
\r
31 bool CxImageTIF::Decode(CxFile * hFile)
\r
33 //Comment this line if you need more information on errors
\r
34 // TIFFSetErrorHandler(NULL); //<Patrick Hoffmann>
\r
36 //Open file and fill the TIFF structure
\r
37 // m_tif = TIFFOpen(imageFileName,"rb");
\r
38 TIFF* m_tif = _TIFFOpenEx(hFile, "rb");
\r
42 uint16 bitspersample=1;
\r
43 uint16 samplesperpixel=1;
\r
44 uint32 rowsperstrip=(DWORD)-1;
\r
45 uint16 photometric=0;
\r
46 uint16 compression=1;
\r
47 uint16 orientation=ORIENTATION_TOPLEFT; //<vho>
\r
48 uint16 res_unit; //<Trifon>
\r
50 float resolution, offset;
\r
52 BYTE *bits; //pointer to source data
\r
53 BYTE *bits2; //pointer to destination data
\r
57 //check if it's a tiff file
\r
59 cx_throw("Error encountered while opening TIFF file");
\r
61 // <Robert Abram> - 12/2002 : get NumFrames directly, instead of looping
\r
62 // info.nNumFrames=0;
\r
63 // while(TIFFSetDirectory(m_tif,(uint16)info.nNumFrames)) info.nNumFrames++;
\r
64 info.nNumFrames = TIFFNumberOfDirectories(m_tif);
\r
66 if (!TIFFSetDirectory(m_tif, (uint16)info.nFrame))
\r
67 cx_throw("Error: page not present in TIFF file");
\r
70 TIFFGetField(m_tif, TIFFTAG_IMAGEWIDTH, &width);
\r
71 TIFFGetField(m_tif, TIFFTAG_IMAGELENGTH, &height);
\r
72 TIFFGetField(m_tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
\r
73 TIFFGetField(m_tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
\r
74 TIFFGetField(m_tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
\r
75 TIFFGetField(m_tif, TIFFTAG_PHOTOMETRIC, &photometric);
\r
76 TIFFGetField(m_tif, TIFFTAG_ORIENTATION, &orientation);
\r
78 if (info.nEscape == -1) {
\r
79 // Return output dimensions only
\r
80 head.biWidth = width;
\r
81 head.biHeight = height;
\r
82 info.dwType = CXIMAGE_FORMAT_TIF;
\r
83 cx_throw("output dimensions returned");
\r
86 TIFFGetFieldDefaulted(m_tif, TIFFTAG_RESOLUTIONUNIT, &res_unit);
\r
87 if (TIFFGetField(m_tif, TIFFTAG_XRESOLUTION, &resolution))
\r
89 if (res_unit == RESUNIT_CENTIMETER) resolution = (float)(resolution*2.54f + 0.5f);
\r
90 SetXDPI((long)resolution);
\r
92 if (TIFFGetField(m_tif, TIFFTAG_YRESOLUTION, &resolution))
\r
94 if (res_unit == RESUNIT_CENTIMETER) resolution = (float)(resolution*2.54f + 0.5f);
\r
95 SetYDPI((long)resolution);
\r
98 if (TIFFGetField(m_tif, TIFFTAG_XPOSITION, &offset)) info.xOffset = (long)offset;
\r
99 if (TIFFGetField(m_tif, TIFFTAG_YPOSITION, &offset)) info.yOffset = (long)offset;
\r
102 info.nBkgndIndex =-1;
\r
104 if (rowsperstrip>height){
\r
105 rowsperstrip=height;
\r
106 TIFFSetField(m_tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
\r
109 isRGB = /*(bitspersample >= 8) && (VK: it is possible so for RGB to have < 8 bpp!)*/
\r
110 (photometric == PHOTOMETRIC_RGB) ||
\r
111 (photometric == PHOTOMETRIC_YCBCR) ||
\r
112 (photometric == PHOTOMETRIC_SEPARATED) ||
\r
113 (photometric == PHOTOMETRIC_LOGL) ||
\r
114 (photometric == PHOTOMETRIC_LOGLUV);
\r
117 head.biBitCount=24;
\r
119 if ((photometric==PHOTOMETRIC_MINISBLACK)||(photometric==PHOTOMETRIC_MINISWHITE)||(photometric==PHOTOMETRIC_PALETTE)){
\r
120 if (bitspersample == 1){
\r
121 head.biBitCount=1; //B&W image
\r
123 } else if (bitspersample == 4) {
\r
124 head.biBitCount=4; //16 colors gray scale
\r
125 head.biClrUsed =16;
\r
127 head.biBitCount=8; //gray scale
\r
128 head.biClrUsed =256;
\r
130 } else if (bitspersample == 4) {
\r
131 head.biBitCount=4; // 16 colors
\r
134 head.biBitCount=8; //256 colors
\r
135 head.biClrUsed=256;
\r
138 if ((bitspersample > 8) && (photometric==PHOTOMETRIC_PALETTE)) // + VK + (BIG palette! => convert to RGB)
\r
139 { head.biBitCount=24;
\r
144 if (info.nEscape) cx_throw("Cancelled"); // <vho> - cancel decoding
\r
146 Create(width,height,head.biBitCount,CXIMAGE_FORMAT_TIF); //image creation
\r
147 if (!pDib) cx_throw("CxImageTIF can't create image");
\r
149 #if CXIMAGE_SUPPORT_ALPHA
\r
150 if (samplesperpixel==4) AlphaCreate(); //add alpha support for 32bpp tiffs
\r
151 if (samplesperpixel==2 && bitspersample==8) AlphaCreate(); //add alpha support for 8bpp + alpha
\r
152 #endif //CXIMAGE_SUPPORT_ALPHA
\r
154 TIFFGetField(m_tif, TIFFTAG_COMPRESSION, &compression);
\r
155 SetCodecOption(compression); // <DPR> save original compression type
\r
158 // Read the whole image into one big RGBA buffer using
\r
159 // the traditional TIFFReadRGBAImage() API that we trust.
\r
160 uint32* raster; // retrieve RGBA image
\r
163 raster = (uint32*)_TIFFmalloc(width * height * sizeof (uint32));
\r
164 if (raster == NULL) cx_throw("No space for raster buffer");
\r
166 // Read the image in one chunk into an RGBA array
\r
167 if(!TIFFReadRGBAImage(m_tif, width, height, raster, 1)) {
\r
169 cx_throw("Corrupted TIFF file!");
\r
172 // read the raster lines and save them in the DIB
\r
173 // with RGB mode, we have to change the order of the 3 samples RGB
\r
175 bits2 = info.pImage;
\r
176 for (y = 0; y < height; y++) {
\r
178 if (info.nEscape){ // <vho> - cancel decoding
\r
180 cx_throw("Cancelled");
\r
184 for (x = 0; x < width; x++) {
\r
185 *bits++ = (BYTE)TIFFGetB(row[x]);
\r
186 *bits++ = (BYTE)TIFFGetG(row[x]);
\r
187 *bits++ = (BYTE)TIFFGetR(row[x]);
\r
188 #if CXIMAGE_SUPPORT_ALPHA
\r
189 if (samplesperpixel==4) AlphaSet(x,y,(BYTE)TIFFGetA(row[x]));
\r
190 #endif //CXIMAGE_SUPPORT_ALPHA
\r
193 bits2 += info.dwEffWidth;
\r
197 int BIG_palette = (bitspersample > 8) && // + VK
\r
198 (photometric==PHOTOMETRIC_PALETTE);
\r
199 if (BIG_palette && (bitspersample > 24)) // + VK
\r
200 cx_throw("Too big palette to handle"); // + VK
\r
203 pal=(RGBQUAD*)calloc(BIG_palette ? 1<<bitspersample : 256,sizeof(RGBQUAD));
\r
204 // ! VK: it coasts nothing but more correct to use 256 as temp palette storage
\r
205 // ! VK: but for case of BIG palette it just copied
\r
206 if (pal==NULL) cx_throw("Unable to allocate TIFF palette");
\r
208 int bpp = bitspersample <= 8 ? bitspersample : 8; // + VK (to use instead of bitspersample for case of > 8)
\r
210 // set up the colormap based on photometric
\r
211 switch(photometric) {
\r
212 case PHOTOMETRIC_MINISBLACK: // bitmap and greyscale image types
\r
213 case PHOTOMETRIC_MINISWHITE:
\r
214 if (bitspersample == 1) { // Monochrome image
\r
215 if (photometric == PHOTOMETRIC_MINISBLACK) {
\r
216 pal[1].rgbRed = pal[1].rgbGreen = pal[1].rgbBlue = 255;
\r
218 pal[0].rgbRed = pal[0].rgbGreen = pal[0].rgbBlue = 255;
\r
220 } else { // need to build the scale for greyscale images
\r
221 if (photometric == PHOTOMETRIC_MINISBLACK) {
\r
222 for (int i=0; i<(1<<bpp); i++){
\r
223 pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (BYTE)(i*(255/((1<<bpp)-1)));
\r
226 for (int i=0; i<(1<<bpp); i++){
\r
227 pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (BYTE)(255-i*(255/((1<<bpp)-1)));
\r
232 case PHOTOMETRIC_PALETTE: // color map indexed
\r
236 TIFFGetField(m_tif, TIFFTAG_COLORMAP, &red, &green, &blue);
\r
238 // Is the palette 16 or 8 bits ?
\r
239 BOOL Palette16Bits = /*FALSE*/ BIG_palette;
\r
240 if (!BIG_palette) {
\r
243 if (red[n] >= 256 || green[n] >= 256 || blue[n] >= 256) {
\r
244 Palette16Bits=TRUE;
\r
250 // load the palette in the DIB
\r
251 for (int i = (1 << ( BIG_palette ? bitspersample : bpp )) - 1; i >= 0; i--) {
\r
252 if (Palette16Bits) {
\r
253 pal[i].rgbRed =(BYTE) CVT(red[i]);
\r
254 pal[i].rgbGreen = (BYTE) CVT(green[i]);
\r
255 pal[i].rgbBlue = (BYTE) CVT(blue[i]);
\r
257 pal[i].rgbRed = (BYTE) red[i];
\r
258 pal[i].rgbGreen = (BYTE) green[i];
\r
259 pal[i].rgbBlue = (BYTE) blue[i];
\r
264 if (!BIG_palette) { // + VK (BIG palette is stored until image is ready)
\r
265 SetPalette(pal,/*head.biClrUsed*/ 1<<bpp); //palette assign // * VK
\r
270 // read the tiff lines and save them in the DIB
\r
273 int line = CalculateLine(width, bitspersample * samplesperpixel);
\r
275 long bitsize = TIFFStripSize(m_tif);
\r
276 //verify bitsize: could be wrong if StripByteCounts is missing.
\r
277 if (bitsize<(long)(head.biSizeImage*samplesperpixel))
\r
278 bitsize = head.biSizeImage*samplesperpixel;
\r
280 if ((bitspersample > 8) && (bitspersample != 16)) // + VK (for bitspersample == 9..15,17..32..64
\r
281 bitsize *= (bitspersample + 7)/8;
\r
283 int tiled_image = TIFFIsTiled(m_tif);
\r
285 BYTE* tilebuf=NULL;
\r
287 TIFFGetField(m_tif, TIFFTAG_TILEWIDTH, &tw);
\r
288 TIFFGetField(m_tif, TIFFTAG_TILELENGTH, &tl);
\r
290 bitsize = TIFFTileSize(m_tif) * (int)(1+width/tw);
\r
291 tilebuf = (BYTE*)malloc(TIFFTileSize(m_tif));
\r
294 bits = (BYTE*)malloc(bitspersample==16? bitsize*2 : bitsize); // * VK
\r
295 BYTE * bits16 = NULL; // + VK
\r
296 int line16 = 0; // + VK
\r
298 if (!tiled_image && bitspersample==16) { // + VK +
\r
300 line = CalculateLine(width, 8 * samplesperpixel);
\r
302 bits = (BYTE*)malloc(bitsize);
\r
306 if (bits16) free(bits16); // + VK
\r
307 if (pal) free(pal); // + VK
\r
308 if (tilebuf)free(tilebuf); // + VK
\r
309 cx_throw("CxImageTIF can't allocate memory");
\r
312 #ifdef FIX_16BPP_DARKIMG // + VK: for each line, store shift count bits used to fix it
\r
313 BYTE* row_shifts = NULL;
\r
314 if (bits16) row_shifts = (BYTE*)malloc(height);
\r
317 for (ys = 0; ys < height; ys += rowsperstrip) {
\r
319 if (info.nEscape){ // <vho> - cancel decoding
\r
321 cx_throw("Cancelled");
\r
324 nrow = (ys + rowsperstrip > height ? height - ys : rowsperstrip);
\r
327 uint32 imagew = TIFFScanlineSize(m_tif);
\r
328 uint32 tilew = TIFFTileRowSize(m_tif);
\r
329 int iskew = imagew - tilew;
\r
330 uint8* bufp = (uint8*) bits;
\r
333 for (uint32 col = 0; col < width; col += tw) {
\r
334 if (TIFFReadTile(m_tif, tilebuf, col, ys, 0, 0) < 0){
\r
337 cx_throw("Corrupted tiled TIFF file!");
\r
340 if (colb + tw > imagew) {
\r
341 uint32 owidth = imagew - colb;
\r
342 uint32 oskew = tilew - owidth;
\r
343 TileToStrip(bufp + colb, tilebuf, nrow, owidth, oskew + iskew, oskew );
\r
345 TileToStrip(bufp + colb, tilebuf, nrow, tilew, iskew, 0);
\r
351 if (TIFFReadEncodedStrip(m_tif, TIFFComputeStrip(m_tif, ys, 0),
\r
352 (bits16? bits16 : bits), nrow * (bits16 ? line16 : line)) == -1) { // * VK
\r
354 #ifdef NOT_IGNORE_CORRUPTED
\r
356 if (bits16) free(bits16); // + VK
\r
357 cx_throw("Corrupted TIFF file!");
\r
364 for (y = 0; y < nrow; y++) {
\r
365 long offset=(nrow-y-1)*line;
\r
366 if ((bitspersample==16) && !BIG_palette) { // * VK
\r
367 long offset16 = (nrow-y-1)*line16; // + VK
\r
368 if (bits16) { // + VK +
\r
369 #ifdef FIX_16BPP_DARKIMG
\r
371 BYTE hi_byte, hi_max=0;
\r
373 for (xi=0;xi<(uint32)line;xi++) {
\r
374 hi_byte = bits16[xi*2+offset16+1];
\r
378 the_shift = (hi_max == 0) ? 8 : 0;
\r
380 while( ! (hi_max & 0x80) ) {
\r
384 row_shifts[height-ys-nrow+y] = the_shift;
\r
385 the_shift = 8 - the_shift;
\r
386 for (xi=0;xi<(uint32)line;xi++)
\r
387 bits[xi+offset]= ((bits16[xi*2+offset16+1]<<8) | bits16[xi*2+offset16]) >> the_shift;
\r
389 for (DWORD xi=0;xi<(uint32)line;xi++)
\r
390 bits[xi+offset]=bits16[xi*2+offset16+1];
\r
393 for (DWORD xi=0;xi<width;xi++)
\r
394 bits[xi+offset]=bits[xi*2+offset+1];
\r
397 if (samplesperpixel==1) {
\r
400 long offset16 = (nrow-y-1)*line16; // + VK
\r
401 MoveBitsPal( info.pImage + info.dwEffWidth * (height-ys-nrow+y),
\r
402 bits16 + offset16, width, bitspersample, pal );
\r
404 MoveBitsPal( info.pImage + info.dwEffWidth * (height-ys-nrow+y),
\r
405 bits + offset, width, bitspersample, pal );
\r
406 else if ((bitspersample == head.biBitCount) ||
\r
407 (bitspersample == 16)) //simple 8bpp, 4bpp image or 16bpp
\r
408 memcpy(info.pImage+info.dwEffWidth*(height-ys-nrow+y),bits+offset,info.dwEffWidth);
\r
410 MoveBits( info.pImage + info.dwEffWidth * (height-ys-nrow+y),
\r
411 bits + offset, width, bitspersample );
\r
412 } else if (samplesperpixel==2) { //8bpp image with alpha layer
\r
415 int yi=height-ys-nrow+y;
\r
416 #if CXIMAGE_SUPPORT_ALPHA
\r
417 if (!pAlpha) AlphaCreate(); // + VK
\r
418 #endif //CXIMAGE_SUPPORT_ALPHA
\r
420 SetPixelIndex(xi,yi,bits[ii+offset]);
\r
421 #if CXIMAGE_SUPPORT_ALPHA
\r
422 AlphaSet(xi,yi,bits[ii+offset+1]);
\r
423 #endif //CXIMAGE_SUPPORT_ALPHA
\r
426 if (xi>=(int)width){
\r
431 } else { //photometric==PHOTOMETRIC_CIELAB
\r
432 if (head.biBitCount!=24){ //fix image
\r
433 Create(width,height,24,CXIMAGE_FORMAT_TIF);
\r
434 #if CXIMAGE_SUPPORT_ALPHA
\r
435 if (samplesperpixel==4) AlphaCreate();
\r
436 #endif //CXIMAGE_SUPPORT_ALPHA
\r
441 int yi=height-ys-nrow+y;
\r
443 int l,a,b,bitsoffset;
\r
444 double p,cx,cy,cz,cr,cg,cb;
\r
445 while (ii</*line*/width){ // * VK
\r
446 bitsoffset = ii*samplesperpixel+offset;
\r
447 l=bits[bitsoffset];
\r
448 a=bits[bitsoffset+1];
\r
449 b=bits[bitsoffset+2];
\r
453 p = (l/2.55 + 16) / 116.0;
\r
454 cx = pow( p + a * 0.002, 3);
\r
456 cz = pow( p - b * 0.005, 3);
\r
462 cr = 3.240479 * cx - 1.537150 * cy - 0.498535 * cz;
\r
463 cg = -0.969256 * cx + 1.875992 * cy + 0.041556 * cz;
\r
464 cb = 0.055648 * cx - 0.204043 * cy + 1.057311 * cz;
\r
466 if ( cr > 0.00304 ) cr = 1.055 * pow(cr,0.41667) - 0.055;
\r
467 else cr = 12.92 * cr;
\r
468 if ( cg > 0.00304 ) cg = 1.055 * pow(cg,0.41667) - 0.055;
\r
469 else cg = 12.92 * cg;
\r
470 if ( cb > 0.00304 ) cb = 1.055 * pow(cb,0.41667) - 0.055;
\r
471 else cb = 12.92 * cb;
\r
473 c.rgbRed =(BYTE)max(0,min(255,(int)(cr*255)));
\r
474 c.rgbGreen=(BYTE)max(0,min(255,(int)(cg*255)));
\r
475 c.rgbBlue =(BYTE)max(0,min(255,(int)(cb*255)));
\r
477 SetPixelColor(xi,yi,c);
\r
478 #if CXIMAGE_SUPPORT_ALPHA
\r
479 if (samplesperpixel==4) AlphaSet(xi,yi,bits[bitsoffset+3]);
\r
480 #endif //CXIMAGE_SUPPORT_ALPHA
\r
483 if (xi>=(int)width){
\r
492 if (bits16) free(bits16);
\r
494 #ifdef FIX_16BPP_DARKIMG
\r
495 if (row_shifts && (samplesperpixel == 1) && (bitspersample==16) && !BIG_palette) {
\r
496 // 1. calculate maximum necessary shift
\r
497 int min_row_shift = 8;
\r
498 for( y=0; y<height; y++ ) {
\r
499 if (min_row_shift > row_shifts[y]) min_row_shift = row_shifts[y];
\r
501 // 2. for rows having less shift value, correct such rows:
\r
502 for( y=0; y<height; y++ ) {
\r
503 if (min_row_shift < row_shifts[y]) {
\r
504 int need_shift = row_shifts[y] - min_row_shift;
\r
505 BYTE* data = info.pImage + info.dwEffWidth * y;
\r
506 for( x=0; x<width; x++, data++ )
\r
507 *data >>= need_shift;
\r
511 if (row_shifts) free( row_shifts );
\r
514 if (tiled_image) free(tilebuf);
\r
515 if (pal) free(pal);
\r
517 switch(orientation){
\r
518 case ORIENTATION_TOPRIGHT: /* row 0 top, col 0 rhs */
\r
521 case ORIENTATION_BOTRIGHT: /* row 0 bottom, col 0 rhs */
\r
525 case ORIENTATION_BOTLEFT: /* row 0 bottom, col 0 lhs */
\r
528 case ORIENTATION_LEFTTOP: /* row 0 lhs, col 0 top */
\r
532 case ORIENTATION_RIGHTTOP: /* row 0 rhs, col 0 top */
\r
535 case ORIENTATION_RIGHTBOT: /* row 0 rhs, col 0 bottom */
\r
539 case ORIENTATION_LEFTBOT: /* row 0 lhs, col 0 bottom */
\r
546 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
547 if (m_tif) TIFFClose(m_tif);
\r
548 if (info.nEscape == -1 && info.dwType == CXIMAGE_FORMAT_TIF) return true;
\r
554 ////////////////////////////////////////////////////////////////////////////////
\r
555 #endif //CXIMAGE_SUPPORT_DECODE
\r
556 ////////////////////////////////////////////////////////////////////////////////
\r
557 #if CXIMAGE_SUPPORT_ENCODE
\r
558 ////////////////////////////////////////////////////////////////////////////////
\r
559 bool CxImageTIF::Encode(CxFile * hFile, bool bAppend)
\r
563 if (hFile==NULL) cx_throw(CXIMAGE_ERR_NOFILE);
\r
564 if (pDib==NULL) cx_throw(CXIMAGE_ERR_NOIMAGE);
\r
566 // <RJ> replaced "w+b" with "a", to append an image directly on an existing file
\r
567 if (m_tif2==NULL) m_tif2=_TIFFOpenEx(hFile, "a");
\r
568 if (m_tif2==NULL) cx_throw("initialization fail");
\r
570 if (bAppend || m_pages) m_multipage=true;
\r
573 if (!EncodeBody(m_tif2,m_multipage,m_pages,m_pages)) cx_throw("Error saving TIFF file");
\r
575 if (!TIFFWriteDirectory(m_tif2)) cx_throw("Error saving TIFF directory");
\r
578 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
595 ////////////////////////////////////////////////////////////////////////////////
\r
596 // Thanks to Abe <God(dot)bless(at)marihuana(dot)com>
\r
597 bool CxImageTIF::Encode(CxFile * hFile, CxImage ** pImages, int pagecount)
\r
601 if (hFile==NULL) cx_throw("invalid file pointer");
\r
602 if (pImages==NULL || pagecount<=0) cx_throw("multipage TIFF, no images!");
\r
605 for (i=0; i<pagecount; i++){
\r
606 if (pImages[i]==NULL)
\r
607 cx_throw("Bad image pointer");
\r
608 if (!(pImages[i]->IsValid()))
\r
609 cx_throw("Empty image");
\r
613 for (i=0; i<pagecount; i++){
\r
614 ghost.Ghost(pImages[i]);
\r
615 if (!ghost.Encode(hFile,true)) cx_throw("Error saving TIFF file");
\r
618 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
623 ////////////////////////////////////////////////////////////////////////////////
\r
624 bool CxImageTIF::EncodeBody(TIFF *m_tif, bool multipage, int page, int pagecount)
\r
626 uint32 height=head.biHeight;
\r
627 uint32 width=head.biWidth;
\r
628 uint16 bitcount=head.biBitCount;
\r
629 uint16 bitspersample;
\r
630 uint16 samplesperpixel;
\r
631 uint16 photometric=0;
\r
632 uint16 compression;
\r
637 samplesperpixel = ((bitcount == 24) || (bitcount == 32)) ? (BYTE)3 : (BYTE)1;
\r
638 #if CXIMAGE_SUPPORT_ALPHA
\r
639 if (bitcount==24 && AlphaIsValid()) { bitcount=32; samplesperpixel=4; }
\r
640 #endif //CXIMAGE_SUPPORT_ALPHA
\r
642 bitspersample = bitcount / samplesperpixel;
\r
644 //set the PHOTOMETRIC tag
\r
645 RGBQUAD *rgb = GetPalette();
\r
646 switch (bitcount) {
\r
648 if (CompareColors(&rgb[0],&rgb[1])<0) {
\r
649 /* <abe> some viewers do not handle PHOTOMETRIC_MINISBLACK:
\r
650 * let's transform the image in PHOTOMETRIC_MINISWHITE
\r
652 //invert the colors
\r
653 RGBQUAD tempRGB=GetPaletteColor(0);
\r
654 SetPaletteColor(0,GetPaletteColor(1));
\r
655 SetPaletteColor(1,tempRGB);
\r
656 //invert the pixels
\r
657 BYTE *iSrc=info.pImage;
\r
658 for (unsigned long i=0;i<head.biSizeImage;i++){
\r
659 *iSrc=(BYTE)~(*(iSrc));
\r
662 photometric = PHOTOMETRIC_MINISWHITE;
\r
663 //photometric = PHOTOMETRIC_MINISBLACK;
\r
665 photometric = PHOTOMETRIC_MINISWHITE;
\r
668 case 4: // Check if the DIB has a color or a greyscale palette
\r
670 photometric = PHOTOMETRIC_MINISBLACK; //default to gray scale
\r
671 for (x = 0; x < head.biClrUsed; x++) {
\r
672 if ((rgb->rgbRed != x)||(rgb->rgbRed != rgb->rgbGreen)||(rgb->rgbRed != rgb->rgbBlue)){
\r
673 photometric = PHOTOMETRIC_PALETTE;
\r
681 photometric = PHOTOMETRIC_RGB;
\r
685 #if CXIMAGE_SUPPORT_ALPHA
\r
686 if (AlphaIsValid() && bitcount==8) samplesperpixel=2; //8bpp + alpha layer
\r
687 #endif //CXIMAGE_SUPPORT_ALPHA
\r
689 // line = CalculateLine(width, bitspersample * samplesperpixel);
\r
690 // pitch = (uint16)CalculatePitch(line);
\r
692 //prepare the palette struct
\r
696 memcpy(pal,GetPalette(),GetPaletteSize());
\r
697 for(WORD a=0;a<head.biClrUsed;a++){ //swap blue and red components
\r
698 b=pal[a].rgbBlue; pal[a].rgbBlue=pal[a].rgbRed; pal[a].rgbRed=b;
\r
702 // handle standard width/height/bpp stuff
\r
703 TIFFSetField(m_tif, TIFFTAG_IMAGEWIDTH, width);
\r
704 TIFFSetField(m_tif, TIFFTAG_IMAGELENGTH, height);
\r
705 TIFFSetField(m_tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
\r
706 TIFFSetField(m_tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
\r
707 TIFFSetField(m_tif, TIFFTAG_PHOTOMETRIC, photometric);
\r
708 TIFFSetField(m_tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // single image plane
\r
709 TIFFSetField(m_tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
\r
711 uint32 rowsperstrip = TIFFDefaultStripSize(m_tif, (uint32) -1); //<REC> gives better compression
\r
712 TIFFSetField(m_tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
\r
715 TIFFSetField(m_tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
\r
716 TIFFSetField(m_tif, TIFFTAG_XRESOLUTION, (float)info.xDPI);
\r
717 TIFFSetField(m_tif, TIFFTAG_YRESOLUTION, (float)info.yDPI);
\r
718 // TIFFSetField(m_tif, TIFFTAG_XPOSITION, (float)info.xOffset);
\r
719 // TIFFSetField(m_tif, TIFFTAG_YPOSITION, (float)info.yOffset);
\r
721 // multi-paging - Thanks to Abe <God(dot)bless(at)marihuana(dot)com>
\r
724 char page_number[20];
\r
725 sprintf(page_number, "Page %d", page);
\r
727 TIFFSetField(m_tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
\r
728 TIFFSetField(m_tif, TIFFTAG_PAGENUMBER, page,pagecount);
\r
729 TIFFSetField(m_tif, TIFFTAG_PAGENAME, page_number);
\r
731 TIFFSetField(m_tif, TIFFTAG_SUBFILETYPE, 0);
\r
734 // palettes (image colormaps are automatically scaled to 16-bits)
\r
735 if (photometric == PHOTOMETRIC_PALETTE) {
\r
737 r = (uint16 *) _TIFFmalloc(sizeof(uint16) * 3 * 256);
\r
741 for (int i = 255; i >= 0; i--) {
\r
742 b[i] = (uint16)SCALE((uint16)pal[i].rgbRed);
\r
743 g[i] = (uint16)SCALE((uint16)pal[i].rgbGreen);
\r
744 r[i] = (uint16)SCALE((uint16)pal[i].rgbBlue);
\r
747 TIFFSetField(m_tif, TIFFTAG_COLORMAP, r, g, b);
\r
752 if (GetCodecOption(CXIMAGE_FORMAT_TIF)) {
\r
753 compression = (WORD)GetCodecOption(CXIMAGE_FORMAT_TIF);
\r
755 switch (bitcount) {
\r
757 compression = COMPRESSION_CCITTFAX4;
\r
761 compression = COMPRESSION_LZW;
\r
765 compression = COMPRESSION_JPEG;
\r
768 compression = COMPRESSION_NONE;
\r
772 TIFFSetField(m_tif, TIFFTAG_COMPRESSION, compression);
\r
774 switch (compression) {
\r
775 case COMPRESSION_JPEG:
\r
776 TIFFSetField(m_tif, TIFFTAG_JPEGQUALITY, GetJpegQuality());
\r
777 TIFFSetField(m_tif, TIFFTAG_ROWSPERSTRIP, ((7+rowsperstrip)>>3)<<3);
\r
779 case COMPRESSION_LZW:
\r
780 if (bitcount>=8) TIFFSetField(m_tif, TIFFTAG_PREDICTOR, 2);
\r
784 // read the DIB lines from bottom to top and save them in the TIF
\r
787 switch(bitcount) {
\r
792 if (samplesperpixel==1){
\r
793 bits = (BYTE*)malloc(info.dwEffWidth);
\r
794 if (!bits) return false;
\r
795 for (y = 0; y < height; y++) {
\r
796 memcpy(bits,info.pImage + (height - y - 1)*info.dwEffWidth,info.dwEffWidth);
\r
797 if (TIFFWriteScanline(m_tif,bits, y, 0)==-1){
\r
804 #if CXIMAGE_SUPPORT_ALPHA
\r
805 else { //8bpp + alpha layer
\r
806 bits = (BYTE*)malloc(2*width);
\r
807 if (!bits) return false;
\r
808 for (y = 0; y < height; y++) {
\r
809 for (x=0;x<width;x++){
\r
810 bits[2*x]=BlindGetPixelIndex(x,height - y - 1);
\r
811 bits[2*x+1]=AlphaGet(x,height - y - 1);
\r
813 if (TIFFWriteScanline(m_tif,bits, y, 0)==-1) {
\r
820 #endif //CXIMAGE_SUPPORT_ALPHA
\r
825 BYTE *buffer = (BYTE *)malloc(info.dwEffWidth);
\r
826 if (!buffer) return false;
\r
827 for (y = 0; y < height; y++) {
\r
828 // get a pointer to the scanline
\r
829 memcpy(buffer, info.pImage + (height - y - 1)*info.dwEffWidth, info.dwEffWidth);
\r
830 // TIFFs store color data RGB instead of BGR
\r
831 BYTE *pBuf = buffer;
\r
832 for (x = 0; x < width; x++) {
\r
833 BYTE tmp = pBuf[0];
\r
838 // write the scanline to disc
\r
839 if (TIFFWriteScanline(m_tif, buffer, y, 0)==-1){
\r
849 #if CXIMAGE_SUPPORT_ALPHA
\r
850 BYTE *buffer = (BYTE *)malloc((info.dwEffWidth*4)/3);
\r
851 if (!buffer) return false;
\r
852 for (y = 0; y < height; y++) {
\r
853 // get a pointer to the scanline
\r
854 memcpy(buffer, info.pImage + (height - y - 1)*info.dwEffWidth, info.dwEffWidth);
\r
855 // TIFFs store color data RGB instead of BGR
\r
856 BYTE *pSrc = buffer + 3 * width;
\r
857 BYTE *pDst = buffer + 4 * width;
\r
858 for (x = 0; x < width; x++) {
\r
861 pDst[3] = AlphaGet(width-x-1,height-y-1);
\r
866 // write the scanline to disc
\r
867 if (TIFFWriteScanline(m_tif, buffer, y, 0)==-1){
\r
873 #endif //CXIMAGE_SUPPORT_ALPHA
\r
879 ////////////////////////////////////////////////////////////////////////////////
\r
880 #endif // CXIMAGE_SUPPORT_ENCODE
\r
881 ////////////////////////////////////////////////////////////////////////////////
\r
882 void CxImageTIF::TileToStrip(uint8* out, uint8* in, uint32 rows, uint32 cols, int outskew, int inskew)
\r
884 while (rows-- > 0) {
\r
892 ////////////////////////////////////////////////////////////////////////////////
\r
893 TIFF* CxImageTIF::TIFFOpenEx(CxFile * hFile)
\r
895 if (hFile) return _TIFFOpenEx(hFile, "rb");
\r
898 ////////////////////////////////////////////////////////////////////////////////
\r
899 void CxImageTIF::TIFFCloseEx(TIFF* tif)
\r
901 if (tif) TIFFClose(tif);
\r
903 ////////////////////////////////////////////////////////////////////////////////
\r
904 void CxImageTIF::MoveBits( BYTE* dest, BYTE* from, int count, int bpp )
\r
909 while (count-- > 0) {
\r
910 if (offbits + bpp <= 8)
\r
911 w = *from >> (8 - offbits - bpp);
\r
913 w = *from++ << (offbits + bpp - 8);
\r
914 w |= *from >> (16 - offbits - bpp);
\r
917 if (offbits >= 8) {
\r
919 if (offbits == 0) from++;
\r
921 *dest++ = (BYTE)w & ((1 << bpp)-1);
\r
923 } else if (bpp < 16) {
\r
924 while (count-- > 0) {
\r
925 d = (*from << 24) | (from[1]<<16) | (from[2]<<8) | from[3];
\r
926 d >>= (24 - offbits);
\r
927 *dest++ = (BYTE) ( d );
\r
929 while (offbits >= 8) {
\r
934 } else if (bpp < 32) {
\r
935 while (count-- > 0) {
\r
936 d = (*from << 24) | (from[1]<<16) | (from[2]<<8) | from[3];
\r
937 //d = *(uint32*)from;
\r
938 *dest++ = (BYTE) ( d >> (offbits + bpp - 8) );
\r
940 while (offbits >= 8) {
\r
946 while (count-- > 0) {
\r
947 d = *(uint32*)from;
\r
948 *dest++ = (BYTE) (d >> 24);
\r
953 ////////////////////////////////////////////////////////////////////////////////
\r
954 void CxImageTIF::MoveBitsPal( BYTE* dest, BYTE*from, int count, int bpp, RGBQUAD* pal )
\r
958 while (count-- > 0) {
\r
959 d = (*from << 24) | ( *( from + 1 ) << 16 )
\r
960 | ( *( from + 2 ) << 8 )
\r
961 | ( *( from + 3 ) );
\r
962 palidx = (uint16) (d >> (32 - offbits - bpp));
\r
965 palidx = (palidx >> 8) | (palidx <<8);
\r
967 } else palidx = (palidx >> 8) | (palidx << 8);
\r
968 *dest++ = pal[palidx].rgbBlue;
\r
969 *dest++ = pal[palidx].rgbGreen;
\r
970 *dest++ = pal[palidx].rgbRed;
\r
972 while (offbits >= 8) {
\r
978 ////////////////////////////////////////////////////////////////////////////////
\r
980 #endif // CXIMAGE_SUPPORT_TIF
\r