]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/test/raw_decode.c
No changes, just move mouse capturing bookkeeping data out of wxWindow.
[wxWidgets.git] / src / tiff / test / raw_decode.c
1
2 /*
3 * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
4 *
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.
12 *
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.
16 *
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
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * TIFF Library
27 *
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.
31 */
32
33 #include "tif_config.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41
42 #include "tiffio.h"
43
44 #include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */
45
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 };
49
50 static int check_cluster( int cluster, unsigned char *buffer, unsigned char *expected_cluster ) {
51 unsigned char *target = buffer + cluster*6;
52
53 if (memcmp(target, expected_cluster, 6) == 0) {
54 return 0;
55 }
56
57 fprintf( stderr, "Cluster %d did not match expected results.\n", cluster );
58 fprintf( stderr,
59 "Expect: %3d %3d %3d %3d\n"
60 " %3d %3d\n",
61 expected_cluster[0], expected_cluster[1],
62 expected_cluster[4], expected_cluster[5],
63 expected_cluster[2], expected_cluster[3] );
64 fprintf( stderr,
65 " Got: %3d %3d %3d %3d\n"
66 " %3d %3d\n",
67 target[0], target[1],
68 target[4], target[5],
69 target[2], target[3] );
70 return 1;
71 }
72
73 static int check_rgb_pixel( int pixel, int red, int green, int blue, unsigned char *buffer ) {
74 unsigned char *rgb = buffer + 3 * pixel;
75
76 if( rgb[0] == red && rgb[1] == green && rgb[2] == blue ) {
77 return 0;
78 }
79
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] );
83 return 1;
84 }
85
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];
90
91 if( TIFFGetR(rgba) == (uint32) red && TIFFGetG(rgba) == (uint32) green &&
92 TIFFGetB(rgba) == (uint32) blue && TIFFGetA(rgba) == (uint32) alpha ) {
93 return 0;
94 }
95
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) );
100 return 1;
101 }
102
103 int
104 main(int argc, char **argv)
105 {
106 TIFF *tif;
107 static const char *srcfilerel = "images/quad-tile.jpg.tiff";
108 char *srcdir = NULL;
109 char srcfile[1024];
110 unsigned short h, v;
111 int status;
112 unsigned char *buffer;
113 uint32 *rgba_buffer;
114 tsize_t sz, szout;
115 unsigned int pixel_status = 0;
116
117 (void) argc;
118 (void) argv;
119
120 if ((srcdir = getenv("srcdir")) == NULL) {
121 srcdir = ".";
122 }
123 if ((strlen(srcdir) + 1 + strlen(srcfilerel)) >= sizeof(srcfile)) {
124 fprintf( stderr, "srcdir too long %s\n", srcdir);
125 exit( 1 );
126 }
127 strcpy(srcfile,srcdir);
128 strcat(srcfile,"/");
129 strcat(srcfile,srcfilerel);
130
131 tif = TIFFOpen(srcfile,"r");
132 if ( tif == NULL ) {
133 fprintf( stderr, "Could not open %s\n", srcfile);
134 exit( 1 );
135 }
136
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" );
140 exit(1);
141 }
142
143 /*
144 * What is the appropriate size of a YCbCr encoded tile?
145 */
146 sz = TIFFTileSize(tif);
147 if( sz != 24576) {
148 fprintf(stderr, "tiles are %d bytes\n", (int)sz);
149 exit(1);
150 }
151
152 buffer = (unsigned char *) malloc(sz);
153
154 /*
155 * Read a tile in decompressed form, but still YCbCr subsampled.
156 */
157 szout = TIFFReadEncodedTile(tif,9,buffer,sz);
158 if (szout != sz) {
159 fprintf( stderr,
160 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
161 (int) szout, (int) sz );
162 return 1;
163 }
164
165 if( check_cluster( 0, buffer, cluster_0 )
166 || check_cluster( 64, buffer, cluster_64 )
167 || check_cluster( 128, buffer, cluster_128 ) ) {
168 exit(1);
169 }
170 free(buffer);
171
172 /*
173 * Read a tile using the built-in conversion to RGB format provided by the JPEG library.
174 */
175 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
176
177 sz = TIFFTileSize(tif);
178 if( sz != 128*128*3) {
179 fprintf(stderr, "tiles are %d bytes\n", (int)sz);
180 exit(1);
181 }
182
183 buffer = (unsigned char *) malloc(sz);
184
185 szout = TIFFReadEncodedTile(tif,9,buffer,sz);
186 if (szout != sz) {
187 fprintf( stderr,
188 "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
189 (int) szout, (int) sz );
190 return 1;
191 }
192
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 );
197 #else
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 );
201 #endif
202
203 free( buffer );
204
205 TIFFClose(tif);
206
207 /*
208 * Reopen and test reading using the RGBA interface.
209 */
210 tif = TIFFOpen(srcfile,"r");
211
212 sz = 128 * 128 * sizeof(uint32);
213 rgba_buffer = (uint32 *) malloc(sz);
214
215 if (!TIFFReadRGBATile( tif, 1*128, 2*128, rgba_buffer )) {
216 fprintf( stderr, "TIFFReadRGBATile() returned failure code.\n" );
217 return 1;
218 }
219
220 /*
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.
225 */
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 );
230 #else
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 );
234 #endif
235
236 free( rgba_buffer );
237 TIFFClose(tif);
238
239 if (pixel_status) {
240 exit(1);
241 }
242
243 exit( 0 );
244 }
245
246 /* vim: set ts=8 sts=8 sw=8 noet: */
247 /*
248 * Local Variables:
249 * mode: c
250 * c-basic-offset: 8
251 * fill-column: 78
252 * End:
253 */