Return-Path: <tiff-bounces@lists.maptools.org>
X-Original-To: info@awaresystems.be
Delivered-To: info@awaresystems.be
Received: from mx-d.b-one.net (mx-d.one.com [91.198.169.51])
	by mail9.b-one.net (Postfix) with ESMTP id 9C77C40090A
	for <info@awaresystems.be>; Thu, 14 Jan 2010 17:05:47 +0100 (CET)
Received: by mx-d.b-one.net (Postfix, from userid 106)
	id 8FC4FC99; Thu, 14 Jan 2010 17:05:47 +0100 (CET)
X-Virus-Scanned: by one.com
X-Spam-Score: 0
X-Spam-Level: 
X-Spam-Status: No, score=0 tagged_above=0 required=6 tests=[none]
X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4
Received: from lists.maptools.org (lists.maptools.org [65.39.174.144]) by
 mx-d.b-one.net (Postfix) with ESMTP id 9AB4EB0F023 for
 <info@awaresystems.be>; Thu, 14 Jan 2010 17:05:37 +0100 (CET)
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by
 lists.maptools.org (Postfix) with ESMTP id 6E0811D4A2E9C; Thu, 14 Jan 2010
 11:04:39 -0500 (EST)
Received: from isadora.cybox.com (isadora.cybox.com [206.168.112.10]) by
 lists.maptools.org (Postfix) with ESMTP id 223821D1AFC16 for
 <tiff@lists.maptools.org>; Thu, 14 Jan 2010 11:02:56 -0500 (EST)
Received: from boreas.wriverody.net (vc7-1-52.dsl.indra.com [199.45.160.84])
 (authenticated) by isadora.cybox.com (msa 8.13.8/8.13.6) with ESMTP id
 o0EG2lYK017159;  Thu, 14 Jan 2010 09:02:48 -0700
Message-ID: <4B4F4022.7000607@cybox.com>
Date: Thu, 14 Jan 2010 09:02:42 -0700
From: Richard Nolde <richard.nolde@cybox.com>
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5)
 Gecko/20091209 Fedora/3.0-3.fc11 Thunderbird/3.0
MIME-Version: 1.0
To: jcupitt@gmail.com, Tiff Listserve <tiff@lists.maptools.org>
References: <mailman.3.1260550802.30590.tiff@lists.maptools.org>
 <4B229448.7010108@cybox.com>
 <522c6461001140335u7d859ca8nc983ec0e8b8908c2@mail.gmail.com>
In-Reply-To: <522c6461001140335u7d859ca8nc983ec0e8b8908c2@mail.gmail.com>
Subject: Re: [Tiff] JPEG compression in 3.9.2
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: TIFF and libtiff discussion <tiff.lists.maptools.org>
List-Unsubscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
 <mailto:tiff-request@lists.maptools.org?subject=unsubscribe>
List-Archive: <http://lists.maptools.org/mailman/private/tiff>
List-Post: <mailto:tiff@lists.maptools.org>
List-Help: <mailto:tiff-request@lists.maptools.org?subject=help>
List-Subscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
 <mailto:tiff-request@lists.maptools.org?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Sender: tiff-bounces@lists.maptools.org
Errors-To: tiff-bounces@lists.maptools.org

On 01/14/2010 04:35 AM, jcupitt@gmail.com wrote:
> I just ran into this problem as well. TIFFReadEncodedStrip() seems to
> write beyond the end of it's buffer for subsampled JPEG YCbCr images.
>    
I did not have the problem of writing beyond the end of the buffer 
because I allocated a buffer large enough for the upsampled data to 
begin with.  My experience was that the amount of data returned was only 
half of what I expected when the data was subsampled.
> I was able to get it working by adding this case to my reader:
>
>          case PHOTOMETRIC_YCBCR:
>                  TIFFSetField( tiff, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB );
>                  parse_rgb8( ...
>                  break;
>
> ie. by asking libjpeg to convert YCbCr to rgb on load. Not a good
> solution if you actually want YCbCR of course, but it did fix my
> crashes.
>
> John
John,
    Thanks for your note.  I was using a much broader brush with the 
following in the code to load the image:

   if (input_compression == COMPRESSION_JPEG)
     {
     jpegcolormode = JPEGCOLORMODE_RGB;
     TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
     }

and the following in the code to write the image.

   TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);
#ifdef DEBUG2
   TIFFError("writeSingleSection", "Input compression: %s",
         (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :
         ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non 
Jpeg"));
#endif
   if (compression == COMPRESSION_JPEG)
     {
     if ((input_photometric == PHOTOMETRIC_PALETTE) ||  /* color map 
indexed */
         (input_photometric == PHOTOMETRIC_MASK))       /* $holdout mask */
       {
       TIFFError ("writeSingleSection",
                  "JPEG compression cannot be used with %s image data",
          (input_photometric == PHOTOMETRIC_PALETTE) ?
                  "palette" : "mask");
       return (-1);
       }
     if (input_photometric == PHOTOMETRIC_RGB)
       {
       if (jpegcolormode == JPEGCOLORMODE_RGB)
     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
       else
     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
       }
     else
     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);
     }

Based on what I've been reading on the list, I may need to put some of 
the code that is in the output logic into the input logic. I was trying 
to handle the case of YCbCr separately from OJPEG and new JPEG and 
finally gave up.  It should be possible to have YCrCr data that is not 
JPEG (Old or New) compressed.  I was hoping that Lee Howard's work might 
simplify this before I returned to my code as my attempts to wade 
through the JPEG interface in Libtiff did not get me very far.

Richard


_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
