| 1 |
/* |
| 2 |
* Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com> |
| 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 |
* Module to test TIFFDeferStrileArrayWriting and TIFFForceStrileArrayWriting |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "tif_config.h" |
| 31 |
|
| 32 |
#include <assert.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#ifdef HAVE_UNISTD_H |
| 37 |
#include <unistd.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "tiffio.h" |
| 41 |
|
| 42 |
int test(const char *mode, int tiled, int height) |
| 43 |
{ |
| 44 |
const char *filename = "defer_strile_writing.tif"; |
| 45 |
TIFF *tif; |
| 46 |
int i; |
| 47 |
int ret = 0; |
| 48 |
(void)ret; |
| 49 |
|
| 50 |
tif = TIFFOpen(filename, mode); |
| 51 |
if (!tif) |
| 52 |
{ |
| 53 |
fprintf(stderr, "cannot create %s\n", filename); |
| 54 |
return 1; |
| 55 |
} |
| 56 |
ret = TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 57 |
assert(ret); |
| 58 |
ret = TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1); |
| 59 |
assert(ret); |
| 60 |
ret = TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); |
| 61 |
assert(ret); |
| 62 |
ret = TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 63 |
assert(ret); |
| 64 |
ret = TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 65 |
assert(ret); |
| 66 |
ret = TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 67 |
assert(ret); |
| 68 |
|
| 69 |
if (tiled) |
| 70 |
{ |
| 71 |
ret = TIFFSetField(tif, TIFFTAG_TILEWIDTH, 16); |
| 72 |
assert(ret); |
| 73 |
ret = TIFFSetField(tif, TIFFTAG_TILELENGTH, 16); |
| 74 |
assert(ret); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
ret = TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); |
| 79 |
assert(ret); |
| 80 |
} |
| 81 |
|
| 82 |
ret = TIFFDeferStrileArrayWriting(tif); |
| 83 |
assert(ret); |
| 84 |
|
| 85 |
ret = TIFFWriteCheck(tif, tiled, "test"); |
| 86 |
assert(ret); |
| 87 |
|
| 88 |
ret = TIFFWriteDirectory(tif); |
| 89 |
assert(ret); |
| 90 |
|
| 91 |
/* Create other directory */ |
| 92 |
TIFFFreeDirectory(tif); |
| 93 |
TIFFCreateDirectory(tif); |
| 94 |
|
| 95 |
ret = TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE); |
| 96 |
assert(ret); |
| 97 |
ret = TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 98 |
assert(ret); |
| 99 |
ret = TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1); |
| 100 |
assert(ret); |
| 101 |
ret = TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1); |
| 102 |
assert(ret); |
| 103 |
ret = TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 104 |
assert(ret); |
| 105 |
ret = TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 106 |
assert(ret); |
| 107 |
ret = TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 108 |
assert(ret); |
| 109 |
ret = TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); |
| 110 |
assert(ret); |
| 111 |
|
| 112 |
ret = TIFFDeferStrileArrayWriting(tif); |
| 113 |
assert(ret); |
| 114 |
|
| 115 |
ret = TIFFWriteCheck(tif, 0, "test"); |
| 116 |
assert(ret); |
| 117 |
|
| 118 |
ret = TIFFWriteDirectory(tif); |
| 119 |
assert(ret); |
| 120 |
|
| 121 |
/* Force writing of strile arrays */ |
| 122 |
ret = TIFFSetDirectory(tif, 0); |
| 123 |
assert(ret); |
| 124 |
|
| 125 |
ret = TIFFForceStrileArrayWriting(tif); |
| 126 |
assert(ret); |
| 127 |
|
| 128 |
ret = TIFFSetDirectory(tif, 1); |
| 129 |
assert(ret); |
| 130 |
|
| 131 |
ret = TIFFForceStrileArrayWriting(tif); |
| 132 |
assert(ret); |
| 133 |
|
| 134 |
/* Now write data on first directory */ |
| 135 |
ret = TIFFSetDirectory(tif, 0); |
| 136 |
assert(ret); |
| 137 |
|
| 138 |
if (tiled) |
| 139 |
{ |
| 140 |
int j; |
| 141 |
for (j = 0; j < (height + 15) / 16; j++) |
| 142 |
{ |
| 143 |
unsigned char tilebuffer[256]; |
| 144 |
memset(tilebuffer, (unsigned char)j, 256); |
| 145 |
ret = TIFFWriteEncodedTile(tif, j, tilebuffer, 256); |
| 146 |
assert(ret == 256); |
| 147 |
} |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
for (i = 0; i < height; i++) |
| 152 |
{ |
| 153 |
unsigned char c = (unsigned char)i; |
| 154 |
ret = TIFFWriteEncodedStrip(tif, i, &c, 1); |
| 155 |
assert(ret == 1); |
| 156 |
|
| 157 |
if (i == 1 && height > 100000) |
| 158 |
i = height - 2; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
TIFFClose(tif); |
| 163 |
|
| 164 |
tif = TIFFOpen(filename, "r"); |
| 165 |
if (!tif) |
| 166 |
{ |
| 167 |
fprintf(stderr, "cannot open %s\n", filename); |
| 168 |
return 1; |
| 169 |
} |
| 170 |
if (tiled) |
| 171 |
{ |
| 172 |
int j; |
| 173 |
for (j = 0; j < (height + 15) / 16; j++) |
| 174 |
{ |
| 175 |
int retry; |
| 176 |
for (retry = 0; retry < 2; retry++) |
| 177 |
{ |
| 178 |
unsigned char tilebuffer[256]; |
| 179 |
unsigned char expected_c = (unsigned char)j; |
| 180 |
memset(tilebuffer, 0, 256); |
| 181 |
ret = TIFFReadEncodedTile(tif, j, tilebuffer, 256); |
| 182 |
assert(ret == 256); |
| 183 |
if (tilebuffer[0] != expected_c || |
| 184 |
tilebuffer[255] != expected_c) |
| 185 |
{ |
| 186 |
fprintf(stderr, "unexpected value at tile %d: %d %d\n", j, |
| 187 |
tilebuffer[0], tilebuffer[255]); |
| 188 |
TIFFClose(tif); |
| 189 |
return 1; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
int j; |
| 197 |
for (j = 0; j < height; j++) |
| 198 |
{ |
| 199 |
int retry; |
| 200 |
for (retry = 0; retry < 2; retry++) |
| 201 |
{ |
| 202 |
unsigned char c = 0; |
| 203 |
unsigned char expected_c = (unsigned char)j; |
| 204 |
ret = TIFFReadEncodedStrip(tif, j, &c, 1); |
| 205 |
assert(ret == 1); |
| 206 |
if (c != expected_c) |
| 207 |
{ |
| 208 |
fprintf(stderr, "unexpected value at line %d: %d\n", j, c); |
| 209 |
TIFFClose(tif); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
TIFFClose(tif); |
| 217 |
|
| 218 |
unlink(filename); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
int main() |
| 223 |
{ |
| 224 |
int tiled; |
| 225 |
for (tiled = 0; tiled <= 1; tiled++) |
| 226 |
{ |
| 227 |
if (test("w", tiled, 1)) |
| 228 |
return 1; |
| 229 |
if (test("w", tiled, 10)) |
| 230 |
return 1; |
| 231 |
if (test("w8", tiled, 1)) |
| 232 |
return 1; |
| 233 |
if (test("wD", tiled, 1)) |
| 234 |
return 1; |
| 235 |
} |
| 236 |
return 0; |
| 237 |
} |