~ubuntu-branches/ubuntu/vivid/tiff/vivid-proposed

« back to all changes in this revision

Viewing changes to test/raw_decode.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2012-06-24 13:45:42 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20120624134542-2xxi2i0ytrcddfdx
Tags: 4.0.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: raw_decode.c,v 1.1 2012-06-01 21:04:22 fwarmerdam Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
 
5
 *
 
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.
 
13
 * 
 
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.  
 
17
 * 
 
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 
 
23
 * OF THIS SOFTWARE.
 
24
 */
 
25
 
 
26
/*
 
27
 * TIFF Library
 
28
 *
 
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. 
 
32
 */
 
33
 
 
34
#include "tif_config.h"
 
35
 
 
36
#include <stdio.h>
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
#ifdef HAVE_UNISTD_H 
 
40
# include <unistd.h> 
 
41
#endif 
 
42
 
 
43
#include "tiffio.h"
 
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, unsigned char *expected_cluster ) {
 
50
        unsigned char *target = buffer + cluster*6;
 
51
 
 
52
        if (memcmp(target, expected_cluster, 6) == 0) {
 
53
                return 0;
 
54
        }
 
55
 
 
56
        fprintf( stderr, "Cluster %d did not match expected results.\n", cluster );
 
57
        fprintf( stderr, 
 
58
                 "Expect: %3d %3d   %3d   %3d\n"
 
59
                 "        %3d %3d\n", 
 
60
                 expected_cluster[0], expected_cluster[1],
 
61
                 expected_cluster[4], expected_cluster[5],
 
62
                 expected_cluster[2], expected_cluster[3] );
 
63
        fprintf( stderr, 
 
64
                 "   Got: %3d %3d   %3d   %3d\n"
 
65
                 "        %3d %3d\n", 
 
66
                 target[0], target[1], 
 
67
                 target[4], target[5],
 
68
                 target[2], target[3] );
 
69
        return 1;
 
70
}
 
71
 
 
72
static int check_rgb_pixel( int pixel, int red, int green, int blue, unsigned char *buffer ) {
 
73
        unsigned char *rgb = buffer + 3 * pixel;
 
74
        
 
75
        if( rgb[0] == red && rgb[1] == green && rgb[2] == blue ) {
 
76
                return 0;
 
77
        }
 
78
 
 
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] );
 
82
        return 1;
 
83
}
 
84
 
 
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;
 
89
        
 
90
        if( rgba[0] == red && rgba[1] == green && rgba[2] == blue && rgba[3] == alpha ) {
 
91
                return 0;
 
92
        }
 
93
 
 
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] );
 
97
        return 1;
 
98
}
 
99
 
 
100
int
 
101
main(int argc, char **argv)
 
102
{
 
103
        TIFF            *tif;
 
104
        static const char *srcfile = "images/quad-tile.jpg.tiff";
 
105
        unsigned short h, v;
 
106
        int status;
 
107
        unsigned char *buffer;
 
108
        tsize_t sz, szout;
 
109
 
 
110
        (void) argc;
 
111
        (void) argv;
 
112
 
 
113
        tif = TIFFOpen(srcfile,"r");
 
114
        if ( tif == NULL ) {
 
115
                fprintf( stderr, "Could not open %s\n", srcfile);
 
116
                exit( 1 );
 
117
        }
 
118
 
 
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" );
 
122
                exit(1);
 
123
        }
 
124
 
 
125
        /*
 
126
         * What is the appropriate size of a YCbCr encoded tile?
 
127
         */
 
128
        sz = TIFFTileSize(tif);
 
129
        if( sz != 24576) {
 
130
                fprintf(stderr, "tiles are %d bytes\n", (int)sz);
 
131
                exit(1);
 
132
        }
 
133
 
 
134
        buffer = (unsigned char *) malloc(sz);
 
135
 
 
136
        /*
 
137
         * Read a tile in decompressed form, but still YCbCr subsampled.
 
138
         */
 
139
        szout = TIFFReadEncodedTile(tif,9,buffer,sz);
 
140
        if (szout != sz) {
 
141
                fprintf( stderr, 
 
142
                         "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n", 
 
143
                         (int) szout, (int) sz );
 
144
                return 1;
 
145
        }
 
146
 
 
147
        if( check_cluster( 0, buffer, cluster_0 )
 
148
            || check_cluster( 64, buffer, cluster_64 )
 
149
            || check_cluster( 128, buffer, cluster_128 ) ) {
 
150
                exit(1);
 
151
        }
 
152
        free(buffer);
 
153
 
 
154
        /*
 
155
         * Read a tile using the built-in conversion to RGB format provided by the JPEG library.
 
156
         */
 
157
        TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
 
158
 
 
159
        sz = TIFFTileSize(tif);
 
160
        if( sz != 128*128*3) {
 
161
                fprintf(stderr, "tiles are %d bytes\n", (int)sz);
 
162
                exit(1);
 
163
        }
 
164
 
 
165
        buffer = (unsigned char *) malloc(sz);
 
166
 
 
167
        szout = TIFFReadEncodedTile(tif,9,buffer,sz);
 
168
        if (szout != sz) {
 
169
                fprintf( stderr, 
 
170
                         "Did not get expected result code from TIFFReadEncodedTile()(%d instead of %d)\n", 
 
171
                         (int) szout, (int) sz );
 
172
                return 1;
 
173
        }
 
174
 
 
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 ) ) {
 
178
                exit(1);
 
179
        }       
 
180
 
 
181
        free( buffer );
 
182
 
 
183
        TIFFClose(tif);
 
184
 
 
185
        /*
 
186
         * Reopen and test reading using the RGBA interface.
 
187
         */
 
188
        tif = TIFFOpen(srcfile,"r");
 
189
        
 
190
        sz = 128 * 128 * 4;
 
191
        buffer = (unsigned char *) malloc(sz);
 
192
        
 
193
        if (!TIFFReadRGBATile( tif, 1*128, 2*128, (uint32 *) buffer )) {
 
194
                fprintf( stderr, "TIFFReadRGBATile() returned failure code.\n" );
 
195
                return 1;
 
196
        }
 
197
 
 
198
        /*
 
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.
 
203
         */
 
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 ) ) {
 
207
                exit(1);
 
208
        }       
 
209
 
 
210
        free( buffer );
 
211
        TIFFClose(tif);
 
212
        
 
213
        exit( 0 );
 
214
}
 
215
 
 
216
/* vim: set ts=8 sts=8 sw=8 noet: */