91 const gboolean export_masks)
95 uint8_t *profile = NULL;
96 uint32_t profile_len = 0;
100 void *rowdata = NULL;
102 gboolean free_mask =
FALSE;
103 float *raster_mask = NULL;
105 wchar_t *wfilename = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL);
112 cmsSaveProfileToMem(out_profile, 0, &profile_len);
115 profile = malloc(profile_len);
121 cmsSaveProfileToMem(out_profile, profile, &profile_len);
125 uint16_t n_pages = 1;
127 if(export_masks && pipe)
129 for(GList *iter = pipe->
nodes; iter; iter = g_list_next(iter))
133 n_pages += g_hash_table_size(piece->module->raster_mask.source.masks);
139 tif = TIFFOpenW(wfilename,
"wl");
141 tif = TIFFOpen(filename,
"wl");
152 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
153 TIFFSetField(tif, TIFFTAG_PAGENAME, _(
"image"));
154 TIFFSetField(tif, TIFFTAG_PAGENUMBER, 0, n_pages);
157 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
159 TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, filename);
170 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
171 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_NONE);
172 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
174 else if(
d->compress == 2)
176 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
178 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
180 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
181 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
186 TIFFSetField(tif, TIFFTAG_ICCPROFILE, (uint32_t)profile_len, profile);
202 if((
d->global.height > 4) && (
d->global.width > 4) && shortmode)
207 for(
int y = 1; y <
d->global.height-1; y++)
209 float *in = (
float *)in_void + (
size_t)4 * y *
d->global.width;
210 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
212 if((fabsf(fmaxf(in[0], 0.001f) / fmaxf(in[1], 0.001f)) > 1.01f) ||
213 (fabsf(fmaxf(in[0], 0.001f) / fmaxf(in[2], 0.001f)) > 1.01f) ||
214 (fabsf(fmaxf(in[1], 0.001f) / fmaxf(in[2], 0.001f)) > 1.01f))
222 else if(
d->bpp == 16)
224 for(
int y = 1; y <
d->global.height-1; y++)
226 uint16_t *in = (uint16_t *)in_void + (
size_t)4 * y *
d->global.width;
227 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
229 if((abs(in[0] - in[1]) > 100) ||
230 (abs(in[0] - in[2]) > 100) ||
231 (abs(in[1] - in[2]) > 100))
241 for(
int y = 1; y <
d->global.height-1; y++)
243 uint8_t *in = (uint8_t *)in_void + (
size_t)4 * y *
d->global.width;
244 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
246 if((abs(in[0] - in[1]) > 5) ||
247 (abs(in[0] - in[2]) > 5) ||
248 (abs(in[1] - in[2]) > 5))
262 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, layers);
263 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)
d->bpp);
264 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (
d->bpp == 32) ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT);
265 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)
d->global.width);
266 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)
d->global.height);
268 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
270 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
272 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
273 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
274 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
277 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (
float)resolution);
278 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (
float)resolution);
279 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
281 const size_t rowsize = (
d->global.width * layers) *
d->bpp / 8;
282 if((rowdata = malloc(rowsize)) == NULL)
290 for(
int y = 0; y <
d->global.height; y++)
292 float *in = (
float *)in_void + (
size_t)4 * y *
d->global.width;
293 float *
out = (
float *)rowdata;
295 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
297 memcpy(
out, in,
sizeof(
float) * layers);
300 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
307 else if(
d->bpp == 16)
309 for(
int y = 0; y <
d->global.height; y++)
311 uint16_t *in = (uint16_t *)in_void + (
size_t)4 * y *
d->global.width;
312 uint16_t *
out = (uint16_t *)rowdata;
314 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
316 memcpy(
out, in,
sizeof(uint16_t) * layers);
319 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
328 for(
int y = 0; y <
d->global.height; y++)
330 uint8_t *in = (uint8_t *)in_void + (
size_t)4 * y *
d->global.width;
331 uint8_t *
out = (uint8_t *)rowdata;
333 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
335 memcpy(
out, in,
sizeof(uint8_t) * layers);
338 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
358 rc = (rc == 1) ? 0 : 1;
363 if(rc == 0 && n_pages > 1)
366 tif = TIFFOpenW(wfilename,
"al");
368 tif = TIFFOpen(filename,
"al");
378 float missing_raster_mask[8 * 8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
379 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
380 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0,
381 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
382 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
383 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
384 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
385 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
386 static const size_t missing_raster_mask_w = 8, missing_raster_mask_h = 8;
388 for(GList *iter = pipe->
nodes; iter; iter = g_list_next(iter))
392 GHashTableIter rm_iter;
397 g_hash_table_iter_init(&rm_iter, piece->module->raster_mask.source.masks);
398 while(g_hash_table_iter_next(&rm_iter, &
key, &
value))
405 size_t w =
d->global.width, h =
d->global.height;
409 w = missing_raster_mask_w;
410 h = missing_raster_mask_h;
411 raster_mask = missing_raster_mask;
415 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
416 TIFFSetField(tif, TIFFTAG_PAGENUMBER, page, n_pages);
418 const char *pagename = g_hash_table_lookup(piece->module->raster_mask.source.masks,
key);
420 TIFFSetField(tif, TIFFTAG_PAGENAME, pagename);
422 TIFFSetField(tif, TIFFTAG_PAGENAME, piece->module->name());
426 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
427 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_NONE);
428 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
430 else if(
d->compress == 2)
432 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
434 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
436 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
437 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
440 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (
float)resolution);
441 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (
float)resolution);
442 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
444 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)w);
445 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)h);
446 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
447 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
449#ifdef MASKS_USE_SAME_FORMAT
450 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, layers);
451 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)
d->bpp);
452 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (
d->bpp == 32) ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT);
454 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
456 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
457 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
459 if(w !=
d->global.width)
462 const size_t _rowsize = (w * layers) *
d->bpp / 8;
463 rowdata = malloc(_rowsize);
468 for(
int y = 0; y < h; y++)
470 const float *in = raster_mask + (size_t)y * w;
471 float *
out = (
float *)rowdata;
473 for(
int x = 0;
x < w;
x++,
out += layers)
475 for(
int c = 0; c < layers; c++)
479 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
486 else if(
d->bpp == 16)
488 for(
int y = 0; y < h; y++)
490 const float *in = raster_mask + (size_t)y * w;
491 uint16_t *
out = (uint16_t *)rowdata;
493 for(
int x = 0;
x < w;
x++,
out += layers)
495 for(
int c = 0; c < layers; c++)
496 out[c] =
CLIP(in[
x]) * 65535.0f + 0.5f;
499 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
508 for(
int y = 0; y < h; y++)
510 const float *in = raster_mask + (size_t)y * w;
511 uint8_t *
out = (uint8_t *)rowdata;
513 for(
int x = 0;
x < w;
x++,
out += layers)
515 for(
int c = 0; c < layers; c++)
516 out[c] =
CLIP(in[
x]) * 255.0f + 0.5f;
519 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
527 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
528 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
529 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
531 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
532 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
533 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
535 for(
int y = 0; y < h; y++)
537 const float *in = raster_mask + (size_t)y * w;
538 if(TIFFWriteScanline(tif, (
void *)in, y, 0) == -1)
550 TIFFWriteDirectory(tif);