1 /* $Id: rgb2ycbcr.c,v 1.9.2.2 2010-06-08 18:50:44 bfriesen Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 #include "tif_config.h"
40 #define streq(a,b) (strcmp(a,b) == 0)
41 #define CopyField(tag, v) \
42 if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
45 #define howmany(x, y) (((x)+((y)-1))/(y))
47 #define roundup(x, y) (howmany(x,y)*((uint32)(y)))
49 #define LumaRed ycbcrCoeffs[0]
50 #define LumaGreen ycbcrCoeffs[1]
51 #define LumaBlue ycbcrCoeffs[2]
53 uint16 compression = COMPRESSION_PACKBITS;
54 uint32 rowsperstrip = (uint32) -1;
56 uint16 horizSubSampling = 2; /* YCbCr horizontal subsampling */
57 uint16 vertSubSampling = 2; /* YCbCr vertical subsampling */
58 float ycbcrCoeffs[3] = { .299F, .587F, .114F };
59 /* default coding range is CCIR Rec 601-1 with no headroom/footroom */
60 float refBlackWhite[6] = { 0.F, 255.F, 128.F, 255.F, 128.F, 255.F };
62 static int tiffcvt(TIFF* in, TIFF* out);
63 static void usage(int code);
64 static void setupLumaTables(void);
67 main(int argc, char* argv[])
74 while ((c = getopt(argc, argv, "c:h:r:v:z")) != -1)
77 if (streq(optarg, "none"))
78 compression = COMPRESSION_NONE;
79 else if (streq(optarg, "packbits"))
80 compression = COMPRESSION_PACKBITS;
81 else if (streq(optarg, "lzw"))
82 compression = COMPRESSION_LZW;
83 else if (streq(optarg, "jpeg"))
84 compression = COMPRESSION_JPEG;
85 else if (streq(optarg, "zip"))
86 compression = COMPRESSION_ADOBE_DEFLATE;
91 horizSubSampling = atoi(optarg);
94 vertSubSampling = atoi(optarg);
97 rowsperstrip = atoi(optarg);
99 case 'z': /* CCIR Rec 601-1 w/ headroom/footroom */
100 refBlackWhite[0] = 16.;
101 refBlackWhite[1] = 235.;
102 refBlackWhite[2] = 128.;
103 refBlackWhite[3] = 240.;
104 refBlackWhite[4] = 128.;
105 refBlackWhite[5] = 240.;
111 if (argc - optind < 2)
113 out = TIFFOpen(argv[argc-1], "w");
117 for (; optind < argc-1; optind++) {
118 in = TIFFOpen(argv[optind], "r");
121 if (!tiffcvt(in, out) ||
122 !TIFFWriteDirectory(out)) {
123 (void) TIFFClose(out);
126 } while (TIFFReadDirectory(in));
127 (void) TIFFClose(in);
130 (void) TIFFClose(out);
143 float *v = (float *)_TIFFmalloc(256 * sizeof (float));
145 for (i = 0; i < 256; i++)
151 V2Code(float f, float RB, float RW, int CR)
153 unsigned int c = (unsigned int)((((f)*(RW-RB)/CR)+RB)+.5);
154 return (c > 255 ? 255 : c);
158 setupLumaTables(void)
160 lumaRed = setupLuma(LumaRed);
161 lumaGreen = setupLuma(LumaGreen);
162 lumaBlue = setupLuma(LumaBlue);
163 D1 = 1.F/(2.F - 2.F*LumaBlue);
164 D2 = 1.F/(2.F - 2.F*LumaRed);
165 Yzero = V2Code(0, refBlackWhite[0], refBlackWhite[1], 255);
169 cvtClump(unsigned char* op, uint32* raster, uint32 ch, uint32 cw, uint32 w)
171 float Y, Cb = 0, Cr = 0;
174 * Convert ch-by-cw block of RGB
175 * to YCbCr and sample accordingly.
177 for (k = 0; k < ch; k++) {
178 for (j = 0; j < cw; j++) {
179 uint32 RGB = (raster - k*w)[j];
180 Y = lumaRed[TIFFGetR(RGB)] +
181 lumaGreen[TIFFGetG(RGB)] +
182 lumaBlue[TIFFGetB(RGB)];
183 /* accumulate chrominance */
184 Cb += (TIFFGetB(RGB) - Y) * D1;
185 Cr += (TIFFGetR(RGB) - Y) * D2;
188 refBlackWhite[0], refBlackWhite[1], 255);
190 for (; j < horizSubSampling; j++)
193 for (; k < vertSubSampling; k++) {
194 for (j = 0; j < horizSubSampling; j++)
197 /* emit sampled chrominance values */
198 *op++ = V2Code(Cb / (ch*cw), refBlackWhite[2], refBlackWhite[3], 127);
199 *op++ = V2Code(Cr / (ch*cw), refBlackWhite[4], refBlackWhite[5], 127);
207 * Convert a strip of RGB data to YCbCr and
208 * sample to generate the output data.
211 cvtStrip(unsigned char* op, uint32* raster, uint32 nrows, uint32 width)
214 int clumpSize = vertSubSampling * horizSubSampling + 2;
217 for (; nrows >= vertSubSampling; nrows -= vertSubSampling) {
219 for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
221 vertSubSampling, horizSubSampling, width);
223 tp += horizSubSampling;
226 cvtClump(op, tp, vertSubSampling, x, width);
229 raster -= vertSubSampling*width;
233 for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
234 cvtClump(op, tp, nrows, horizSubSampling, width);
236 tp += horizSubSampling;
239 cvtClump(op, tp, nrows, x, width);
244 cvtRaster(TIFF* tif, uint32* raster, uint32 width, uint32 height)
250 uint32 rwidth = roundup(width, horizSubSampling);
251 uint32 rheight = roundup(height, vertSubSampling);
252 uint32 nrows = (rowsperstrip > rheight ? rheight : rowsperstrip);
253 uint32 rnrows = roundup(nrows,vertSubSampling);
256 2*((rnrows*rwidth) / (horizSubSampling*vertSubSampling));
257 buf = (unsigned char*)_TIFFmalloc(cc);
258 for (y = height; (int32) y > 0; y -= nrows) {
259 uint32 nr = (y > nrows ? nrows : y);
260 cvtStrip(buf, raster + (y-1)*width, nr, width);
261 nr = roundup(nr, vertSubSampling);
263 2*((nr*rwidth)/(horizSubSampling*vertSubSampling));
264 if (!TIFFWriteEncodedStrip(tif, strip++, buf, acc)) {
274 tiffcvt(TIFF* in, TIFF* out)
276 uint32 width, height; /* image width & height */
277 uint32* raster; /* retrieve RGBA image */
284 TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
285 TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
286 pixel_count = width * height;
288 /* XXX: Check the integer overflow. */
289 if (!width || !height || pixel_count / width != height) {
290 TIFFError(TIFFFileName(in),
291 "Malformed input file; "
292 "can't allocate buffer for raster of %lux%lu size",
293 (unsigned long)width, (unsigned long)height);
297 raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32),
300 TIFFError(TIFFFileName(in),
301 "Requested buffer size is %lu elements %lu each",
302 (unsigned long)pixel_count,
303 (unsigned long)sizeof(uint32));
307 if (!TIFFReadRGBAImage(in, width, height, raster, 0)) {
312 CopyField(TIFFTAG_SUBFILETYPE, longv);
313 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
314 TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
315 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
316 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
317 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
318 if (compression == COMPRESSION_JPEG)
319 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);
320 CopyField(TIFFTAG_FILLORDER, shortv);
321 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
322 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
323 CopyField(TIFFTAG_XRESOLUTION, floatv);
324 CopyField(TIFFTAG_YRESOLUTION, floatv);
325 CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
326 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
328 char *cp = strrchr(TIFFFileName(in), '/');
329 sprintf(buf, "YCbCr conversion of %s", cp ? cp+1 : TIFFFileName(in));
330 TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, buf);
332 TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
333 CopyField(TIFFTAG_DOCUMENTNAME, stringv);
335 TIFFSetField(out, TIFFTAG_REFERENCEBLACKWHITE, refBlackWhite);
336 TIFFSetField(out, TIFFTAG_YCBCRSUBSAMPLING,
337 horizSubSampling, vertSubSampling);
338 TIFFSetField(out, TIFFTAG_YCBCRPOSITIONING, YCBCRPOSITION_CENTERED);
339 TIFFSetField(out, TIFFTAG_YCBCRCOEFFICIENTS, ycbcrCoeffs);
340 rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
341 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
343 return (cvtRaster(out, raster, width, height));
347 "usage: rgb2ycbcr [-c comp] [-r rows] [-h N] [-v N] input... output\n",
348 "where comp is one of the following compression algorithms:\n",
349 " jpeg\t\tJPEG encoding\n",
350 " lzw\t\tLempel-Ziv & Welch encoding\n",
351 " zip\t\tdeflate encoding\n",
352 " packbits\tPackBits encoding (default)\n",
353 " none\t\tno compression\n",
354 "and the other options are:\n",
356 " -h\thorizontal sampling factor (1,2,4)\n",
357 " -v\tvertical sampling factor (1,2,4)\n",
369 fprintf(stderr, "%s\n\n", TIFFGetVersion());
370 for (i = 0; stuff[i] != NULL; i++)
371 fprintf(stderr, "%s\n", stuff[i]);
375 /* vim: set ts=8 sts=8 sw=8 noet: */