2006.03.29 22:22 "[Tiff] TIFF_UPSAMPLED", by Frank Warmerdam

2006.03.30 18:44 "Re: [Tiff] TIFF_UPSAMPLED", by Antonio Scuri

Hi,

   I solved my problem when saving/loading YCbCr color mode JPEG
compressed TIFF in a different way.

   When encoding I do not want to provide a downsampled output so I changed:
function JPEGPreEncode, at line 1298 in tif_jpeg.c from CVS
FROM
                 if (sp->photometric == PHOTOMETRIC_YCBCR) {
                         if (sp->jpegcolormode == JPEGCOLORMODE_RGB) {
                                 sp->cinfo.c.in_color_space = JCS_RGB;
                         } else {
                                 sp->cinfo.c.in_color_space = JCS_YCbCr;
                                 if (sp->h_sampling != 1 ||
sp->v_sampling != 1)
                                         downsampled_input = TRUE;

TO
                 if (sp->photometric == PHOTOMETRIC_YCBCR) {
                         if (sp->jpegcolormode == JPEGCOLORMODE_RGB) {
                                 sp->cinfo.c.in_color_space = JCS_RGB;
                         } else {
                                 sp->cinfo.c.in_color_space = JCS_YCbCr;
                                 tif->tif_flags |= TIFF_UPSAMPLED; /*
Changed for IM
                                 if (sp->h_sampling != 1 ||
sp->v_sampling != 1)

                                         downsampled_input = TRUE;   */

And

   Also when decoding I do not want to get a downsampled output so I changed:
function JPEGPreDecode, at line 801 in tif_jpeg.c from CVS
FROM
         if (td->td_planarconfig == PLANARCONFIG_CONTIG &&

    sp->photometric == PHOTOMETRIC_YCBCR &&
    sp->jpegcolormode == JPEGCOLORMODE_RGB) {
/* Convert YCbCr to RGB */
        sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
        sp->cinfo.d.out_color_space = JCS_RGB;
} else {
                /* Suppress colorspace handling */
        sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
        sp->cinfo.d.out_color_space = JCS_UNKNOWN;
        if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
            (sp->h_sampling != 1 || sp->v_sampling != 1))
                downsampled_output = TRUE;
        /* XXX what about up-sampling? */

TO
         if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
             sp->photometric == PHOTOMETRIC_YCBCR &&
             sp->jpegcolormode == JPEGCOLORMODE_RGB) {
         /* Convert YCbCr to RGB */
                 sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
                 sp->cinfo.d.out_color_space = JCS_RGB;
         } else {
                         /* Suppress colorspace handling */
                 sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;

sp->cinfo.d.out_color_space = JCS_UNKNOWN; tif->tif_flags |= TIFF_UPSAMPLED; /* Changed for IM if (td->td_planarconfig == PLANARCONFIG_CONTIG &&

    (sp->h_sampling != 1 || sp->v_sampling != 1))

                         downsampled_output = TRUE;   */

        /* XXX what about up-sampling? */
}

Hope this helps too.

Best,
scuri

For roughly the last six or seven years I have avoided writing YCbCr color

mode JPEG compressed TIFF files in my application because it seemed terribly complex to make it work. Every feeble attempt was defeated by lots of gobblygook from the library.

Well, finally a client has banged me over the head until I worked out why it wasn't working for me. It turns out that when writing stuff in RGB mode (for autoconversion to ycbcr) I was setting the TIFFTAG_JPEGCOLORMODE to RGB *before* I was setting TIFFTAG_PHOTOMETRIC to TIFFTAG_YCBCR. This resulted in the TIFF_UPSAMPLED flag being set improperly and it wasn't reset when the photometric interpretation was set.

The work around is to ensure that application code sets the JPEGCOLORMODE *after* the photometric interpretation. I have also made changes in the library to reset the upsampled flag when the photometric flag is set, so hopefully in the future applications won't be subject to this order of setting flags vagarity.

The larger lesson here might be that applications may be safer setting codec-specific flags and pseudo-flags *after* setting all the basic image configuration tags as the codec specific tags are the ones most likely to have odd conditions around them.

I just thought I would pass on what has been (for me) hard won wisdom.