]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/test/raw_decode.c
ce9e8f2701c21663b5c47603bc2962e1b02f8ba3
4 * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 * The objective of this test suite is to test the JPEGRawDecode()
30 * interface via TIFReadEncodedTile(). This function with YCbCr subsampling
31 * is a frequent source of bugs.
34 #include "tif_config.h"
45 #include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */
47 static unsigned char cluster_0
[] = { 0, 0, 2, 0, 138, 139 };
48 static unsigned char cluster_64
[] = { 0, 0, 9, 6, 134, 119 };
49 static unsigned char cluster_128
[] = { 44, 40, 63, 59, 230, 95 };
51 static int check_cluster( int cluster
, unsigned char *buffer
, unsigned char *expected_cluster
) {
52 unsigned char *target
= buffer
+ cluster
*6;
54 if (memcmp(target
, expected_cluster
, 6) == 0) {
58 fprintf( stderr
, "Cluster %d did not match expected results.\n", cluster
);
60 "Expect: %3d %3d %3d %3d\n"
62 expected_cluster
[0], expected_cluster
[1],
63 expected_cluster
[4], expected_cluster
[5],
64 expected_cluster
[2], expected_cluster
[3] );
66 " Got: %3d %3d %3d %3d\n"
70 target
[2], target
[3] );
74 static int check_rgb_pixel( int pixel
, int red
, int green
, int blue
, unsigned char *buffer
) {
75 unsigned char *rgb
= buffer
+ 3 * pixel
;
77 if( rgb
[0] == red
&& rgb
[1] == green
&& rgb
[2] == blue
) {
81 fprintf( stderr
, "Pixel %d did not match expected results.\n", pixel
);
82 fprintf( stderr
, "Expect: %3d %3d %3d\n", red
, green
, blue
);
83 fprintf( stderr
, " Got: %3d %3d %3d\n", rgb
[0], rgb
[1], rgb
[2] );
87 static int check_rgba_pixel( int pixel
, int red
, int green
, int blue
, int alpha
, uint32
*buffer
) {
88 /* RGBA images are upside down - adjust for normal ordering */
89 int adjusted_pixel
= pixel
% 128 + (127 - (pixel
/128)) * 128;
90 uint32 rgba
= buffer
[adjusted_pixel
];
92 if( TIFFGetR(rgba
) == (uint32
) red
&& TIFFGetG(rgba
) == (uint32
) green
&&
93 TIFFGetB(rgba
) == (uint32
) blue
&& TIFFGetA(rgba
) == (uint32
) alpha
) {
97 fprintf( stderr
, "Pixel %d did not match expected results.\n", pixel
);
98 fprintf( stderr
, "Expect: %3d %3d %3d %3d\n", red
, green
, blue
, alpha
);
99 fprintf( stderr
, " Got: %3d %3d %3d %3d\n",
100 TIFFGetR(rgba
), TIFFGetG(rgba
), TIFFGetB(rgba
), TIFFGetA(rgba
) );
105 main(int argc
, char **argv
)
108 static const char *srcfilerel
= "images/quad-tile.jpg.tiff";
113 unsigned char *buffer
;
116 unsigned int pixel_status
= 0;
121 if ((srcdir
= getenv("srcdir")) == NULL
) {
124 if ((strlen(srcdir
) + 1 + strlen(srcfilerel
)) >= sizeof(srcfile
)) {
125 fprintf( stderr
, "srcdir too long %s\n", srcdir
);
128 strcpy(srcfile
,srcdir
);
130 strcat(srcfile
,srcfilerel
);
132 tif
= TIFFOpen(srcfile
,"r");
134 fprintf( stderr
, "Could not open %s\n", srcfile
);
138 status
= TIFFGetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
, &h
, &v
);
139 if ( status
== 0 || h
!= 2 || v
!= 2) {
140 fprintf( stderr
, "Could not retrieve subsampling tag.\n" );
145 * What is the appropriate size of a YCbCr encoded tile?
147 sz
= TIFFTileSize(tif
);
149 fprintf(stderr
, "tiles are %d bytes\n", (int)sz
);
153 buffer
= (unsigned char *) malloc(sz
);
156 * Read a tile in decompressed form, but still YCbCr subsampled.
158 szout
= TIFFReadEncodedTile(tif
,9,buffer
,sz
);
161 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
162 (int) szout
, (int) sz
);
166 if( check_cluster( 0, buffer
, cluster_0
)
167 || check_cluster( 64, buffer
, cluster_64
)
168 || check_cluster( 128, buffer
, cluster_128
) ) {
174 * Read a tile using the built-in conversion to RGB format provided by the JPEG library.
176 TIFFSetField(tif
, TIFFTAG_JPEGCOLORMODE
, JPEGCOLORMODE_RGB
);
178 sz
= TIFFTileSize(tif
);
179 if( sz
!= 128*128*3) {
180 fprintf(stderr
, "tiles are %d bytes\n", (int)sz
);
184 buffer
= (unsigned char *) malloc(sz
);
186 szout
= TIFFReadEncodedTile(tif
,9,buffer
,sz
);
189 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
190 (int) szout
, (int) sz
);
194 #if JPEG_LIB_VERSION >= 70
195 pixel_status
|= check_rgb_pixel( 0, 18, 0, 41, buffer
);
196 pixel_status
|= check_rgb_pixel( 64, 0, 0, 0, buffer
);
197 pixel_status
|= check_rgb_pixel( 512, 5, 34, 196, buffer
);
199 pixel_status
|= check_rgb_pixel( 0, 15, 0, 18, buffer
);
200 pixel_status
|= check_rgb_pixel( 64, 0, 0, 2, buffer
);
201 pixel_status
|= check_rgb_pixel( 512, 6, 36, 182, buffer
);
209 * Reopen and test reading using the RGBA interface.
211 tif
= TIFFOpen(srcfile
,"r");
213 sz
= 128 * 128 * sizeof(uint32
);
214 rgba_buffer
= (uint32
*) malloc(sz
);
216 if (!TIFFReadRGBATile( tif
, 1*128, 2*128, rgba_buffer
)) {
217 fprintf( stderr
, "TIFFReadRGBATile() returned failure code.\n" );
222 * Currently TIFFReadRGBATile() just uses JPEGCOLORMODE_RGB so this
223 * trivally matches the last results. Eventually we should actually
224 * accomplish it from the YCbCr subsampled buffer ourselves in which
225 * case the results may be subtly different but similar.
227 #if JPEG_LIB_VERSION >= 70
228 pixel_status
|= check_rgba_pixel( 0, 18, 0, 41, 255, rgba_buffer
);
229 pixel_status
|= check_rgba_pixel( 64, 0, 0, 0, 255, rgba_buffer
);
230 pixel_status
|= check_rgba_pixel( 512, 5, 34, 196, 255, rgba_buffer
);
232 pixel_status
|= check_rgba_pixel( 0, 15, 0, 18, 255, rgba_buffer
);
233 pixel_status
|= check_rgba_pixel( 64, 0, 0, 2, 255, rgba_buffer
);
234 pixel_status
|= check_rgba_pixel( 512, 6, 36, 182, 255, rgba_buffer
);
247 /* vim: set ts=8 sts=8 sw=8 noet: */