1
/* $Id: raw_decode.c,v 1.1 2012-06-01 21:04:22 fwarmerdam Exp $ */
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
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 };
49
static int check_cluster( int cluster, unsigned char *buffer, unsigned char *expected_cluster ) {
50
unsigned char *target = buffer + cluster*6;
52
if (memcmp(target, expected_cluster, 6) == 0) {
56
fprintf( stderr, "Cluster %d did not match expected results.\n", cluster );
58
"Expect: %3d %3d %3d %3d\n"
60
expected_cluster[0], expected_cluster[1],
61
expected_cluster[4], expected_cluster[5],
62
expected_cluster[2], expected_cluster[3] );
64
" Got: %3d %3d %3d %3d\n"
68
target[2], target[3] );
72
static int check_rgb_pixel( int pixel, int red, int green, int blue, unsigned char *buffer ) {
73
unsigned char *rgb = buffer + 3 * pixel;
75
if( rgb[0] == red && rgb[1] == green && rgb[2] == blue ) {
79
fprintf( stderr, "Pixel %d did not match expected results.\n", pixel );
80
fprintf( stderr, "Expect: %3d %3d %3d\n", red, green, blue );
81
fprintf( stderr, " Got: %3d %3d %3d\n", rgb[0], rgb[1], rgb[2] );
85
static int check_rgba_pixel( int pixel, int red, int green, int blue, int alpha, unsigned char *buffer ) {
86
/* RGBA images are upside down - adjust for normal ordering */
87
int adjusted_pixel = pixel % 128 + (127 - (pixel/128)) * 128;
88
unsigned char *rgba = buffer + 4 * adjusted_pixel;
90
if( rgba[0] == red && rgba[1] == green && rgba[2] == blue && rgba[3] == alpha ) {
94
fprintf( stderr, "Pixel %d did not match expected results.\n", pixel );
95
fprintf( stderr, "Expect: %3d %3d %3d %3d\n", red, green, blue, alpha );
96
fprintf( stderr, " Got: %3d %3d %3d %3d\n", rgba[0], rgba[1], rgba[2], rgba[3] );
101
main(int argc, char **argv)
104
static const char *srcfile = "images/quad-tile.jpg.tiff";
107
unsigned char *buffer;
113
tif = TIFFOpen(srcfile,"r");
115
fprintf( stderr, "Could not open %s\n", srcfile);
119
status = TIFFGetField(tif,TIFFTAG_YCBCRSUBSAMPLING, &h, &v);
120
if ( status == 0 || h != 2 || v != 2) {
121
fprintf( stderr, "Could not retrieve subsampling tag.\n" );
126
* What is the appropriate size of a YCbCr encoded tile?
128
sz = TIFFTileSize(tif);
130
fprintf(stderr, "tiles are %d bytes\n", (int)sz);
134
buffer = (unsigned char *) malloc(sz);
137
* Read a tile in decompressed form, but still YCbCr subsampled.
139
szout = TIFFReadEncodedTile(tif,9,buffer,sz);
142
"Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
143
(int) szout, (int) sz );
147
if( check_cluster( 0, buffer, cluster_0 )
148
|| check_cluster( 64, buffer, cluster_64 )
149
|| check_cluster( 128, buffer, cluster_128 ) ) {
155
* Read a tile using the built-in conversion to RGB format provided by the JPEG library.
157
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
159
sz = TIFFTileSize(tif);
160
if( sz != 128*128*3) {
161
fprintf(stderr, "tiles are %d bytes\n", (int)sz);
165
buffer = (unsigned char *) malloc(sz);
167
szout = TIFFReadEncodedTile(tif,9,buffer,sz);
170
"Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
171
(int) szout, (int) sz );
175
if (check_rgb_pixel( 0, 15, 0, 18, buffer )
176
|| check_rgb_pixel( 64, 0, 0, 2, buffer )
177
|| check_rgb_pixel( 512, 6, 36, 182, buffer ) ) {
186
* Reopen and test reading using the RGBA interface.
188
tif = TIFFOpen(srcfile,"r");
191
buffer = (unsigned char *) malloc(sz);
193
if (!TIFFReadRGBATile( tif, 1*128, 2*128, (uint32 *) buffer )) {
194
fprintf( stderr, "TIFFReadRGBATile() returned failure code.\n" );
199
* Currently TIFFReadRGBATile() just uses JPEGCOLORMODE_RGB so this
200
* trivally matches the last results. Eventually we should actually
201
* accomplish it from the YCbCr subsampled buffer ourselves in which
202
* case the results may be subtly different but similar.
204
if (check_rgba_pixel( 0, 15, 0, 18, 255, buffer )
205
|| check_rgba_pixel( 64, 0, 0, 2, 255, buffer )
206
|| check_rgba_pixel( 512, 6, 36, 182, 255, buffer ) ) {
216
/* vim: set ts=8 sts=8 sw=8 noet: */