]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/test/raw_decode.c
3 * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * The objective of this test suite is to test the JPEGRawDecode()
29 * interface via TIFReadEncodedTile(). This function with YCbCr subsampling
30 * is a frequent source of bugs.
33 #include "tif_config.h"
44 #include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */
46 static unsigned char cluster_0
[] = { 0, 0, 2, 0, 138, 139 };
47 static unsigned char cluster_64
[] = { 0, 0, 9, 6, 134, 119 };
48 static unsigned char cluster_128
[] = { 44, 40, 63, 59, 230, 95 };
50 static int check_cluster( int cluster
, unsigned char *buffer
, unsigned char *expected_cluster
) {
51 unsigned char *target
= buffer
+ cluster
*6;
53 if (memcmp(target
, expected_cluster
, 6) == 0) {
57 fprintf( stderr
, "Cluster %d did not match expected results.\n", cluster
);
59 "Expect: %3d %3d %3d %3d\n"
61 expected_cluster
[0], expected_cluster
[1],
62 expected_cluster
[4], expected_cluster
[5],
63 expected_cluster
[2], expected_cluster
[3] );
65 " Got: %3d %3d %3d %3d\n"
69 target
[2], target
[3] );
73 static int check_rgb_pixel( int pixel
, int red
, int green
, int blue
, unsigned char *buffer
) {
74 unsigned char *rgb
= buffer
+ 3 * pixel
;
76 if( rgb
[0] == red
&& rgb
[1] == green
&& rgb
[2] == blue
) {
80 fprintf( stderr
, "Pixel %d did not match expected results.\n", pixel
);
81 fprintf( stderr
, "Expect: %3d %3d %3d\n", red
, green
, blue
);
82 fprintf( stderr
, " Got: %3d %3d %3d\n", rgb
[0], rgb
[1], rgb
[2] );
86 static int check_rgba_pixel( int pixel
, int red
, int green
, int blue
, int alpha
, uint32
*buffer
) {
87 /* RGBA images are upside down - adjust for normal ordering */
88 int adjusted_pixel
= pixel
% 128 + (127 - (pixel
/128)) * 128;
89 uint32 rgba
= buffer
[adjusted_pixel
];
91 if( TIFFGetR(rgba
) == (uint32
) red
&& TIFFGetG(rgba
) == (uint32
) green
&&
92 TIFFGetB(rgba
) == (uint32
) blue
&& TIFFGetA(rgba
) == (uint32
) alpha
) {
96 fprintf( stderr
, "Pixel %d did not match expected results.\n", pixel
);
97 fprintf( stderr
, "Expect: %3d %3d %3d %3d\n", red
, green
, blue
, alpha
);
98 fprintf( stderr
, " Got: %3d %3d %3d %3d\n",
99 TIFFGetR(rgba
), TIFFGetG(rgba
), TIFFGetB(rgba
), TIFFGetA(rgba
) );
104 main(int argc
, char **argv
)
107 static const char *srcfilerel
= "images/quad-tile.jpg.tiff";
112 unsigned char *buffer
;
115 unsigned int pixel_status
= 0;
120 if ((srcdir
= getenv("srcdir")) == NULL
) {
123 if ((strlen(srcdir
) + 1 + strlen(srcfilerel
)) >= sizeof(srcfile
)) {
124 fprintf( stderr
, "srcdir too long %s\n", srcdir
);
127 strcpy(srcfile
,srcdir
);
129 strcat(srcfile
,srcfilerel
);
131 tif
= TIFFOpen(srcfile
,"r");
133 fprintf( stderr
, "Could not open %s\n", srcfile
);
137 status
= TIFFGetField(tif
,TIFFTAG_YCBCRSUBSAMPLING
, &h
, &v
);
138 if ( status
== 0 || h
!= 2 || v
!= 2) {
139 fprintf( stderr
, "Could not retrieve subsampling tag.\n" );
144 * What is the appropriate size of a YCbCr encoded tile?
146 sz
= TIFFTileSize(tif
);
148 fprintf(stderr
, "tiles are %d bytes\n", (int)sz
);
152 buffer
= (unsigned char *) malloc(sz
);
155 * Read a tile in decompressed form, but still YCbCr subsampled.
157 szout
= TIFFReadEncodedTile(tif
,9,buffer
,sz
);
160 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
161 (int) szout
, (int) sz
);
165 if( check_cluster( 0, buffer
, cluster_0
)
166 || check_cluster( 64, buffer
, cluster_64
)
167 || check_cluster( 128, buffer
, cluster_128
) ) {
173 * Read a tile using the built-in conversion to RGB format provided by the JPEG library.
175 TIFFSetField(tif
, TIFFTAG_JPEGCOLORMODE
, JPEGCOLORMODE_RGB
);
177 sz
= TIFFTileSize(tif
);
178 if( sz
!= 128*128*3) {
179 fprintf(stderr
, "tiles are %d bytes\n", (int)sz
);
183 buffer
= (unsigned char *) malloc(sz
);
185 szout
= TIFFReadEncodedTile(tif
,9,buffer
,sz
);
188 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
189 (int) szout
, (int) sz
);
193 #if JPEG_LIB_VERSION >= 70
194 pixel_status
|= check_rgb_pixel( 0, 18, 0, 41, buffer
);
195 pixel_status
|= check_rgb_pixel( 64, 0, 0, 0, buffer
);
196 pixel_status
|= check_rgb_pixel( 512, 5, 34, 196, buffer
);
198 pixel_status
|= check_rgb_pixel( 0, 15, 0, 18, buffer
);
199 pixel_status
|= check_rgb_pixel( 64, 0, 0, 2, buffer
);
200 pixel_status
|= check_rgb_pixel( 512, 6, 36, 182, buffer
);
208 * Reopen and test reading using the RGBA interface.
210 tif
= TIFFOpen(srcfile
,"r");
212 sz
= 128 * 128 * sizeof(uint32
);
213 rgba_buffer
= (uint32
*) malloc(sz
);
215 if (!TIFFReadRGBATile( tif
, 1*128, 2*128, rgba_buffer
)) {
216 fprintf( stderr
, "TIFFReadRGBATile() returned failure code.\n" );
221 * Currently TIFFReadRGBATile() just uses JPEGCOLORMODE_RGB so this
222 * trivally matches the last results. Eventually we should actually
223 * accomplish it from the YCbCr subsampled buffer ourselves in which
224 * case the results may be subtly different but similar.
226 #if JPEG_LIB_VERSION >= 70
227 pixel_status
|= check_rgba_pixel( 0, 18, 0, 41, 255, rgba_buffer
);
228 pixel_status
|= check_rgba_pixel( 64, 0, 0, 0, 255, rgba_buffer
);
229 pixel_status
|= check_rgba_pixel( 512, 5, 34, 196, 255, rgba_buffer
);
231 pixel_status
|= check_rgba_pixel( 0, 15, 0, 18, 255, rgba_buffer
);
232 pixel_status
|= check_rgba_pixel( 64, 0, 0, 2, 255, rgba_buffer
);
233 pixel_status
|= check_rgba_pixel( 512, 6, 36, 182, 255, rgba_buffer
);
246 /* vim: set ts=8 sts=8 sw=8 noet: */