int write_image (dt_imageio_tiff_t *d, const char *filename, const void *in_void, void *exif, int exif_len, int imgid) { // Fetch colorprofile into buffer if wanted uint8_t *profile = NULL; uint32_t profile_len = 0; int rc = 0; if(imgid > 0) { cmsHPROFILE out_profile = dt_colorspaces_create_output_profile(imgid); cmsSaveProfileToMem(out_profile, 0, &profile_len); if (profile_len > 0) { profile=malloc(profile_len); cmsSaveProfileToMem(out_profile, profile, &profile_len); } dt_colorspaces_cleanup_profile(out_profile); } // Create tiff image TIFF *tif=TIFFOpen(filename,"wb"); if(d->bpp == 8) TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); else TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); if(profile!=NULL) TIFFSetField(tif, TIFFTAG_ICCPROFILE, profile_len, profile); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, d->width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, d->height); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PREDICTOR, 1); // Reference www.awaresystems.be/imaging/tiff/tifftags/predictor.html TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, DT_TIFFIO_STRIPE); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(tif, TIFFTAG_XRESOLUTION, 300.0); TIFFSetField(tif, TIFFTAG_YRESOLUTION, 300.0); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, 9); const uint8_t *in8 =(const uint8_t *)in_void; const uint16_t *in16=(const uint16_t *)in_void; if(d->bpp == 16) { uint32_t rowsize=(d->width*3)*sizeof(uint16_t); uint32_t stripesize=rowsize*DT_TIFFIO_STRIPE; uint16_t *rowdata = (uint16_t *)malloc(stripesize); uint16_t *wdata = rowdata; uint32_t stripe=0; // uint32_t insize=((d->width*d->height)*3)*sizeof(uint16_t); // while(stripedata<(in8+insize)-(stripesize)) { // TIFFWriteEncodedStrip(tif,stripe++,stripedata,stripesize); // stripedata+=stripesize; // } for (int y = 0; y < d->height; y++) { for(int x=0; x<d->width; x++) for(int k=0; k<3; k++) { (wdata)[0] = in16[4*d->width*y + 4*x + k]; wdata++; } if((wdata-stripesize/sizeof(uint16_t))==rowdata) { TIFFWriteEncodedStrip(tif,stripe++,rowdata,rowsize*DT_TIFFIO_STRIPE); wdata=rowdata; } } if((wdata-stripesize/sizeof(uint16_t))!=rowdata) TIFFWriteEncodedStrip(tif,stripe,rowdata,(wdata-rowdata)*sizeof(uint16_t)); TIFFClose(tif); free(rowdata); } else { uint32_t rowsize=(d->width*3)*sizeof(uint8_t); uint32_t stripesize=rowsize*DT_TIFFIO_STRIPE; uint8_t *rowdata = (uint8_t *)malloc(stripesize); uint8_t *wdata = rowdata; uint32_t stripe=0; for (int y = 0; y < d->height; y++) { for(int x=0; x<d->width; x++) for(int k=0; k<3; k++) { (wdata)[0] = in8[4*d->width*y + 4*x + k]; wdata++; } if((wdata-stripesize)==rowdata) { TIFFWriteEncodedStrip(tif,stripe++,rowdata,rowsize*DT_TIFFIO_STRIPE); wdata=rowdata; } } if((wdata-stripesize)!=rowdata) TIFFWriteEncodedStrip(tif,stripe,rowdata,wdata-rowdata); TIFFClose(tif); free(rowdata); } if(exif) rc = dt_exif_write_blob(exif,exif_len,filename); free(profile); /* * Until we get symbolic error status codes, if rc is 1, return 0. */ return ((rc == 1) ? 0 : 1); }
int write_image(dt_imageio_module_data_t *d_tmp, const char *filename, const void *in_void, void *exif, int exif_len, int imgid) { const dt_imageio_tiff_t *d = (dt_imageio_tiff_t *)d_tmp; uint8_t *profile = NULL; uint32_t profile_len = 0; TIFF *tif = NULL; void *rowdata = NULL; int rc = 1; // default to error if(imgid > 0) { cmsHPROFILE out_profile = dt_colorspaces_create_output_profile(imgid); cmsSaveProfileToMem(out_profile, 0, &profile_len); if(profile_len > 0) { profile = malloc(profile_len); if(!profile) { rc = 1; goto exit; } cmsSaveProfileToMem(out_profile, profile, &profile_len); } dt_colorspaces_cleanup_profile(out_profile); } // Create little endian tiff image tif = TIFFOpen(filename, "wl"); if(!tif) { rc = 1; goto exit; } // http://partners.adobe.com/public/developer/en/tiff/TIFFphotoshop.pdf (dated 2002) // "A proprietary ZIP/Flate compression code (0x80b2) has been used by some" // "software vendors. This code should be considered obsolete. We recommend" // "that TIFF implentations recognize and read the obsolete code but only" // "write the official compression code (0x0008)." // http://www.awaresystems.be/imaging/tiff/tifftags/compression.html // http://www.awaresystems.be/imaging/tiff/tifftags/predictor.html if(d->compress == 1) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)1); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else if(d->compress == 2) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)2); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else if(d->compress == 3) { TIFFSetField(tif, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_ADOBE_DEFLATE); if(d->bpp == 32) TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)3); else TIFFSetField(tif, TIFFTAG_PREDICTOR, (uint16_t)2); TIFFSetField(tif, TIFFTAG_ZIPQUALITY, (uint16_t)9); } else // (d->compress == 0) { TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); } TIFFSetField(tif, TIFFTAG_FILLORDER, (uint16_t)FILLORDER_MSB2LSB); if(profile != NULL) { TIFFSetField(tif, TIFFTAG_ICCPROFILE, (uint32_t)profile_len, profile); } TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint16_t)3); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (uint16_t)d->bpp); TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, (uint16_t)(d->bpp == 32 ? SAMPLEFORMAT_IEEEFP : SAMPLEFORMAT_UINT)); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)d->width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)d->height); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, (uint16_t)PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, (uint16_t)PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, (uint32_t)1); TIFFSetField(tif, TIFFTAG_ORIENTATION, (uint16_t)ORIENTATION_TOPLEFT); int resolution = dt_conf_get_int("metadata/resolution"); if(resolution > 0) { TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)resolution); TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)resolution); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (uint16_t)RESUNIT_INCH); } const size_t rowsize = (d->width * 3) * d->bpp / 8; if((rowdata = malloc(rowsize)) == NULL) { rc = 1; goto exit; } if(d->bpp == 32) { for(int y = 0; y < d->height; y++) { float *in = (float *)in_void + (size_t)4 * y * d->width; float *out = (float *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(float)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } else if(d->bpp == 16) { for(int y = 0; y < d->height; y++) { uint16_t *in = (uint16_t *)in_void + (size_t)4 * y * d->width; uint16_t *out = (uint16_t *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(uint16_t)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } else { for(int y = 0; y < d->height; y++) { uint8_t *in = (uint8_t *)in_void + (size_t)4 * y * d->width; uint8_t *out = (uint8_t *)rowdata; for(int x = 0; x < d->width; x++, in += 4, out += 3) { memcpy(out, in, 3 * sizeof(uint8_t)); } if(TIFFWriteScanline(tif, rowdata, y, 0) == -1) { rc = 1; goto exit; } } } // success rc = 0; exit: // close the file before adding exif data if(tif) { TIFFClose(tif); tif = NULL; } if(!rc && exif) { rc = dt_exif_write_blob(exif, exif_len, filename); // Until we get symbolic error status codes, if rc is 1, return 0 rc = (rc == 1) ? 0 : 1; } free(profile); profile = NULL; free(rowdata); rowdata = NULL; return rc; }
int write_image (dt_imageio_j2k_t *j2k, const char *filename, const float *in, void *exif, int exif_len, int imgid) { opj_cparameters_t parameters; /* compression parameters */ float *rates = NULL; opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; int quality = CLAMP(j2k->quality, 1, 100); /* configure the event callbacks (not required) setting of each callback is optionnal */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set encoding parameters to default values */ opj_set_default_encoder_parameters(¶meters); /* compression ratio */ /* invert range, from 10-100, 100-1 * where jpeg see's 1 and highest quality (lossless) and 100 is very low quality*/ parameters.tcp_rates[0] = 100 - quality + 1; parameters.tcp_numlayers = 1; /* only one resolution */ parameters.cp_disto_alloc = 1; parameters.cp_rsiz = STD_RSIZ; parameters.cod_format = j2k->format; parameters.cp_cinema = j2k->preset; if(parameters.cp_cinema) { rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float)); for(int i=0; i< parameters.tcp_numlayers; i++) { rates[i] = parameters.tcp_rates[i]; } cinema_parameters(¶meters); } /* Create comment for codestream */ const char comment[] = "Created by "PACKAGE_STRING; parameters.cp_comment = g_strdup(comment); /*Converting the image to a format suitable for encoding*/ { int subsampling_dx = parameters.subsampling_dx; int subsampling_dy = parameters.subsampling_dy; int numcomps = 3; int prec = 12; //TODO: allow other bitdepths! int w = j2k->width, h = j2k->height; opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */ memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t)); for(int i = 0; i < numcomps; i++) { cmptparm[i].prec = prec; cmptparm[i].bpp = prec; cmptparm[i].sgnd = 0; cmptparm[i].dx = subsampling_dx; cmptparm[i].dy = subsampling_dy; cmptparm[i].w = w; cmptparm[i].h = h; } image = opj_image_create(numcomps, &cmptparm[0], CLRSPC_SRGB); if(!image) { fprintf(stderr, "Error: opj_image_create() failed\n"); return 1; } /* set image offset and reference grid */ image->x0 = parameters.image_offset_x0; image->y0 = parameters.image_offset_y0; image->x1 = parameters.image_offset_x0 + (w - 1) * subsampling_dx + 1; image->y1 = parameters.image_offset_y0 + (h - 1) * subsampling_dy + 1; switch(prec) { case 8: for(int i = 0; i < w * h; i++) { for(int k = 0; k < numcomps; k++) image->comps[k].data[i] = DOWNSAMPLE_FLOAT_TO_8BIT(in[i*4 + k]); } break; case 12: for(int i = 0; i < w * h; i++) { for(int k = 0; k < numcomps; k++) image->comps[k].data[i] = DOWNSAMPLE_FLOAT_TO_12BIT(in[i*4 + k]); } break; case 16: for(int i = 0; i < w * h; i++) { for(int k = 0; k < numcomps; k++) image->comps[k].data[i] = DOWNSAMPLE_FLOAT_TO_16BIT(in[i*4 + k]); } break; default: fprintf(stderr, "Error: this shouldn't happen, there is no bit depth of %d for jpeg 2000 images.\n", prec); return 1; } } /*Encoding image*/ /* Decide if MCT should be used */ parameters.tcp_mct = image->numcomps == 3 ? 1 : 0; if(parameters.cp_cinema) { cinema_setup_encoder(¶meters,image,rates); } /* encode the destination image */ /* ---------------------------- */ int rc = 1; OPJ_CODEC_FORMAT codec; if(parameters.cod_format == J2K_CFMT) /* J2K format output */ codec = CODEC_J2K; else codec = CODEC_JP2; int codestream_length; size_t res; opj_cio_t *cio = NULL; FILE *f = NULL; /* get a J2K/JP2 compressor handle */ opj_cinfo_t* cinfo = opj_create_compress(codec); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr); /* setup the encoder parameters using the current image and user parameters */ opj_setup_encoder(cinfo, ¶meters, image); /* open a byte stream for writing */ /* allocate memory for all tiles */ cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); /* encode the image */ if(!opj_encode(cinfo, cio, image, NULL)) { opj_cio_close(cio); fprintf(stderr, "failed to encode image\n"); return 1; } codestream_length = cio_tell(cio); /* write the buffer to disk */ f = fopen(filename, "wb"); if(!f) { fprintf(stderr, "failed to open %s for writing\n", filename); return 1; } res = fwrite(cio->buffer, 1, codestream_length, f); if(res < (size_t)codestream_length) /* FIXME */ { fprintf(stderr, "failed to write %d (%s)\n", codestream_length, filename); fclose(f); return 1; } fclose(f); /* close and free the byte stream */ opj_cio_close(cio); /* free remaining compression structures */ opj_destroy_compress(cinfo); /* add exif data blob. seems to not work for j2k files :( */ if(exif && j2k->format == JP2_CFMT) rc = dt_exif_write_blob(exif,exif_len,filename); /* free image data */ opj_image_destroy(image); /* free user parameters structure */ g_free(parameters.cp_comment); if(parameters.cp_matrice) free(parameters.cp_matrice); return ((rc == 1) ? 0 : 1); }
int write_image(dt_imageio_module_data_t *jpg_tmp, const char *filename, const void *in_tmp, dt_colorspaces_color_profile_type_t over_type, const char *over_filename, void *exif, int exif_len, int imgid, int num, int total, struct dt_dev_pixelpipe_t *pipe) { dt_imageio_jpeg_t *jpg = (dt_imageio_jpeg_t *)jpg_tmp; const uint8_t *in = (const uint8_t *)in_tmp; struct dt_imageio_jpeg_error_mgr jerr; jpg->cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = dt_imageio_jpeg_error_exit; if(setjmp(jerr.setjmp_buffer)) { jpeg_destroy_compress(&(jpg->cinfo)); return 1; } jpeg_create_compress(&(jpg->cinfo)); FILE *f = g_fopen(filename, "wb"); if(!f) return 1; jpeg_stdio_dest(&(jpg->cinfo), f); jpg->cinfo.image_width = jpg->global.width; jpg->cinfo.image_height = jpg->global.height; jpg->cinfo.input_components = 3; jpg->cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&(jpg->cinfo)); jpeg_set_quality(&(jpg->cinfo), jpg->quality, TRUE); if(jpg->quality > 90) jpg->cinfo.comp_info[0].v_samp_factor = 1; if(jpg->quality > 92) jpg->cinfo.comp_info[0].h_samp_factor = 1; if(jpg->quality > 95) jpg->cinfo.dct_method = JDCT_FLOAT; if(jpg->quality < 50) jpg->cinfo.dct_method = JDCT_IFAST; if(jpg->quality < 80) jpg->cinfo.smoothing_factor = 20; if(jpg->quality < 60) jpg->cinfo.smoothing_factor = 40; if(jpg->quality < 40) jpg->cinfo.smoothing_factor = 60; jpg->cinfo.optimize_coding = 1; // according to specs density_unit = 0, X_density = 1, Y_density = 1 should be fine and valid since it // describes an image with unknown unit and square pixels. // however, some applications (like the Telekom cloud thingy) seem to be confused by that, so let's set // these calues to the same as stored in exiv :/ const int resolution = dt_conf_get_int("metadata/resolution"); if(resolution > 0) { jpg->cinfo.density_unit = 1; jpg->cinfo.X_density = resolution; jpg->cinfo.Y_density = resolution; } else { jpg->cinfo.density_unit = 0; jpg->cinfo.X_density = 1; jpg->cinfo.Y_density = 1; } jpeg_start_compress(&(jpg->cinfo), TRUE); if(imgid > 0) { cmsHPROFILE out_profile = dt_colorspaces_get_output_profile(imgid, over_type, over_filename)->profile; uint32_t len = 0; cmsSaveProfileToMem(out_profile, 0, &len); if(len > 0) { unsigned char *buf = malloc(len * sizeof(unsigned char)); cmsSaveProfileToMem(out_profile, buf, &len); write_icc_profile(&(jpg->cinfo), buf, len); free(buf); } } uint8_t *row = malloc((size_t)3 * jpg->global.width * sizeof(uint8_t)); const uint8_t *buf; while(jpg->cinfo.next_scanline < jpg->cinfo.image_height) { JSAMPROW tmp[1]; buf = in + (size_t)jpg->cinfo.next_scanline * jpg->cinfo.image_width * 4; for(int i = 0; i < jpg->global.width; i++) for(int k = 0; k < 3; k++) row[3 * i + k] = buf[4 * i + k]; tmp[0] = row; jpeg_write_scanlines(&(jpg->cinfo), tmp, 1); } jpeg_finish_compress(&(jpg->cinfo)); free(row); jpeg_destroy_compress(&(jpg->cinfo)); fclose(f); dt_exif_write_blob(exif, exif_len, filename, 1); return 0; }