87 const gboolean export_masks)
91 uint8_t *profile = NULL;
92 uint32_t profile_len = 0;
98 gboolean free_mask =
FALSE;
99 float *raster_mask = NULL;
101 wchar_t *wfilename = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL);
108 cmsSaveProfileToMem(out_profile, 0, &profile_len);
111 profile = malloc(profile_len);
117 cmsSaveProfileToMem(out_profile, profile, &profile_len);
121 uint16_t n_pages = 1;
123 if(export_masks && pipe)
125 for(GList *iter = pipe->
nodes; iter; iter = g_list_next(iter))
129 n_pages += g_hash_table_size(piece->module->raster_mask.source.masks);
135 tif = TIFFOpenW(wfilename,
"wl");
137 tif = TIFFOpen(filename,
"wl");
148 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
149 TIFFSetField(tif, TIFFTAG_PAGENAME, _(
"image"));
150 TIFFSetField(tif, TIFFTAG_PAGENUMBER, 0, n_pages);
153 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
155 TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, filename);
166 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
167 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_NONE);
168 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
170 else if(
d->compress == 2)
172 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
174 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
176 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
177 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
182 TIFFSetField(tif, TIFFTAG_ICCPROFILE, (uint32_t)profile_len, profile);
198 if((
d->global.height > 4) && (
d->global.width > 4) && shortmode)
203 for(
int y = 1; y <
d->global.height-1; y++)
205 float *in = (
float *)in_void + (
size_t)4 * y *
d->global.width;
206 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
208 if((fabsf(fmaxf(in[0], 0.001f) / fmaxf(in[1], 0.001f)) > 1.01f) ||
209 (fabsf(fmaxf(in[0], 0.001f) / fmaxf(in[2], 0.001f)) > 1.01f) ||
210 (fabsf(fmaxf(in[1], 0.001f) / fmaxf(in[2], 0.001f)) > 1.01f))
218 else if(
d->bpp == 16)
220 for(
int y = 1; y <
d->global.height-1; y++)
222 uint16_t *in = (uint16_t *)in_void + (
size_t)4 * y *
d->global.width;
223 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
225 if((abs(in[0] - in[1]) > 100) ||
226 (abs(in[0] - in[2]) > 100) ||
227 (abs(in[1] - in[2]) > 100))
237 for(
int y = 1; y <
d->global.height-1; y++)
239 uint8_t *in = (uint8_t *)in_void + (
size_t)4 * y *
d->global.width;
240 for(
int x = 1;
x <
d->global.width-1;
x++, in += 4)
242 if((abs(in[0] - in[1]) > 5) ||
243 (abs(in[0] - in[2]) > 5) ||
244 (abs(in[1] - in[2]) > 5))
258 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, layers);
259 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)
d->bpp);
260 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (
d->bpp == 32) ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT);
261 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)
d->global.width);
262 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)
d->global.height);
264 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
266 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
268 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
269 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
270 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
273 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (
float)resolution);
274 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (
float)resolution);
275 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
277 const size_t rowsize = (
d->global.width * layers) *
d->bpp / 8;
278 if((rowdata = malloc(rowsize)) == NULL)
286 for(
int y = 0; y <
d->global.height; y++)
288 float *in = (
float *)in_void + (
size_t)4 * y *
d->global.width;
289 float *
out = (
float *)rowdata;
291 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
293 memcpy(
out, in,
sizeof(
float) * layers);
296 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
303 else if(
d->bpp == 16)
305 for(
int y = 0; y <
d->global.height; y++)
307 uint16_t *in = (uint16_t *)in_void + (
size_t)4 * y *
d->global.width;
308 uint16_t *
out = (uint16_t *)rowdata;
310 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
312 memcpy(
out, in,
sizeof(uint16_t) * layers);
315 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
324 for(
int y = 0; y <
d->global.height; y++)
326 uint8_t *in = (uint8_t *)in_void + (
size_t)4 * y *
d->global.width;
327 uint8_t *
out = (uint8_t *)rowdata;
329 for(
int x = 0;
x <
d->global.width;
x++, in += 4,
out += layers)
331 memcpy(
out, in,
sizeof(uint8_t) * layers);
334 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
354 rc = (rc == 1) ? 0 : 1;
359 if(rc == 0 && n_pages > 1)
362 tif = TIFFOpenW(wfilename,
"al");
364 tif = TIFFOpen(filename,
"al");
374 float missing_raster_mask[8 * 8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
375 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
376 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0,
377 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
378 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
379 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
380 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
381 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
382 static const size_t missing_raster_mask_w = 8, missing_raster_mask_h = 8;
384 for(GList *iter = pipe->
nodes; iter; iter = g_list_next(iter))
388 GHashTableIter rm_iter;
393 g_hash_table_iter_init(&rm_iter, piece->module->raster_mask.source.masks);
394 while(g_hash_table_iter_next(&rm_iter, &
key, &
value))
401 size_t w =
d->global.width, h =
d->global.height;
405 w = missing_raster_mask_w;
406 h = missing_raster_mask_h;
407 raster_mask = missing_raster_mask;
411 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
412 TIFFSetField(tif, TIFFTAG_PAGENUMBER, page, n_pages);
414 const char *pagename = g_hash_table_lookup(piece->module->raster_mask.source.masks,
key);
416 TIFFSetField(tif, TIFFTAG_PAGENAME, pagename);
418 TIFFSetField(tif, TIFFTAG_PAGENAME, piece->module->name());
422 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
423 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_NONE);
424 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
426 else if(
d->compress == 2)
428 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
430 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
432 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
433 TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)
d->compresslevel);
436 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (
float)resolution);
437 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (
float)resolution);
438 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
440 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)w);
441 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)h);
442 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
443 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
445#ifdef MASKS_USE_SAME_FORMAT
446 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, layers);
447 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)
d->bpp);
448 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (
d->bpp == 32) ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT);
450 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
452 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
453 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
455 if(w !=
d->global.width)
458 const size_t _rowsize = (w * layers) *
d->bpp / 8;
459 rowdata = malloc(_rowsize);
464 for(
int y = 0; y < h; y++)
466 const float *in = raster_mask + (size_t)y * w;
467 float *
out = (
float *)rowdata;
469 for(
int x = 0;
x < w;
x++,
out += layers)
471 for(
int c = 0; c < layers; c++)
475 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
482 else if(
d->bpp == 16)
484 for(
int y = 0; y < h; y++)
486 const float *in = raster_mask + (size_t)y * w;
487 uint16_t *
out = (uint16_t *)rowdata;
489 for(
int x = 0;
x < w;
x++,
out += layers)
491 for(
int c = 0; c < layers; c++)
492 out[c] =
CLIP(in[
x]) * 65535.0f + 0.5f;
495 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
504 for(
int y = 0; y < h; y++)
506 const float *in = raster_mask + (size_t)y * w;
507 uint8_t *
out = (uint8_t *)rowdata;
509 for(
int x = 0;
x < w;
x++,
out += layers)
511 for(
int c = 0; c < layers; c++)
512 out[c] =
CLIP(in[
x]) * 255.0f + 0.5f;
515 if(TIFFWriteScanline(tif, rowdata, y, 0) == -1)
523 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
524 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
525 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
527 TIFFSetField(tif, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
528 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
529 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
531 for(
int y = 0; y < h; y++)
533 const float *in = raster_mask + (size_t)y * w;
534 if(TIFFWriteScanline(tif, (
void *)in, y, 0) == -1)
546 TIFFWriteDirectory(tif);