|
roentgen |
b75cab |
/* $Id: raw_decode.c,v 1.4 2012-07-06 17:05:16 bfriesen Exp $ */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com></warmerdam@pobox.com>
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
roentgen |
b75cab |
* its documentation for any purpose is hereby granted without fee, provided
|
|
roentgen |
b75cab |
* that (i) the above copyright notices and this permission notice appear in
|
|
roentgen |
b75cab |
* all copies of the software and related documentation, and (ii) the names of
|
|
roentgen |
b75cab |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
roentgen |
b75cab |
* publicity relating to the software without the specific, prior written
|
|
roentgen |
b75cab |
* permission of Sam Leffler and Silicon Graphics.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
roentgen |
b75cab |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
roentgen |
b75cab |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
roentgen |
b75cab |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
roentgen |
b75cab |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
roentgen |
b75cab |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
roentgen |
b75cab |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
roentgen |
b75cab |
* OF THIS SOFTWARE.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* TIFF Library
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* The objective of this test suite is to test the JPEGRawDecode()
|
|
roentgen |
b75cab |
* interface via TIFReadEncodedTile(). This function with YCbCr subsampling
|
|
roentgen |
b75cab |
* is a frequent source of bugs.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include "tif_config.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include <stdio.h></stdio.h>
|
|
roentgen |
b75cab |
#include <stdlib.h></stdlib.h>
|
|
roentgen |
b75cab |
#include <string.h></string.h>
|
|
roentgen |
b75cab |
#ifdef HAVE_UNISTD_H
|
|
roentgen |
b75cab |
# include <unistd.h> </unistd.h>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include "tiffio.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static unsigned char cluster_0[] = { 0, 0, 2, 0, 138, 139 };
|
|
roentgen |
b75cab |
static unsigned char cluster_64[] = { 0, 0, 9, 6, 134, 119 };
|
|
roentgen |
b75cab |
static unsigned char cluster_128[] = { 44, 40, 63, 59, 230, 95 };
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int check_cluster( int cluster, unsigned char *buffer, unsigned char *expected_cluster ) {
|
|
roentgen |
b75cab |
unsigned char *target = buffer + cluster*6;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (memcmp(target, expected_cluster, 6) == 0) {
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf( stderr, "Cluster %d did not match expected results.\n", cluster );
|
|
roentgen |
b75cab |
fprintf( stderr,
|
|
roentgen |
b75cab |
"Expect: %3d %3d %3d %3d\n"
|
|
roentgen |
b75cab |
" %3d %3d\n",
|
|
roentgen |
b75cab |
expected_cluster[0], expected_cluster[1],
|
|
roentgen |
b75cab |
expected_cluster[4], expected_cluster[5],
|
|
roentgen |
b75cab |
expected_cluster[2], expected_cluster[3] );
|
|
roentgen |
b75cab |
fprintf( stderr,
|
|
roentgen |
b75cab |
" Got: %3d %3d %3d %3d\n"
|
|
roentgen |
b75cab |
" %3d %3d\n",
|
|
roentgen |
b75cab |
target[0], target[1],
|
|
roentgen |
b75cab |
target[4], target[5],
|
|
roentgen |
b75cab |
target[2], target[3] );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int check_rgb_pixel( int pixel, int red, int green, int blue, unsigned char *buffer ) {
|
|
roentgen |
b75cab |
unsigned char *rgb = buffer + 3 * pixel;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if( rgb[0] == red && rgb[1] == green && rgb[2] == blue ) {
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf( stderr, "Pixel %d did not match expected results.\n", pixel );
|
|
roentgen |
b75cab |
fprintf( stderr, "Expect: %3d %3d %3d\n", red, green, blue );
|
|
roentgen |
b75cab |
fprintf( stderr, " Got: %3d %3d %3d\n", rgb[0], rgb[1], rgb[2] );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int check_rgba_pixel( int pixel, int red, int green, int blue, int alpha, uint32 *buffer ) {
|
|
roentgen |
b75cab |
/* RGBA images are upside down - adjust for normal ordering */
|
|
roentgen |
b75cab |
int adjusted_pixel = pixel % 128 + (127 - (pixel/128)) * 128;
|
|
roentgen |
b75cab |
uint32 rgba = buffer[adjusted_pixel];
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if( TIFFGetR(rgba) == (uint32) red && TIFFGetG(rgba) == (uint32) green &&
|
|
roentgen |
b75cab |
TIFFGetB(rgba) == (uint32) blue && TIFFGetA(rgba) == (uint32) alpha ) {
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf( stderr, "Pixel %d did not match expected results.\n", pixel );
|
|
roentgen |
b75cab |
fprintf( stderr, "Expect: %3d %3d %3d %3d\n", red, green, blue, alpha );
|
|
roentgen |
b75cab |
fprintf( stderr, " Got: %3d %3d %3d %3d\n",
|
|
roentgen |
b75cab |
TIFFGetR(rgba), TIFFGetG(rgba), TIFFGetB(rgba), TIFFGetA(rgba) );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
main(int argc, char **argv)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
TIFF *tif;
|
|
roentgen |
b75cab |
static const char *srcfilerel = "images/quad-tile.jpg.tiff";
|
|
roentgen |
b75cab |
char *srcdir = NULL;
|
|
roentgen |
b75cab |
char srcfile[1024];
|
|
roentgen |
b75cab |
unsigned short h, v;
|
|
roentgen |
b75cab |
int status;
|
|
roentgen |
b75cab |
unsigned char *buffer;
|
|
roentgen |
b75cab |
uint32 *rgba_buffer;
|
|
roentgen |
b75cab |
tsize_t sz, szout;
|
|
roentgen |
b75cab |
unsigned int pixel_status = 0;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
(void) argc;
|
|
roentgen |
b75cab |
(void) argv;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if ((srcdir = getenv("srcdir")) == NULL) {
|
|
roentgen |
b75cab |
srcdir = ".";
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if ((strlen(srcdir) + 1 + strlen(srcfilerel)) >= sizeof(srcfile)) {
|
|
roentgen |
b75cab |
fprintf( stderr, "srcdir too long %s\n", srcdir);
|
|
roentgen |
b75cab |
exit( 1 );
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
strcpy(srcfile,srcdir);
|
|
roentgen |
b75cab |
strcat(srcfile,"/");
|
|
roentgen |
b75cab |
strcat(srcfile,srcfilerel);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
tif = TIFFOpen(srcfile,"r");
|
|
roentgen |
b75cab |
if ( tif == NULL ) {
|
|
roentgen |
b75cab |
fprintf( stderr, "Could not open %s\n", srcfile);
|
|
roentgen |
b75cab |
exit( 1 );
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
status = TIFFGetField(tif,TIFFTAG_YCBCRSUBSAMPLING, &h, &v);
|
|
roentgen |
b75cab |
if ( status == 0 || h != 2 || v != 2) {
|
|
roentgen |
b75cab |
fprintf( stderr, "Could not retrieve subsampling tag.\n" );
|
|
roentgen |
b75cab |
exit(1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* What is the appropriate size of a YCbCr encoded tile?
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
sz = TIFFTileSize(tif);
|
|
roentgen |
b75cab |
if( sz != 24576) {
|
|
roentgen |
b75cab |
fprintf(stderr, "tiles are %d bytes\n", (int)sz);
|
|
roentgen |
b75cab |
exit(1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
buffer = (unsigned char *) malloc(sz);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Read a tile in decompressed form, but still YCbCr subsampled.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
szout = TIFFReadEncodedTile(tif,9,buffer,sz);
|
|
roentgen |
b75cab |
if (szout != sz) {
|
|
roentgen |
b75cab |
fprintf( stderr,
|
|
roentgen |
b75cab |
"Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
|
|
roentgen |
b75cab |
(int) szout, (int) sz );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if( check_cluster( 0, buffer, cluster_0 )
|
|
roentgen |
b75cab |
|| check_cluster( 64, buffer, cluster_64 )
|
|
roentgen |
b75cab |
|| check_cluster( 128, buffer, cluster_128 ) ) {
|
|
roentgen |
b75cab |
exit(1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
free(buffer);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Read a tile using the built-in conversion to RGB format provided by the JPEG library.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
sz = TIFFTileSize(tif);
|
|
roentgen |
b75cab |
if( sz != 128*128*3) {
|
|
roentgen |
b75cab |
fprintf(stderr, "tiles are %d bytes\n", (int)sz);
|
|
roentgen |
b75cab |
exit(1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
buffer = (unsigned char *) malloc(sz);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
szout = TIFFReadEncodedTile(tif,9,buffer,sz);
|
|
roentgen |
b75cab |
if (szout != sz) {
|
|
roentgen |
b75cab |
fprintf( stderr,
|
|
roentgen |
b75cab |
"Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n",
|
|
roentgen |
b75cab |
(int) szout, (int) sz );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#if JPEG_LIB_VERSION >= 70
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 0, 18, 0, 41, buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 64, 0, 0, 0, buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 512, 5, 34, 196, buffer );
|
|
roentgen |
b75cab |
#else
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 0, 15, 0, 18, buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 64, 0, 0, 2, buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgb_pixel( 512, 6, 36, 182, buffer );
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
free( buffer );
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
TIFFClose(tif);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Reopen and test reading using the RGBA interface.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
tif = TIFFOpen(srcfile,"r");
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
sz = 128 * 128 * sizeof(uint32);
|
|
roentgen |
b75cab |
rgba_buffer = (uint32 *) malloc(sz);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (!TIFFReadRGBATile( tif, 1*128, 2*128, rgba_buffer )) {
|
|
roentgen |
b75cab |
fprintf( stderr, "TIFFReadRGBATile() returned failure code.\n" );
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Currently TIFFReadRGBATile() just uses JPEGCOLORMODE_RGB so this
|
|
roentgen |
b75cab |
* trivally matches the last results. Eventually we should actually
|
|
roentgen |
b75cab |
* accomplish it from the YCbCr subsampled buffer ourselves in which
|
|
roentgen |
b75cab |
* case the results may be subtly different but similar.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
#if JPEG_LIB_VERSION >= 70
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 0, 18, 0, 41, 255, rgba_buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 64, 0, 0, 0, 255, rgba_buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 512, 5, 34, 196, 255, rgba_buffer );
|
|
roentgen |
b75cab |
#else
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 0, 15, 0, 18, 255, rgba_buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 64, 0, 0, 2, 255, rgba_buffer );
|
|
roentgen |
b75cab |
pixel_status |= check_rgba_pixel( 512, 6, 36, 182, 255, rgba_buffer );
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
free( rgba_buffer );
|
|
roentgen |
b75cab |
TIFFClose(tif);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (pixel_status) {
|
|
roentgen |
b75cab |
exit(1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
exit( 0 );
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/* vim: set ts=8 sts=8 sw=8 noet: */
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Local Variables:
|
|
roentgen |
b75cab |
* mode: c
|
|
roentgen |
b75cab |
* c-basic-offset: 8
|
|
roentgen |
b75cab |
* fill-column: 78
|
|
roentgen |
b75cab |
* End:
|
|
roentgen |
b75cab |
*/
|