230 const cmsHTRANSFORM xform = cmsCreateTransform(
Lab, TYPE_LabA_FLT, output_profile, TYPE_RGBA_FLT, INTENT_PERCEPTUAL, 0);
234 uint8_t *in = ((uint8_t *)
t->buf);
235 float *output = ((
float *)
t->mipbuf) + (size_t)4 *
row *
t->width;
239 if(TIFFReadScanline(
t->tiff, in,
row, 0) == -1)
goto failed;
241 for(uint32_t
i = 0;
i <
t->width;
i++, in +=
t->spp,
out += 4)
243 out[0] = ((float)in[0]) * (100.0f/255.0f);
251 if(photometric == PHOTOMETRIC_CIELAB)
253 out[1] = ((float)((int8_t)in[1]));
254 out[2] = ((float)((int8_t)in[2]));
258 out[1] = ((float)(in[1])) - 128.0f;
259 out[2] = ((float)(in[2])) - 128.0f;
266 cmsDoTransform(xform, output, output,
t->width);
269 cmsDeleteTransform(xform);
274 cmsDeleteTransform(xform);
283 const cmsHTRANSFORM xform = cmsCreateTransform(
Lab, TYPE_LabA_FLT, output_profile, TYPE_RGBA_FLT, INTENT_PERCEPTUAL, 0);
284 const float range = (photometric == PHOTOMETRIC_CIELAB) ? 65535.0f : 65280.0f;
288 uint16_t *in = ((uint16_t *)
t->buf);
289 float *output = ((
float *)
t->mipbuf) + (size_t)4 *
row *
t->width;
293 if(TIFFReadScanline(
t->tiff, in,
row, 0) == -1)
goto failed;
295 for(uint32_t
i = 0;
i <
t->width;
i++, in +=
t->spp,
out += 4)
297 out[0] = ((float)in[0]) * (100.0f/range);
305 if(photometric == PHOTOMETRIC_CIELAB)
307 out[1] = ((float)((int16_t)in[1])) / 256.0f;
308 out[2] = ((float)((int16_t)in[2])) / 256.0f;
312 out[1] = (((float)(in[1])) - 32768.0f) / 256.0f;
313 out[2] = (((float)(in[2])) - 32768.0f) / 256.0f;
320 cmsDoTransform(xform, output, output,
t->width);
323 cmsDeleteTransform(xform);
328 cmsDeleteTransform(xform);
360 const char *ext = filename + strlen(filename);
361 while(*ext !=
'.' && ext > filename) ext--;
362 if(strncmp(ext,
".tif", 4) && strncmp(ext,
".TIF", 4) && strncmp(ext,
".tiff", 5)
363 && strncmp(ext,
".TIFF", 5))
369 uint16_t photometric;
375 wchar_t *wfilename = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL);
376 t.tiff = TIFFOpenW(wfilename,
"rb");
379 t.tiff = TIFFOpen(filename,
"rb");
384 TIFFGetField(
t.tiff, TIFFTAG_IMAGEWIDTH, &
t.width);
385 TIFFGetField(
t.tiff, TIFFTAG_IMAGELENGTH, &
t.height);
386 TIFFGetField(
t.tiff, TIFFTAG_BITSPERSAMPLE, &
t.bpp);
387 TIFFGetField(
t.tiff, TIFFTAG_SAMPLESPERPIXEL, &
t.spp);
388 TIFFGetFieldDefaulted(
t.tiff, TIFFTAG_SAMPLEFORMAT, &
t.sampleformat);
389 TIFFGetField(
t.tiff, TIFFTAG_PLANARCONFIG, &config);
390 TIFFGetField(
t.tiff, TIFFTAG_PHOTOMETRIC, &photometric);
391 TIFFGetField(
t.tiff, TIFFTAG_INKSET, &inkset);
393 if(inkset == INKSET_CMYK || inkset == INKSET_MULTIINK)
395 fprintf(stderr,
"[tiff_open] error: CMYK (or multiink) TIFFs are not supported.\n");
402 t.scanlinesize = TIFFScanlineSize(
t.tiff);
407 if(
t.bpp != 8 &&
t.bpp != 16 &&
t.bpp != 32)
414 if(
t.spp != 1 &&
t.spp != 3 &&
t.spp != 4)
421 if(
t.spp > 1 && config != PLANARCONFIG_CONTIG)
423 fprintf(stderr,
"[tiff_open] error: PlanarConfiguration other than chunky is not supported.\n");
429 t.image->width =
t.width;
430 t.image->height =
t.height;
432 t.image->dsc.channels = 4;
434 t.image->dsc.bpp = 4 *
sizeof(float);
436 t.image->dsc.filters = 0u;
439 if(
t.sampleformat == SAMPLEFORMAT_IEEEFP)
442 t.image->flags &= ~DT_IMAGE_LDR;
449 t.image->flags &= ~DT_IMAGE_HDR;
452 if(photometric == PHOTOMETRIC_CIELAB || photometric == PHOTOMETRIC_ICCLAB)
455 t.image->flags &= ~DT_IMAGE_RAW;
456 t.image->flags &= ~DT_IMAGE_S_RAW;
468 fprintf(stderr,
"[tiff_open] error: could not alloc full buffer for image `%s'\n",
t.image->filename);
473 if((
t.buf = _TIFFmalloc(
t.scanlinesize)) == NULL)
481 if((photometric == PHOTOMETRIC_CIELAB || photometric == PHOTOMETRIC_ICCLAB) &&
t.bpp == 8 &&
t.sampleformat == SAMPLEFORMAT_UINT)
486 else if((photometric == PHOTOMETRIC_CIELAB || photometric == PHOTOMETRIC_ICCLAB) &&
t.bpp == 16 &&
t.sampleformat == SAMPLEFORMAT_UINT)
491 else if(
t.bpp == 8 &&
t.sampleformat == SAMPLEFORMAT_UINT)
493 else if(
t.bpp == 16 &&
t.sampleformat == SAMPLEFORMAT_UINT)
495 else if(
t.bpp == 16 &&
t.sampleformat == SAMPLEFORMAT_IEEEFP)
497 else if(
t.bpp == 32 &&
t.sampleformat == SAMPLEFORMAT_IEEEFP)
501 fprintf(stderr,
"[tiff_open] error: not a supported tiff image format.\n");
519 uint32_t profile_len = 0;
520 uint8_t *profile = NULL;
521 uint16_t photometric;
523 if(!(filename && *filename &&
out))
return 0;
526 wchar_t *wfilename = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL);
527 tiff = TIFFOpenW(wfilename,
"rb");
530 tiff = TIFFOpen(filename,
"rb");
535 TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
537 if(photometric == PHOTOMETRIC_CIELAB || photometric == PHOTOMETRIC_ICCLAB)
541 cmsSaveProfileToMem(profile, 0, &profile_len);
544 *
out = (uint8_t *)g_malloc(profile_len);
545 cmsSaveProfileToMem(profile, *
out, &profile_len);
548 else if(TIFFGetField(tiff, TIFFTAG_ICCPROFILE, &profile_len, &profile))
552 *
out = (uint8_t *)g_malloc(profile_len);
553 memcpy(*
out, profile, profile_len);