Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/tools/rgb2ycbcr.c

raw
1 /*
2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include "libport.h"
26 #include "tif_config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include "tiffio.h"
37 #include "tiffiop.h"
38
39 #ifndef EXIT_SUCCESS
40 #define EXIT_SUCCESS 0
41 #endif
42 #ifndef EXIT_FAILURE
43 #define EXIT_FAILURE 1
44 #endif
45
46 #define streq(a, b) (strcmp(a, b) == 0)
47 #define CopyField(tag, v) \
48 if (TIFFGetField(in, tag, &v)) \
49 TIFFSetField(out, tag, v)
50
51 #ifndef howmany
52 #define howmany(x, y) (((x) + ((y)-1)) / (y))
53 #endif
54 #define roundup(x, y) (howmany(x, y) * ((uint32_t)(y)))
55
56 #define LumaRed ycbcrCoeffs[0]
57 #define LumaGreen ycbcrCoeffs[1]
58 #define LumaBlue ycbcrCoeffs[2]
59
60 uint16_t compression = COMPRESSION_PACKBITS;
61 uint32_t rowsperstrip = (uint32_t)-1;
62
63 uint16_t horizSubSampling = 2; /* YCbCr horizontal subsampling */
64 uint16_t vertSubSampling = 2; /* YCbCr vertical subsampling */
65 float ycbcrCoeffs[3] = {.299F, .587F, .114F};
66 /* default coding range is CCIR Rec 601-1 with no headroom/footroom */
67 float refBlackWhite[6] = {0.F, 255.F, 128.F, 255.F, 128.F, 255.F};
68
69 static int tiffcvt(TIFF *in, TIFF *out);
70 static void usage(int code);
71 static void setupLumaTables(void);
72
73 int main(int argc, char *argv[])
74 {
75 TIFF *in, *out;
76 int c;
77 #if !HAVE_DECL_OPTARG
78 extern int optind;
79 extern char *optarg;
80 #endif
81
82 while ((c = getopt(argc, argv, "c:h:r:v:z")) != -1)
83 switch (c)
84 {
85 case 'c':
86 if (streq(optarg, "none"))
87 compression = COMPRESSION_NONE;
88 else if (streq(optarg, "packbits"))
89 compression = COMPRESSION_PACKBITS;
90 else if (streq(optarg, "lzw"))
91 compression = COMPRESSION_LZW;
92 else if (streq(optarg, "jpeg"))
93 compression = COMPRESSION_JPEG;
94 else if (streq(optarg, "zip"))
95 compression = COMPRESSION_ADOBE_DEFLATE;
96 else
97 usage(EXIT_FAILURE);
98 break;
99 case 'h':
100 horizSubSampling = atoi(optarg);
101 if (horizSubSampling != 1 && horizSubSampling != 2 &&
102 horizSubSampling != 4)
103 usage(EXIT_FAILURE);
104 break;
105 case 'v':
106 vertSubSampling = atoi(optarg);
107 if (vertSubSampling != 1 && vertSubSampling != 2 &&
108 vertSubSampling != 4)
109 usage(EXIT_FAILURE);
110 break;
111 case 'r':
112 rowsperstrip = atoi(optarg);
113 break;
114 case 'z': /* CCIR Rec 601-1 w/ headroom/footroom */
115 refBlackWhite[0] = 16.;
116 refBlackWhite[1] = 235.;
117 refBlackWhite[2] = 128.;
118 refBlackWhite[3] = 240.;
119 refBlackWhite[4] = 128.;
120 refBlackWhite[5] = 240.;
121 break;
122 case '?':
123 usage(EXIT_FAILURE);
124 /*NOTREACHED*/
125 }
126 if (argc - optind < 2)
127 usage(EXIT_FAILURE);
128 out = TIFFOpen(argv[argc - 1], "w");
129 if (out == NULL)
130 return (EXIT_FAILURE);
131 setupLumaTables();
132 for (; optind < argc - 1; optind++)
133 {
134 in = TIFFOpen(argv[optind], "r");
135 if (in != NULL)
136 {
137 do
138 {
139 if (!tiffcvt(in, out) || !TIFFWriteDirectory(out))
140 {
141 (void)TIFFClose(out);
142 (void)TIFFClose(in);
143 return (1);
144 }
145 } while (TIFFReadDirectory(in));
146 (void)TIFFClose(in);
147 }
148 }
149 (void)TIFFClose(out);
150 return (EXIT_SUCCESS);
151 }
152
153 float *lumaRed;
154 float *lumaGreen;
155 float *lumaBlue;
156 float D1, D2;
157 int Yzero;
158
159 static float *setupLuma(float c)
160 {
161 float *v = (float *)_TIFFmalloc(256 * sizeof(float));
162 int i;
163 for (i = 0; i < 256; i++)
164 v[i] = c * i;
165 return (v);
166 }
167
168 static unsigned V2Code(float f, float RB, float RW, int CR)
169 {
170 unsigned int c = (unsigned int)((((f) * (RW - RB) / CR) + RB) + .5);
171 return (c > 255 ? 255 : c);
172 }
173
174 static void setupLumaTables(void)
175 {
176 lumaRed = setupLuma(LumaRed);
177 lumaGreen = setupLuma(LumaGreen);
178 lumaBlue = setupLuma(LumaBlue);
179 D1 = 1.F / (2.F - 2.F * LumaBlue);
180 D2 = 1.F / (2.F - 2.F * LumaRed);
181 Yzero = V2Code(0, refBlackWhite[0], refBlackWhite[1], 255);
182 }
183
184 static void cvtClump(unsigned char *op, uint32_t *raster, uint32_t ch,
185 uint32_t cw, uint32_t w)
186 {
187 float Y, Cb = 0, Cr = 0;
188 uint32_t j, k;
189 /*
190 * Convert ch-by-cw block of RGB
191 * to YCbCr and sample accordingly.
192 */
193 for (k = 0; k < ch; k++)
194 {
195 for (j = 0; j < cw; j++)
196 {
197 uint32_t RGB = (raster - k * w)[j];
198 Y = lumaRed[TIFFGetR(RGB)] + lumaGreen[TIFFGetG(RGB)] +
199 lumaBlue[TIFFGetB(RGB)];
200 /* accumulate chrominance */
201 Cb += (TIFFGetB(RGB) - Y) * D1;
202 Cr += (TIFFGetR(RGB) - Y) * D2;
203 /* emit luminence */
204 *op++ = V2Code(Y, refBlackWhite[0], refBlackWhite[1], 255);
205 }
206 for (; j < horizSubSampling; j++)
207 *op++ = Yzero;
208 }
209 for (; k < vertSubSampling; k++)
210 {
211 for (j = 0; j < horizSubSampling; j++)
212 *op++ = Yzero;
213 }
214 /* emit sampled chrominance values */
215 *op++ = V2Code(Cb / (ch * cw), refBlackWhite[2], refBlackWhite[3], 127);
216 *op++ = V2Code(Cr / (ch * cw), refBlackWhite[4], refBlackWhite[5], 127);
217 }
218 #undef LumaRed
219 #undef LumaGreen
220 #undef LumaBlue
221 #undef V2Code
222
223 /*
224 * Convert a strip of RGB data to YCbCr and
225 * sample to generate the output data.
226 */
227 static void cvtStrip(unsigned char *op, uint32_t *raster, uint32_t nrows,
228 uint32_t width)
229 {
230 uint32_t x;
231 int clumpSize = vertSubSampling * horizSubSampling + 2;
232 uint32_t *tp;
233
234 for (; nrows >= vertSubSampling; nrows -= vertSubSampling)
235 {
236 tp = raster;
237 for (x = width; x >= horizSubSampling; x -= horizSubSampling)
238 {
239 cvtClump(op, tp, vertSubSampling, horizSubSampling, width);
240 op += clumpSize;
241 tp += horizSubSampling;
242 }
243 if (x > 0)
244 {
245 cvtClump(op, tp, vertSubSampling, x, width);
246 op += clumpSize;
247 }
248 raster -= vertSubSampling * width;
249 }
250 if (nrows > 0)
251 {
252 tp = raster;
253 for (x = width; x >= horizSubSampling; x -= horizSubSampling)
254 {
255 cvtClump(op, tp, nrows, horizSubSampling, width);
256 op += clumpSize;
257 tp += horizSubSampling;
258 }
259 if (x > 0)
260 cvtClump(op, tp, nrows, x, width);
261 }
262 }
263
264 static int cvtRaster(TIFF *tif, uint32_t *raster, uint32_t width,
265 uint32_t height)
266 {
267 uint32_t y;
268 tstrip_t strip = 0;
269 tsize_t cc, acc;
270 unsigned char *buf;
271 uint32_t rwidth = roundup(width, horizSubSampling);
272 uint32_t rheight = roundup(height, vertSubSampling);
273 uint32_t nrows = (rowsperstrip > rheight ? rheight : rowsperstrip);
274 uint32_t rnrows = roundup(nrows, vertSubSampling);
275
276 cc = (tsize_t)rnrows * rwidth +
277 2 * ((rnrows * rwidth) / (horizSubSampling * vertSubSampling));
278 buf = (unsigned char *)_TIFFmalloc(cc);
279 // FIXME unchecked malloc
280 for (y = height; (int32_t)y > 0; y -= nrows)
281 {
282 uint32_t nr = (y > nrows ? nrows : y);
283 cvtStrip(buf, raster + (y - 1) * width, nr, width);
284 nr = roundup(nr, vertSubSampling);
285 acc = (tsize_t)nr * rwidth +
286 2 * ((nr * rwidth) / (horizSubSampling * vertSubSampling));
287 if (!TIFFWriteEncodedStrip(tif, strip++, buf, acc))
288 {
289 _TIFFfree(buf);
290 return (0);
291 }
292 }
293 _TIFFfree(buf);
294 return (1);
295 }
296
297 static int tiffcvt(TIFF *in, TIFF *out)
298 {
299 uint32_t width, height; /* image width & height */
300 uint32_t *raster; /* retrieve RGBA image */
301 uint16_t shortv;
302 float floatv;
303 char *stringv;
304 uint32_t longv;
305 int result;
306 size_t pixel_count;
307
308 TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
309 TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
310 pixel_count = width * height;
311
312 /* XXX: Check the integer overflow. */
313 if (!width || !height || pixel_count / width != height)
314 {
315 TIFFError(TIFFFileName(in),
316 "Malformed input file; "
317 "can't allocate buffer for raster of %" PRIu32 "x%" PRIu32
318 " size",
319 width, height);
320 return 0;
321 }
322
323 raster = (uint32_t *)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32_t),
324 "raster buffer");
325 if (raster == 0)
326 {
327 TIFFError(TIFFFileName(in),
328 "Failed to allocate buffer (%" TIFF_SIZE_FORMAT
329 " elements of %" TIFF_SIZE_FORMAT " each)",
330 pixel_count, sizeof(uint32_t));
331 return (0);
332 }
333
334 if (!TIFFReadRGBAImage(in, width, height, raster, 0))
335 {
336 _TIFFfree(raster);
337 return (0);
338 }
339
340 CopyField(TIFFTAG_SUBFILETYPE, longv);
341 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
342 TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
343 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
344 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
345 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
346 if (compression == COMPRESSION_JPEG)
347 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);
348 CopyField(TIFFTAG_FILLORDER, shortv);
349 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
350 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
351 CopyField(TIFFTAG_XRESOLUTION, floatv);
352 CopyField(TIFFTAG_YRESOLUTION, floatv);
353 CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
354 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
355 {
356 char buf[2048];
357 char *cp = strrchr(TIFFFileName(in), '/');
358 snprintf(buf, sizeof(buf), "YCbCr conversion of %s",
359 cp ? cp + 1 : TIFFFileName(in));
360 TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, buf);
361 }
362 TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
363 CopyField(TIFFTAG_DOCUMENTNAME, stringv);
364
365 TIFFSetField(out, TIFFTAG_REFERENCEBLACKWHITE, refBlackWhite);
366 TIFFSetField(out, TIFFTAG_YCBCRSUBSAMPLING, horizSubSampling,
367 vertSubSampling);
368 TIFFSetField(out, TIFFTAG_YCBCRPOSITIONING, YCBCRPOSITION_CENTERED);
369 TIFFSetField(out, TIFFTAG_YCBCRCOEFFICIENTS, ycbcrCoeffs);
370 rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
371 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
372
373 result = cvtRaster(out, raster, width, height);
374 _TIFFfree(raster);
375 return result;
376 }
377
378 const char *usage_info[] = {
379 /* Help information format modified for the sake of consistency with the
380 other tiff tools */
381 /* "usage: rgb2ycbcr [-c comp] [-r rows] [-h N] [-v N] input...
382 output\n", */
383 /* "where comp is one of the following compression algorithms:\n", */
384 "Convert RGB color, greyscale, or bi-level TIFF images to YCbCr images\n\n"
385 "usage: rgb2ycbcr [options] input output",
386 "where options are:",
387 #ifdef JPEG_SUPPORT
388 " -c jpeg JPEG encoding",
389 #endif
390 #ifdef ZIP_SUPPORT
391 " -c zip Zip/Deflate encoding",
392 #endif
393 #ifdef LZW_SUPPORT
394 " -c lzw Lempel-Ziv & Welch encoding",
395 #endif
396 #ifdef PACKBITS_SUPPORT
397 " -c packbits PackBits encoding (default)",
398 #endif
399 #if defined(JPEG_SUPPORT) || defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || \
400 defined(PACKBITS_SUPPORT)
401 " -c none no compression",
402 #endif
403 "",
404 /* "and the other options are:\n", */
405 " -r rows/strip", " -h horizontal sampling factor (1,2,4)",
406 " -v vertical sampling factor (1,2,4)", NULL};
407
408 static void usage(int code)
409 {
410 int i;
411 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
412
413 fprintf(out, "%s\n\n", TIFFGetVersion());
414 for (i = 0; usage_info[i] != NULL; i++)
415 fprintf(out, "%s\n", usage_info[i]);
416 exit(code);
417 }