// Rearrange pixel type to build output descriptor static cmsUInt32Number ComputeOutputFormatDescriptor(cmsUInt32Number dwInput, int OutColorSpace) { int IsPlanar = T_PLANAR(dwInput); int Channels = 0; int Flavor = 0; switch (OutColorSpace) { case PT_GRAY: Channels = 1; break; case PT_RGB: case PT_CMY: case PT_Lab: case PT_YUV: case PT_YCbCr: Channels = 3; break; case PT_CMYK: if (Compressor.write_Adobe_marker) // Adobe keeps CMYK inverted, so change flavor to chocolate Flavor = 1; Channels = 4; break; default: FatalError("Unsupported output color space"); } return (COLORSPACE_SH(OutColorSpace)|PLANAR_SH(IsPlanar)|CHANNELS_SH(Channels)|BYTES_SH(1)|FLAVOR_SH(Flavor)); }
static cmsUInt32Number ComputeFormatDescriptor (int OutColorSpace, int bps) { int IsPlanar = 0; int Channels = 3; int IsFlt = 0; return (FLOAT_SH(IsFlt)|COLORSPACE_SH(OutColorSpace)|PLANAR_SH(IsPlanar)|CHANNELS_SH(Channels)|BYTES_SH(bps)); }
/* Transform an entire buffer */ void gscms_transform_color_buffer(gsicc_link_t *icclink, gsicc_bufferdesc_t *input_buff_desc, gsicc_bufferdesc_t *output_buff_desc, void *inputbuffer, void *outputbuffer) { cmsHTRANSFORM hTransform = (cmsHTRANSFORM) icclink->link_handle; DWORD dwInputFormat,dwOutputFormat,curr_input,curr_output; int planar,numbytes,big_endian,hasalpha,k; unsigned char *inputpos, *outputpos; int numchannels; #if DUMP_CMS_BUFFER FILE *fid_in, *fid_out; #endif /* Although little CMS does make assumptions about data types in its transformations you can change it after the fact. */ /* Set us to the proper output type */ /* Note, we could speed this up by passing back the encoded data type to the caller so that we could avoid having to go through this computation each time if they are doing multiple calls to this operation */ _LPcmsTRANSFORM p = (_LPcmsTRANSFORM) (LPSTR) hTransform; curr_input = p->InputFormat; curr_output = p->OutputFormat; /* Color space MUST be the same */ dwInputFormat = COLORSPACE_SH(T_COLORSPACE(curr_input)); dwOutputFormat = COLORSPACE_SH(T_COLORSPACE(curr_output)); /* Now set if we have planar, num bytes, endian case, and alpha data to skip */ /* Planar -- pdf14 case for example */ planar = input_buff_desc->is_planar; dwInputFormat = dwInputFormat | PLANAR_SH(planar); planar = output_buff_desc->is_planar; dwOutputFormat = dwOutputFormat | PLANAR_SH(planar); /* 8 or 16 byte input and output */ numbytes = input_buff_desc->bytes_per_chan; if (numbytes>2) numbytes = 0; /* littleCMS encodes float with 0 ToDO. */ dwInputFormat = dwInputFormat | BYTES_SH(numbytes); numbytes = output_buff_desc->bytes_per_chan; if (numbytes>2) numbytes = 0; dwOutputFormat = dwOutputFormat | BYTES_SH(numbytes); /* endian */ big_endian = !input_buff_desc->little_endian; dwInputFormat = dwInputFormat | ENDIAN16_SH(big_endian); big_endian = !output_buff_desc->little_endian; dwOutputFormat = dwOutputFormat | ENDIAN16_SH(big_endian); /* number of channels */ numchannels = input_buff_desc->num_chan; dwInputFormat = dwInputFormat | CHANNELS_SH(numchannels); numchannels = output_buff_desc->num_chan; dwOutputFormat = dwOutputFormat | CHANNELS_SH(numchannels); /* alpha, which is passed through unmolested */ /* ToDo: Right now we always must have alpha last */ /* This is really only going to be an issue when we have interleaved alpha data */ hasalpha = input_buff_desc->has_alpha; dwInputFormat = dwInputFormat | EXTRA_SH(hasalpha); dwOutputFormat = dwOutputFormat | EXTRA_SH(hasalpha); /* Change the formaters */ cmsChangeBuffersFormat(hTransform,dwInputFormat,dwOutputFormat); /* littleCMS knows nothing about word boundarys. As such, we need to do this row by row adjusting for our stride. Output buffer must already be allocated. ToDo: Check issues with plane and row stride and word boundry */ inputpos = (unsigned char *) inputbuffer; outputpos = (unsigned char *) outputbuffer; if(input_buff_desc->is_planar){ /* Do entire buffer. Care must be taken here with respect to row stride, word boundry and number of source versus output channels. We may need to take a closer look at this. */ cmsDoTransform(hTransform,inputpos,outputpos, input_buff_desc->plane_stride); } else { /* Do row by row. */ for(k = 0; k < input_buff_desc->num_rows ; k++){ cmsDoTransform(hTransform,inputpos,outputpos, input_buff_desc->pixels_per_row); inputpos += input_buff_desc->row_stride; outputpos += output_buff_desc->row_stride; } } #if DUMP_CMS_BUFFER fid_in = fopen("CM_Input.raw","ab"); fid_out = fopen("CM_Output.raw","ab"); fwrite((unsigned char*) inputbuffer,sizeof(unsigned char), input_buff_desc->row_stride,fid_in); fwrite((unsigned char*) outputbuffer,sizeof(unsigned char), output_buff_desc->row_stride,fid_out); fclose(fid_in); fclose(fid_out); #endif }
static DWORD ComputeOutputFormatDescriptor(DWORD dwInput, int OutColorSpace, int bps) { int IsPlanar = T_PLANAR(dwInput); int Channels = 0; switch (OutColorSpace) { case PT_GRAY: Channels = 1; break; case PT_RGB: case PT_CMY: case PT_Lab: case PT_YUV: case PT_YCbCr: Channels = 3; break; case PT_CMYK: Channels = 4; break; case PT_HiFi: Channels = 6; break; case PT_HiFi7: Channels = 7; break; case PT_HiFi8: Channels = 8; break; case PT_HiFi9: Channels = 9; break; case PT_HiFi10: Channels = 10; break; case PT_HiFi11: Channels = 11; break; case PT_HiFi12: Channels = 12; break; case PT_HiFi13: Channels = 13; break; case PT_HiFi14: Channels = 14; break; case PT_HiFi15: Channels = 15; break; default: FatalError("Unsupported output color space"); } return (COLORSPACE_SH(OutColorSpace)|PLANAR_SH(IsPlanar)|CHANNELS_SH(Channels)|BYTES_SH(bps)); }
static DWORD GetInputPixelType(TIFF *Bank) { uint16 Photometric, bps, spp, extra, PlanarConfig, *info; uint16 Compression, reverse = 0; int ColorChannels, IsPlanar = 0, pt = 0; TIFFGetField(Bank, TIFFTAG_PHOTOMETRIC, &Photometric); TIFFGetFieldDefaulted(Bank, TIFFTAG_BITSPERSAMPLE, &bps); if (bps == 1) FatalError("Sorry, bilevel TIFFs has nothig to do with ICC profiles"); if (bps != 8 && bps != 16) FatalError("Sorry, 8 or 16 bits per sample only"); TIFFGetFieldDefaulted(Bank, TIFFTAG_SAMPLESPERPIXEL, &spp); TIFFGetFieldDefaulted(Bank, TIFFTAG_PLANARCONFIG, &PlanarConfig); switch (PlanarConfig) { case PLANARCONFIG_CONTIG: IsPlanar = 0; break; case PLANARCONFIG_SEPARATE: IsPlanar = 1; break; default: FatalError("Unsupported planar configuration (=%d) ", (int) PlanarConfig); } // If Samples per pixel == 1, PlanarConfiguration is irrelevant and need // not to be included. if (spp == 1) IsPlanar = 0; // Any alpha? TIFFGetFieldDefaulted(Bank, TIFFTAG_EXTRASAMPLES, &extra, &info); // Read alpha channels as colorant if (StoreAsAlpha) { ColorChannels = spp; extra = 0; } else ColorChannels = spp - extra; switch (Photometric) { case PHOTOMETRIC_MINISWHITE: reverse = 1; case PHOTOMETRIC_MINISBLACK: pt = PT_GRAY; break; case PHOTOMETRIC_RGB: pt = PT_RGB; break; case PHOTOMETRIC_PALETTE: FatalError("Sorry, palette images not supported (at least on this version)"); case PHOTOMETRIC_SEPARATED: if (ColorChannels == 4) pt = PT_CMYK; else if (ColorChannels == 3) pt = PT_CMY; else if (ColorChannels == 6) pt = PT_HiFi; else if (ColorChannels == 7) pt = PT_HiFi7; else if (ColorChannels == 8) pt = PT_HiFi8; else if (ColorChannels == 9) pt = PT_HiFi9; else if (ColorChannels == 10) pt = PT_HiFi10; else if (ColorChannels == 11) pt = PT_HiFi11; else if (ColorChannels == 12) pt = PT_HiFi8; else if (ColorChannels == 13) pt = PT_HiFi13; else if (ColorChannels == 14) pt = PT_HiFi14; else if (ColorChannels == 15) pt = PT_HiFi15; else FatalError("What a weird separation of %d channels?!?!", ColorChannels); break; case PHOTOMETRIC_YCBCR: TIFFGetField(Bank, TIFFTAG_COMPRESSION, &Compression); { uint16 subx, suby; pt = PT_YCbCr; TIFFGetFieldDefaulted(Bank, TIFFTAG_YCBCRSUBSAMPLING, &subx, &suby); if (subx != 1 || suby != 1) FatalError("Sorry, subsampled images not supported"); } break; case 9: pt = PT_Lab; InputLabUsingICC = TRUE; break; case PHOTOMETRIC_CIELAB: pt = PT_Lab; InputLabUsingICC = FALSE; break; case PHOTOMETRIC_LOGLUV: /* CIE Log2(L) (u',v') */ TIFFSetField(Bank, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT); pt = PT_YUV; // *ICCSpace = icSigLuvData; bps = 16; // 16 bits forced by LibTiff break; default: FatalError("Unsupported TIFF color space (Photometric %d)", Photometric); } // Convert bits per sample to bytes per sample bps >>= 3; return (COLORSPACE_SH(pt)|PLANAR_SH(IsPlanar)|EXTRA_SH(extra)|CHANNELS_SH(ColorChannels)|BYTES_SH(bps)|FLAVOR_SH(reverse)); }
/* Transform an entire buffer */ void gscms_transform_color_buffer(gx_device *dev, gsicc_link_t *icclink, gsicc_bufferdesc_t *input_buff_desc, gsicc_bufferdesc_t *output_buff_desc, void *inputbuffer, void *outputbuffer) { cmsHTRANSFORM hTransform = (cmsHTRANSFORM)icclink->link_handle; cmsUInt32Number dwInputFormat, dwOutputFormat, num_src_lcms, num_des_lcms; int planar,numbytes, big_endian, hasalpha, k; unsigned char *inputpos, *outputpos; #if DUMP_CMS_BUFFER FILE *fid_in, *fid_out; #endif /* Although little CMS does make assumptions about data types in its transformations you can change it after the fact. */ /* Set us to the proper output type */ /* Note, we could speed this up by passing back the encoded data type to the caller so that we could avoid having to go through this computation each time if they are doing multiple calls to this operation */ /* Color space MUST be the same */ dwInputFormat = COLORSPACE_SH(T_COLORSPACE(cmsGetTransformInputFormat(hTransform))); dwOutputFormat = COLORSPACE_SH(T_COLORSPACE(cmsGetTransformOutputFormat(hTransform))); /* Now set if we have planar, num bytes, endian case, and alpha data to skip */ /* Planar -- pdf14 case for example */ planar = input_buff_desc->is_planar; dwInputFormat = dwInputFormat | PLANAR_SH(planar); planar = output_buff_desc->is_planar; dwOutputFormat = dwOutputFormat | PLANAR_SH(planar); /* 8 or 16 byte input and output */ numbytes = input_buff_desc->bytes_per_chan; if (numbytes>2) numbytes = 0; /* littleCMS encodes float with 0 ToDO. */ dwInputFormat = dwInputFormat | BYTES_SH(numbytes); numbytes = output_buff_desc->bytes_per_chan; if (numbytes>2) numbytes = 0; dwOutputFormat = dwOutputFormat | BYTES_SH(numbytes); /* endian */ big_endian = !input_buff_desc->little_endian; dwInputFormat = dwInputFormat | ENDIAN16_SH(big_endian); big_endian = !output_buff_desc->little_endian; dwOutputFormat = dwOutputFormat | ENDIAN16_SH(big_endian); /* number of channels. This should not really be changing! */ num_src_lcms = T_CHANNELS(cmsGetTransformInputFormat(hTransform)); num_des_lcms = T_CHANNELS(cmsGetTransformOutputFormat(hTransform)); if (num_src_lcms != input_buff_desc->num_chan || num_des_lcms != output_buff_desc->num_chan) { /* We can't transform this. Someone is doing something odd */ return; } dwInputFormat = dwInputFormat | CHANNELS_SH(num_src_lcms); dwOutputFormat = dwOutputFormat | CHANNELS_SH(num_des_lcms); /* alpha, which is passed through unmolested */ /* ToDo: Right now we always must have alpha last */ /* This is really only going to be an issue when we have interleaved alpha data */ hasalpha = input_buff_desc->has_alpha; dwInputFormat = dwInputFormat | EXTRA_SH(hasalpha); dwOutputFormat = dwOutputFormat | EXTRA_SH(hasalpha); /* Change the formatters */ cmsChangeBuffersFormat(hTransform,dwInputFormat,dwOutputFormat); /* littleCMS knows nothing about word boundarys. As such, we need to do this row by row adjusting for our stride. Output buffer must already be allocated. ToDo: Check issues with plane and row stride and word boundry */ inputpos = (byte *) inputbuffer; outputpos = (byte *) outputbuffer; if(input_buff_desc->is_planar) { /* Determine if we can do this in one operation or if we have to break it up. Essentially if the width * height = plane_stride then yes. If we are doing some subsection of a plane then no. */ if (input_buff_desc->num_rows * input_buff_desc->pixels_per_row == input_buff_desc->plane_stride && output_buff_desc->num_rows * output_buff_desc->pixels_per_row == output_buff_desc->plane_stride) { /* Do entire buffer.*/ cmsDoTransform(hTransform, inputpos, outputpos, input_buff_desc->plane_stride); } else { /* We have to do this row by row, with memory transfers */ byte *temp_des, *temp_src; int source_size = input_buff_desc->bytes_per_chan * input_buff_desc->pixels_per_row; int des_size = output_buff_desc->bytes_per_chan * output_buff_desc->pixels_per_row; int y, i; temp_src = (byte*) gs_alloc_bytes(icclink->icc_link_cache->memory, source_size * input_buff_desc->num_chan, "gscms_transform_color_buffer"); if (temp_src == NULL) return; temp_des = (byte*) gs_alloc_bytes(icclink->icc_link_cache->memory, des_size * output_buff_desc->num_chan, "gscms_transform_color_buffer"); if (temp_des == NULL) return; for (y = 0; y < input_buff_desc->num_rows; y++) { byte *src_cm = temp_src; byte *src_buff = inputpos; byte *des_cm = temp_des; byte *des_buff = outputpos; /* Put into planar temp buffer */ for (i = 0; i < input_buff_desc->num_chan; i ++) { memcpy(src_cm, src_buff, source_size); src_cm += source_size; src_buff += input_buff_desc->plane_stride; } /* Transform */ cmsDoTransform(hTransform, temp_src, temp_des, input_buff_desc->pixels_per_row); /* Get out of temp planar buffer */ for (i = 0; i < output_buff_desc->num_chan; i ++) { memcpy(des_buff, des_cm, des_size); des_cm += des_size; des_buff += output_buff_desc->plane_stride; } inputpos += input_buff_desc->row_stride; outputpos += output_buff_desc->row_stride; } gs_free_object(icclink->icc_link_cache->memory, temp_src, "gscms_transform_color_buffer"); gs_free_object(icclink->icc_link_cache->memory, temp_des, "gscms_transform_color_buffer"); } } else { /* Do row by row. */ for(k = 0; k < input_buff_desc->num_rows ; k++) { cmsDoTransform(hTransform, inputpos, outputpos, input_buff_desc->pixels_per_row); inputpos += input_buff_desc->row_stride; outputpos += output_buff_desc->row_stride; } } #if DUMP_CMS_BUFFER fid_in = gp_fopen("CM_Input.raw","ab"); fid_out = gp_fopen("CM_Output.raw","ab"); fwrite((unsigned char*) inputbuffer,sizeof(unsigned char), input_buff_desc->row_stride,fid_in); fwrite((unsigned char*) outputbuffer,sizeof(unsigned char), output_buff_desc->row_stride,fid_out); fclose(fid_in); fclose(fid_out); #endif }
static DWORD MakeFormatDescriptor(icColorSpaceSignature ColorSpace, int Bytes) { int Channels = _cmsChannelsOf(ColorSpace); return COLORSPACE_SH(ICC2LCMS(ColorSpace))|BYTES_SH(Bytes)|CHANNELS_SH(Channels)|PLANAR_SH(1); }