|
Blender V2.27
|
00001 /* 00002 * tiff.c 00003 * 00004 * $Id: tiff.c 34290 2011-01-13 04:53:55Z campbellbarton $ 00005 * 00006 * ***** BEGIN GPL LICENSE BLOCK ***** 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * Contributor(s): Jonathan Merritt. 00023 * 00024 * ***** END GPL LICENSE BLOCK ***** 00025 */ 00026 00043 #ifdef WITH_TIFF 00044 00045 #include <string.h> 00046 00047 #include "imbuf.h" 00048 00049 #include "BLI_math.h" 00050 #include "BLI_string.h" 00051 #include "BLI_utildefines.h" 00052 00053 #include "BKE_global.h" 00054 00055 00056 #include "IMB_imbuf_types.h" 00057 #include "IMB_imbuf.h" 00058 00059 #include "IMB_allocimbuf.h" 00060 #include "IMB_filetype.h" 00061 #include "IMB_filter.h" 00062 00063 #include "tiffio.h" 00064 00065 00066 00067 /*********************** 00068 * Local declarations. * 00069 ***********************/ 00070 /* Reading and writing of an in-memory TIFF file. */ 00071 static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n); 00072 static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n); 00073 static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence); 00074 static int imb_tiff_CloseProc(thandle_t handle); 00075 static toff_t imb_tiff_SizeProc(thandle_t handle); 00076 static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize); 00077 static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size); 00078 00079 00080 /* Structure for in-memory TIFF file. */ 00081 typedef struct ImbTIFFMemFile { 00082 unsigned char *mem; /* Location of first byte of TIFF file. */ 00083 toff_t offset; /* Current offset within the file. */ 00084 tsize_t size; /* Size of the TIFF file. */ 00085 } ImbTIFFMemFile; 00086 #define IMB_TIFF_GET_MEMFILE(x) ((ImbTIFFMemFile*)(x)); 00087 00088 00089 00090 /***************************** 00091 * Function implementations. * 00092 *****************************/ 00093 00094 00095 static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size) 00096 { 00097 (void)fd; 00098 (void)base; 00099 (void)size; 00100 } 00101 00102 static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize) 00103 { 00104 (void)fd; 00105 (void)pbase; 00106 (void)psize; 00107 00108 return (0); 00109 } 00110 00121 static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n) 00122 { 00123 tsize_t nRemaining, nCopy; 00124 ImbTIFFMemFile* mfile; 00125 void *srcAddr; 00126 00127 /* get the pointer to the in-memory file */ 00128 mfile = IMB_TIFF_GET_MEMFILE(handle); 00129 if(!mfile || !mfile->mem) { 00130 fprintf(stderr, "imb_tiff_ReadProc: !mfile || !mfile->mem!\n"); 00131 return 0; 00132 } 00133 00134 /* find the actual number of bytes to read (copy) */ 00135 nCopy = n; 00136 if((tsize_t)mfile->offset >= mfile->size) 00137 nRemaining = 0; 00138 else 00139 nRemaining = mfile->size - mfile->offset; 00140 00141 if(nCopy > nRemaining) 00142 nCopy = nRemaining; 00143 00144 /* on EOF, return immediately and read (copy) nothing */ 00145 if(nCopy <= 0) 00146 return (0); 00147 00148 /* all set -> do the read (copy) */ 00149 srcAddr = (void*)(&(mfile->mem[mfile->offset])); 00150 memcpy((void*)data, srcAddr, nCopy); 00151 mfile->offset += nCopy; /* advance file ptr by copied bytes */ 00152 return nCopy; 00153 } 00154 00155 00156 00163 static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n) 00164 { 00165 (void)handle; 00166 (void)data; 00167 (void)n; 00168 00169 printf("imb_tiff_WriteProc: this function should not be called.\n"); 00170 return (-1); 00171 } 00172 00173 00174 00189 static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence) 00190 { 00191 ImbTIFFMemFile *mfile; 00192 toff_t new_offset; 00193 00194 /* get the pointer to the in-memory file */ 00195 mfile = IMB_TIFF_GET_MEMFILE(handle); 00196 if(!mfile || !mfile->mem) { 00197 fprintf(stderr, "imb_tiff_SeekProc: !mfile || !mfile->mem!\n"); 00198 return (-1); 00199 } 00200 00201 /* find the location we plan to seek to */ 00202 switch (whence) { 00203 case SEEK_SET: 00204 new_offset = ofs; 00205 break; 00206 case SEEK_CUR: 00207 new_offset = mfile->offset + ofs; 00208 break; 00209 default: 00210 /* no other types are supported - return an error */ 00211 fprintf(stderr, 00212 "imb_tiff_SeekProc: " 00213 "Unsupported TIFF SEEK type.\n"); 00214 return (-1); 00215 } 00216 00217 /* set the new location */ 00218 mfile->offset = new_offset; 00219 return mfile->offset; 00220 } 00221 00222 00223 00236 static int imb_tiff_CloseProc(thandle_t handle) 00237 { 00238 ImbTIFFMemFile *mfile; 00239 00240 /* get the pointer to the in-memory file */ 00241 mfile = IMB_TIFF_GET_MEMFILE(handle); 00242 if(!mfile || !mfile->mem) { 00243 fprintf(stderr,"imb_tiff_CloseProc: !mfile || !mfile->mem!\n"); 00244 return (0); 00245 } 00246 00247 /* virtually close the file */ 00248 mfile->mem = NULL; 00249 mfile->offset = 0; 00250 mfile->size = 0; 00251 00252 return (0); 00253 } 00254 00255 00256 00262 static toff_t imb_tiff_SizeProc(thandle_t handle) 00263 { 00264 ImbTIFFMemFile* mfile; 00265 00266 /* get the pointer to the in-memory file */ 00267 mfile = IMB_TIFF_GET_MEMFILE(handle); 00268 if(!mfile || !mfile->mem) { 00269 fprintf(stderr,"imb_tiff_SizeProc: !mfile || !mfile->mem!\n"); 00270 return (0); 00271 } 00272 00273 /* return the size */ 00274 return (toff_t)(mfile->size); 00275 } 00276 00277 static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, size_t size) 00278 { 00279 /* open the TIFF client layer interface to the in-memory file */ 00280 memFile->mem = mem; 00281 memFile->offset = 0; 00282 memFile->size = size; 00283 00284 return TIFFClientOpen("(Blender TIFF Interface Layer)", 00285 "r", (thandle_t)(memFile), 00286 imb_tiff_ReadProc, imb_tiff_WriteProc, 00287 imb_tiff_SeekProc, imb_tiff_CloseProc, 00288 imb_tiff_SizeProc, imb_tiff_DummyMapProc, imb_tiff_DummyUnmapProc); 00289 } 00290 00306 #define IMB_TIFF_NCB 4 /* number of comparison bytes used */ 00307 int imb_is_a_tiff(unsigned char *mem) 00308 { 00309 char big_endian[IMB_TIFF_NCB] = { 0x4d, 0x4d, 0x00, 0x2a }; 00310 char lil_endian[IMB_TIFF_NCB] = { 0x49, 0x49, 0x2a, 0x00 }; 00311 00312 return ( (memcmp(big_endian, mem, IMB_TIFF_NCB) == 0) || 00313 (memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0) ); 00314 } 00315 00316 static void scanline_contig_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int spp) 00317 { 00318 int i; 00319 for (i=0; i < scanline_w; i++) { 00320 rectf[i*4 + 0] = sbuf[i*spp + 0] / 65535.0; 00321 rectf[i*4 + 1] = sbuf[i*spp + 1] / 65535.0; 00322 rectf[i*4 + 2] = sbuf[i*spp + 2] / 65535.0; 00323 rectf[i*4 + 3] = (spp==4)?(sbuf[i*spp + 3] / 65535.0):1.0; 00324 } 00325 } 00326 00327 static void scanline_contig_32bit(float *rectf, float *fbuf, int scanline_w, int spp) 00328 { 00329 int i; 00330 for (i=0; i < scanline_w; i++) { 00331 rectf[i*4 + 0] = fbuf[i*spp + 0]; 00332 rectf[i*4 + 1] = fbuf[i*spp + 1]; 00333 rectf[i*4 + 2] = fbuf[i*spp + 2]; 00334 rectf[i*4 + 3] = (spp==4)?fbuf[i*spp + 3]:1.0; 00335 } 00336 } 00337 00338 static void scanline_separate_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int chan) 00339 { 00340 int i; 00341 for (i=0; i < scanline_w; i++) 00342 rectf[i*4 + chan] = sbuf[i] / 65535.0; 00343 } 00344 00345 static void scanline_separate_32bit(float *rectf, float *fbuf, int scanline_w, int chan) 00346 { 00347 int i; 00348 for (i=0; i < scanline_w; i++) 00349 rectf[i*4 + chan] = fbuf[i]; 00350 } 00351 00352 00353 /* 00354 * Use the libTIFF scanline API to read a TIFF image. 00355 * This method is most flexible and can handle multiple different bit depths 00356 * and RGB channel orderings. 00357 */ 00358 static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul) 00359 { 00360 ImBuf *tmpibuf; 00361 int success= 0; 00362 short bitspersample, spp, config; 00363 size_t scanline; 00364 int ib_flag=0, row, chan; 00365 float *fbuf=NULL; 00366 unsigned short *sbuf=NULL; 00367 00368 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample); 00369 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp); /* number of 'channels' */ 00370 TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config); 00371 scanline = TIFFScanlineSize(image); 00372 00373 if (bitspersample == 32) { 00374 ib_flag = IB_rectfloat; 00375 fbuf = (float *)_TIFFmalloc(scanline); 00376 } else if (bitspersample == 16) { 00377 ib_flag = IB_rectfloat; 00378 sbuf = (unsigned short *)_TIFFmalloc(scanline); 00379 } else { 00380 ib_flag = IB_rect; 00381 } 00382 00383 tmpibuf= IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->depth, ib_flag); 00384 00385 /* simple RGBA image */ 00386 if (!(bitspersample == 32 || bitspersample == 16)) { 00387 success |= TIFFReadRGBAImage(image, ibuf->x, ibuf->y, tmpibuf->rect, 0); 00388 } 00389 /* contiguous channels: RGBRGBRGB */ 00390 else if (config == PLANARCONFIG_CONTIG) { 00391 for (row = 0; row < ibuf->y; row++) { 00392 int ib_offset = ibuf->x*ibuf->y*4 - ibuf->x*4 * (row+1); 00393 00394 if (bitspersample == 32) { 00395 success |= TIFFReadScanline(image, fbuf, row, 0); 00396 scanline_contig_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, spp); 00397 00398 } else if (bitspersample == 16) { 00399 success |= TIFFReadScanline(image, sbuf, row, 0); 00400 scanline_contig_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, spp); 00401 } 00402 } 00403 /* separate channels: RRRGGGBBB */ 00404 } else if (config == PLANARCONFIG_SEPARATE) { 00405 00406 /* imbufs always have 4 channels of data, so we iterate over all of them 00407 * but only fill in from the TIFF scanline where necessary. */ 00408 for (chan = 0; chan < 4; chan++) { 00409 for (row = 0; row < ibuf->y; row++) { 00410 int ib_offset = ibuf->x*ibuf->y*4 - ibuf->x*4 * (row+1); 00411 00412 if (bitspersample == 32) { 00413 if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */ 00414 memset(fbuf, 1.0, sizeof(fbuf)); 00415 else 00416 success |= TIFFReadScanline(image, fbuf, row, chan); 00417 scanline_separate_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, chan); 00418 00419 } else if (bitspersample == 16) { 00420 if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */ 00421 memset(sbuf, 65535, sizeof(sbuf)); 00422 else 00423 success |= TIFFReadScanline(image, sbuf, row, chan); 00424 scanline_separate_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, chan); 00425 00426 } 00427 } 00428 } 00429 } 00430 00431 if (bitspersample == 32) 00432 _TIFFfree(fbuf); 00433 else if (bitspersample == 16) 00434 _TIFFfree(sbuf); 00435 00436 if(success) { 00437 ibuf->profile = (bitspersample==32)?IB_PROFILE_LINEAR_RGB:IB_PROFILE_SRGB; 00438 00439 if(ENDIAN_ORDER == B_ENDIAN) 00440 IMB_convert_rgba_to_abgr(tmpibuf); 00441 if(premul) { 00442 IMB_premultiply_alpha(tmpibuf); 00443 ibuf->flags |= IB_premul; 00444 } 00445 00446 /* assign rect last */ 00447 if (tmpibuf->rect_float) 00448 ibuf->rect_float= tmpibuf->rect_float; 00449 else 00450 ibuf->rect= tmpibuf->rect; 00451 ibuf->mall |= ib_flag; 00452 ibuf->flags |= ib_flag; 00453 00454 tmpibuf->mall &= ~ib_flag; 00455 } 00456 00457 IMB_freeImBuf(tmpibuf); 00458 00459 return success; 00460 } 00461 00462 void imb_inittiff(void) 00463 { 00464 if (!(G.f & G_DEBUG)) 00465 TIFFSetErrorHandler(NULL); 00466 } 00467 00479 ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags) 00480 { 00481 TIFF *image = NULL; 00482 ImBuf *ibuf = NULL, *hbuf; 00483 ImbTIFFMemFile memFile; 00484 uint32 width, height; 00485 char *format = NULL; 00486 int level; 00487 short spp; 00488 int ib_depth; 00489 00490 /* check whether or not we have a TIFF file */ 00491 if(size < IMB_TIFF_NCB) { 00492 fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n"); 00493 return NULL; 00494 } 00495 if(imb_is_a_tiff(mem) == 0) 00496 return NULL; 00497 00498 image = imb_tiff_client_open(&memFile, mem, size); 00499 00500 if(image == NULL) { 00501 printf("imb_loadtiff: could not open TIFF IO layer.\n"); 00502 return NULL; 00503 } 00504 00505 /* allocate the image buffer */ 00506 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width); 00507 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height); 00508 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp); 00509 00510 ib_depth = (spp==3)?24:32; 00511 00512 ibuf = IMB_allocImBuf(width, height, ib_depth, 0); 00513 if(ibuf) { 00514 ibuf->ftype = TIF; 00515 } 00516 else { 00517 fprintf(stderr, 00518 "imb_loadtiff: could not allocate memory for TIFF " \ 00519 "image.\n"); 00520 TIFFClose(image); 00521 return NULL; 00522 } 00523 00524 /* if testing, we're done */ 00525 if(flags & IB_test) { 00526 TIFFClose(image); 00527 return ibuf; 00528 } 00529 00530 /* detect if we are reading a tiled/mipmapped texture, in that case 00531 we don't read pixels but leave it to the cache to load tiles */ 00532 if(flags & IB_tilecache) { 00533 format= NULL; 00534 TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format); 00535 00536 if(format && strcmp(format, "Plain Texture")==0 && TIFFIsTiled(image)) { 00537 int numlevel = TIFFNumberOfDirectories(image); 00538 00539 /* create empty mipmap levels in advance */ 00540 for(level=0; level<numlevel; level++) { 00541 if(!TIFFSetDirectory(image, level)) 00542 break; 00543 00544 if(level > 0) { 00545 width= (width > 1)? width/2: 1; 00546 height= (height > 1)? height/2: 1; 00547 00548 hbuf= IMB_allocImBuf(width, height, 32, 0); 00549 hbuf->miplevel= level; 00550 hbuf->ftype= ibuf->ftype; 00551 ibuf->mipmap[level-1] = hbuf; 00552 00553 if(flags & IB_premul) 00554 hbuf->flags |= IB_premul; 00555 } 00556 else 00557 hbuf= ibuf; 00558 00559 hbuf->flags |= IB_tilecache; 00560 00561 TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex); 00562 TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley); 00563 00564 hbuf->xtiles= ceil(hbuf->x/(float)hbuf->tilex); 00565 hbuf->ytiles= ceil(hbuf->y/(float)hbuf->tiley); 00566 00567 imb_addtilesImBuf(hbuf); 00568 00569 ibuf->miptot++; 00570 } 00571 } 00572 } 00573 00574 /* read pixels */ 00575 if(!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image, 0)) { 00576 fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n"); 00577 TIFFClose(image); 00578 return NULL; 00579 } 00580 00581 /* close the client layer interface to the in-memory file */ 00582 TIFFClose(image); 00583 00584 /* return successfully */ 00585 return ibuf; 00586 } 00587 00588 void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect) 00589 { 00590 TIFF *image = NULL; 00591 uint32 width, height; 00592 ImbTIFFMemFile memFile; 00593 00594 image = imb_tiff_client_open(&memFile, mem, size); 00595 00596 if(image == NULL) { 00597 printf("imb_loadtiff: could not open TIFF IO layer for loading mipmap level.\n"); 00598 return; 00599 } 00600 00601 if(TIFFSetDirectory(image, ibuf->miplevel)) { 00602 /* allocate the image buffer */ 00603 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width); 00604 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height); 00605 00606 if(width == ibuf->x && height == ibuf->y) { 00607 if(rect) { 00608 /* tiff pixels are bottom to top, tiles are top to bottom */ 00609 if(TIFFReadRGBATile(image, tx*ibuf->tilex, (ibuf->ytiles - 1 - ty)*ibuf->tiley, rect) == 1) { 00610 if(ibuf->tiley > ibuf->y) 00611 memmove(rect, rect+ibuf->tilex*(ibuf->tiley - ibuf->y), sizeof(int)*ibuf->tilex*ibuf->y); 00612 00613 if(ibuf->flags & IB_premul) 00614 IMB_premultiply_rect(rect, 32, ibuf->tilex, ibuf->tiley); 00615 } 00616 else 00617 printf("imb_loadtiff: failed to read tiff tile at mipmap level %d\n", ibuf->miplevel); 00618 } 00619 } 00620 else 00621 printf("imb_loadtiff: mipmap level %d has unexpected size %dx%d instead of %dx%d\n", ibuf->miplevel, width, height, ibuf->x, ibuf->y); 00622 } 00623 else 00624 printf("imb_loadtiff: could not find mipmap level %d\n", ibuf->miplevel); 00625 00626 /* close the client layer interface to the in-memory file */ 00627 TIFFClose(image); 00628 } 00629 00646 int imb_savetiff(ImBuf *ibuf, const char *name, int flags) 00647 { 00648 TIFF *image = NULL; 00649 uint16 samplesperpixel, bitspersample; 00650 size_t npixels; 00651 unsigned char *pixels = NULL; 00652 unsigned char *from = NULL, *to = NULL; 00653 unsigned short *pixels16 = NULL, *to16 = NULL; 00654 float *fromf = NULL; 00655 int x, y, from_i, to_i, i; 00656 int extraSampleTypes[1] = { EXTRASAMPLE_ASSOCALPHA }; 00657 00658 00659 /* check for a valid number of bytes per pixel. Like the PNG writer, 00660 * the TIFF writer supports 1, 3 or 4 bytes per pixel, corresponding 00661 * to gray, RGB, RGBA respectively. */ 00662 samplesperpixel = (uint16)((ibuf->depth + 7) >> 3); 00663 if((samplesperpixel > 4) || (samplesperpixel == 2)) { 00664 fprintf(stderr, 00665 "imb_savetiff: unsupported number of bytes per " 00666 "pixel: %d\n", samplesperpixel); 00667 return (0); 00668 } 00669 00670 if((ibuf->ftype & TIF_16BIT) && ibuf->rect_float) 00671 bitspersample = 16; 00672 else 00673 bitspersample = 8; 00674 00675 /* open TIFF file for writing */ 00676 if(flags & IB_mem) { 00677 /* bork at the creation of a TIFF in memory */ 00678 fprintf(stderr, 00679 "imb_savetiff: creation of in-memory TIFF files is " 00680 "not yet supported.\n"); 00681 return (0); 00682 } 00683 else { 00684 /* create image as a file */ 00685 image = TIFFOpen(name, "w"); 00686 } 00687 if(image == NULL) { 00688 fprintf(stderr, 00689 "imb_savetiff: could not open TIFF for writing.\n"); 00690 return (0); 00691 } 00692 00693 /* allocate array for pixel data */ 00694 npixels = ibuf->x * ibuf->y; 00695 if(bitspersample == 16) 00696 pixels16 = (unsigned short*)_TIFFmalloc(npixels * 00697 samplesperpixel * sizeof(unsigned short)); 00698 else 00699 pixels = (unsigned char*)_TIFFmalloc(npixels * 00700 samplesperpixel * sizeof(unsigned char)); 00701 00702 if(pixels == NULL && pixels16 == NULL) { 00703 fprintf(stderr, 00704 "imb_savetiff: could not allocate pixels array.\n"); 00705 TIFFClose(image); 00706 return (0); 00707 } 00708 00709 /* setup pointers */ 00710 if(bitspersample == 16) { 00711 fromf = ibuf->rect_float; 00712 to16 = pixels16; 00713 } 00714 else { 00715 from = (unsigned char*)ibuf->rect; 00716 to = pixels; 00717 } 00718 00719 /* setup samples per pixel */ 00720 TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample); 00721 TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel); 00722 00723 if(samplesperpixel == 4) { 00724 /* RGBA images */ 00725 TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1, 00726 extraSampleTypes); 00727 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, 00728 PHOTOMETRIC_RGB); 00729 } 00730 else if(samplesperpixel == 3) { 00731 /* RGB images */ 00732 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, 00733 PHOTOMETRIC_RGB); 00734 } 00735 else if(samplesperpixel == 1) { 00736 /* greyscale images, 1 channel */ 00737 TIFFSetField(image, TIFFTAG_PHOTOMETRIC, 00738 PHOTOMETRIC_MINISBLACK); 00739 } 00740 00741 /* copy pixel data. While copying, we flip the image vertically. */ 00742 for(x = 0; x < ibuf->x; x++) { 00743 for(y = 0; y < ibuf->y; y++) { 00744 from_i = 4*(y*ibuf->x+x); 00745 to_i = samplesperpixel*((ibuf->y-y-1)*ibuf->x+x); 00746 00747 if(pixels16) { 00748 /* convert from float source */ 00749 float rgb[3]; 00750 00751 if (ibuf->profile == IB_PROFILE_LINEAR_RGB) 00752 linearrgb_to_srgb_v3_v3(rgb, &fromf[from_i]); 00753 else 00754 copy_v3_v3(rgb, &fromf[from_i]); 00755 00756 to16[to_i+0] = FTOUSHORT(rgb[0]); 00757 to16[to_i+1] = FTOUSHORT(rgb[1]); 00758 to16[to_i+2] = FTOUSHORT(rgb[2]); 00759 to_i += 3; from_i+=3; 00760 00761 if (samplesperpixel == 4) { 00762 to16[to_i+3] = FTOUSHORT(fromf[from_i+3]); 00763 /*to_i++; from_i++;*/ /*unused, set on each loop */ 00764 } 00765 } 00766 else { 00767 for(i = 0; i < samplesperpixel; i++, to_i++, from_i++) 00768 to[to_i] = from[from_i]; 00769 } 00770 } 00771 } 00772 00773 /* write the actual TIFF file */ 00774 TIFFSetField(image, TIFFTAG_IMAGEWIDTH, ibuf->x); 00775 TIFFSetField(image, TIFFTAG_IMAGELENGTH, ibuf->y); 00776 TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, ibuf->y); 00777 TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); 00778 TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); 00779 TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 00780 TIFFSetField(image, TIFFTAG_XRESOLUTION, 150.0); 00781 TIFFSetField(image, TIFFTAG_YRESOLUTION, 150.0); 00782 TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); 00783 if(TIFFWriteEncodedStrip(image, 0, 00784 (bitspersample == 16)? (unsigned char*)pixels16: pixels, 00785 ibuf->x*ibuf->y*samplesperpixel*bitspersample/8) == -1) { 00786 fprintf(stderr, 00787 "imb_savetiff: Could not write encoded TIFF.\n"); 00788 TIFFClose(image); 00789 if(pixels) _TIFFfree(pixels); 00790 if(pixels16) _TIFFfree(pixels16); 00791 return (1); 00792 } 00793 00794 /* close the TIFF file */ 00795 TIFFClose(image); 00796 if(pixels) _TIFFfree(pixels); 00797 if(pixels16) _TIFFfree(pixels16); 00798 return (1); 00799 } 00800 00801 #endif /* WITH_TIFF */
1.7.3