| 1 |
/* |
| 2 |
* Copyright (c) 2012, Frank Warmerdam <[email protected]> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 5 |
* its documentation for any purpose is hereby granted without fee, provided |
| 6 |
* that (i) the above copyright notices and this permission notice appear in |
| 7 |
* all copies of the software and related documentation, and (ii) the names of |
| 8 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 9 |
* publicity relating to the software without the specific, prior written |
| 10 |
* permission of Sam Leffler and Silicon Graphics. |
| 11 |
* |
| 12 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 13 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 14 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* |
| 16 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 17 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 18 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 19 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 20 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 |
* OF THIS SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* TIFF Library |
| 26 |
* |
| 27 |
* The objective of this test suite is to test the JPEGRawDecode() |
| 28 |
* interface via TIFReadEncodedTile(). This function with YCbCr subsampling |
| 29 |
* is a frequent source of bugs. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include "tif_config.h" |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#ifdef HAVE_UNISTD_H |
| 38 |
#include <unistd.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "tiffio.h" |
| 42 |
|
| 43 |
#include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */ |
| 44 |
|
| 45 |
static unsigned char cluster_0[] = {0, 0, 2, 0, 138, 139}; |
| 46 |
static unsigned char cluster_64[] = {0, 0, 9, 6, 134, 119}; |
| 47 |
static unsigned char cluster_128[] = {44, 40, 63, 59, 230, 95}; |
| 48 |
|
| 49 |
static int check_cluster(int cluster, unsigned char *buffer, |
| 50 |
unsigned char *expected_cluster) |
| 51 |
{ |
| 52 |
unsigned char *target = buffer + cluster * 6; |
| 53 |
|
| 54 |
if (memcmp(target, expected_cluster, 6) == 0) |
| 55 |
{ |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
fprintf(stderr, "Cluster %d did not match expected results.\n", cluster); |
| 60 |
fprintf(stderr, |
| 61 |
"Expect: %3d %3d %3d %3d\n" |
| 62 |
" %3d %3d\n", |
| 63 |
expected_cluster[0], expected_cluster[1], expected_cluster[4], |
| 64 |
expected_cluster[5], expected_cluster[2], expected_cluster[3]); |
| 65 |
fprintf(stderr, |
| 66 |
" Got: %3d %3d %3d %3d\n" |
| 67 |
" %3d %3d\n", |
| 68 |
target[0], target[1], target[4], target[5], target[2], target[3]); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
static int check_rgb_pixel(int pixel, int min_red, int max_red, int min_green, |
| 73 |
int max_green, int min_blue, int max_blue, |
| 74 |
unsigned char *buffer) |
| 75 |
{ |
| 76 |
unsigned char *rgb = buffer + 3 * pixel; |
| 77 |
|
| 78 |
if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green && |
| 79 |
rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue) |
| 80 |
{ |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
fprintf(stderr, "Pixel %d did not match expected results.\n", pixel); |
| 85 |
fprintf(stderr, |
| 86 |
"Got R=%d (expected %d..%d), G=%d (expected %d..%d), B=%d " |
| 87 |
"(expected %d..%d)\n", |
| 88 |
rgb[0], min_red, max_red, rgb[1], min_green, max_green, rgb[2], |
| 89 |
min_blue, max_blue); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
static int check_rgba_pixel(int pixel, int min_red, int max_red, int min_green, |
| 94 |
int max_green, int min_blue, int max_blue, |
| 95 |
int min_alpha, int max_alpha, uint32_t *buffer) |
| 96 |
{ |
| 97 |
/* RGBA images are upside down - adjust for normal ordering */ |
| 98 |
int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; |
| 99 |
uint32_t rgba = buffer[adjusted_pixel]; |
| 100 |
|
| 101 |
if (TIFFGetR(rgba) >= (uint32_t)min_red && |
| 102 |
TIFFGetR(rgba) <= (uint32_t)max_red && |
| 103 |
TIFFGetG(rgba) >= (uint32_t)min_green && |
| 104 |
TIFFGetG(rgba) <= (uint32_t)max_green && |
| 105 |
TIFFGetB(rgba) >= (uint32_t)min_blue && |
| 106 |
TIFFGetB(rgba) <= (uint32_t)max_blue && |
| 107 |
TIFFGetA(rgba) >= (uint32_t)min_alpha && |
| 108 |
TIFFGetA(rgba) <= (uint32_t)max_alpha) |
| 109 |
{ |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
fprintf(stderr, "Pixel %d did not match expected results.\n", pixel); |
| 114 |
fprintf(stderr, |
| 115 |
"Got R=%d (expected %d..%d), G=%d (expected %d..%d), B=%d " |
| 116 |
"(expected %d..%d), A=%d (expected %d..%d)\n", |
| 117 |
TIFFGetR(rgba), min_red, max_red, TIFFGetG(rgba), min_green, |
| 118 |
max_green, TIFFGetB(rgba), min_blue, max_blue, TIFFGetA(rgba), |
| 119 |
min_alpha, max_alpha); |
| 120 |
return 1; |
| 121 |
} |
| 122 |
|
| 123 |
int main(int argc, char **argv) |
| 124 |
{ |
| 125 |
TIFF *tif; |
| 126 |
static const char *srcfilerel = "images/quad-tile.jpg.tiff"; |
| 127 |
char *srcdir = NULL; |
| 128 |
char srcfile[1024]; |
| 129 |
unsigned short h, v; |
| 130 |
int status; |
| 131 |
unsigned char *buffer; |
| 132 |
uint32_t *rgba_buffer; |
| 133 |
tsize_t sz, szout; |
| 134 |
unsigned int pixel_status = 0; |
| 135 |
|
| 136 |
(void)argc; |
| 137 |
(void)argv; |
| 138 |
|
| 139 |
if ((srcdir = getenv("srcdir")) == NULL) |
| 140 |
{ |
| 141 |
srcdir = "."; |
| 142 |
} |
| 143 |
if ((strlen(srcdir) + 1 + strlen(srcfilerel)) >= sizeof(srcfile)) |
| 144 |
{ |
| 145 |
fprintf(stderr, "srcdir too long %s\n", srcdir); |
| 146 |
exit(1); |
| 147 |
} |
| 148 |
strcpy(srcfile, srcdir); |
| 149 |
strcat(srcfile, "/"); |
| 150 |
strcat(srcfile, srcfilerel); |
| 151 |
|
| 152 |
tif = TIFFOpen(srcfile, "r"); |
| 153 |
if (tif == NULL) |
| 154 |
{ |
| 155 |
fprintf(stderr, "Could not open %s\n", srcfile); |
| 156 |
exit(1); |
| 157 |
} |
| 158 |
|
| 159 |
status = TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING, &h, &v); |
| 160 |
if (status == 0 || h != 2 || v != 2) |
| 161 |
{ |
| 162 |
fprintf(stderr, "Could not retrieve subsampling tag.\n"); |
| 163 |
exit(1); |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* What is the appropriate size of a YCbCr encoded tile? |
| 168 |
*/ |
| 169 |
sz = TIFFTileSize(tif); |
| 170 |
if (sz != 24576) |
| 171 |
{ |
| 172 |
fprintf(stderr, "tiles are %d bytes\n", (int)sz); |
| 173 |
exit(1); |
| 174 |
} |
| 175 |
|
| 176 |
buffer = (unsigned char *)malloc(sz); |
| 177 |
|
| 178 |
/* |
| 179 |
* Read a tile in decompressed form, but still YCbCr subsampled. |
| 180 |
*/ |
| 181 |
szout = TIFFReadEncodedTile(tif, 9, buffer, sz); |
| 182 |
if (szout != sz) |
| 183 |
{ |
| 184 |
fprintf(stderr, |
| 185 |
"Did not get expected result code from " |
| 186 |
"TIFFReadEncodedTile()(%d instead of %d)\n", |
| 187 |
(int)szout, (int)sz); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
|
| 191 |
if (check_cluster(0, buffer, cluster_0) || |
| 192 |
check_cluster(64, buffer, cluster_64) || |
| 193 |
check_cluster(128, buffer, cluster_128)) |
| 194 |
{ |
| 195 |
exit(1); |
| 196 |
} |
| 197 |
free(buffer); |
| 198 |
|
| 199 |
/* |
| 200 |
* Read a tile using the built-in conversion to RGB format provided by the |
| 201 |
* JPEG library. |
| 202 |
*/ |
| 203 |
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 204 |
|
| 205 |
sz = TIFFTileSize(tif); |
| 206 |
if (sz != 128 * 128 * 3) |
| 207 |
{ |
| 208 |
fprintf(stderr, "tiles are %d bytes\n", (int)sz); |
| 209 |
exit(1); |
| 210 |
} |
| 211 |
|
| 212 |
buffer = (unsigned char *)malloc(sz); |
| 213 |
|
| 214 |
szout = TIFFReadEncodedTile(tif, 9, buffer, sz); |
| 215 |
if (szout != sz) |
| 216 |
{ |
| 217 |
fprintf(stderr, |
| 218 |
"Did not get expected result code from " |
| 219 |
"TIFFReadEncodedTile()(%d instead of %d)\n", |
| 220 |
(int)szout, (int)sz); |
| 221 |
return 1; |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* JPEG decoding is inherently inexact, so we can't test for exact |
| 226 |
* pixel values. (Well, if we knew exactly which libjpeg version |
| 227 |
* we were using, and with what settings, we could expect specific |
| 228 |
* values ... but it's not worth the trouble to keep track of.) |
| 229 |
* Hence, use ranges of expected values. The ranges may need to be |
| 230 |
* widened over time as more versions of libjpeg appear. |
| 231 |
*/ |
| 232 |
pixel_status |= check_rgb_pixel(0, 15, 18, 0, 0, 18, 41, buffer); |
| 233 |
pixel_status |= check_rgb_pixel(64, 0, 0, 0, 0, 0, 2, buffer); |
| 234 |
pixel_status |= check_rgb_pixel(512, 5, 6, 34, 36, 182, 196, buffer); |
| 235 |
|
| 236 |
free(buffer); |
| 237 |
|
| 238 |
TIFFClose(tif); |
| 239 |
|
| 240 |
/* |
| 241 |
* Reopen and test reading using the RGBA interface. |
| 242 |
*/ |
| 243 |
tif = TIFFOpen(srcfile, "r"); |
| 244 |
|
| 245 |
sz = 128 * 128 * sizeof(uint32_t); |
| 246 |
rgba_buffer = (uint32_t *)malloc(sz); |
| 247 |
|
| 248 |
if (!TIFFReadRGBATile(tif, 1 * 128, 2 * 128, rgba_buffer)) |
| 249 |
{ |
| 250 |
fprintf(stderr, "TIFFReadRGBATile() returned failure code.\n"); |
| 251 |
return 1; |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* Currently TIFFReadRGBATile() just uses JPEGCOLORMODE_RGB so this |
| 256 |
* trivially matches the last results. Eventually we should actually |
| 257 |
* accomplish it from the YCbCr subsampled buffer ourselves in which |
| 258 |
* case the results may be subtly different but similar. |
| 259 |
*/ |
| 260 |
pixel_status |= |
| 261 |
check_rgba_pixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer); |
| 262 |
pixel_status |= |
| 263 |
check_rgba_pixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer); |
| 264 |
pixel_status |= |
| 265 |
check_rgba_pixel(512, 5, 6, 34, 36, 182, 196, 255, 255, rgba_buffer); |
| 266 |
|
| 267 |
free(rgba_buffer); |
| 268 |
TIFFClose(tif); |
| 269 |
|
| 270 |
if (pixel_status) |
| 271 |
{ |
| 272 |
exit(1); |
| 273 |
} |
| 274 |
|
| 275 |
exit(0); |
| 276 |
} |