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 "tif_config.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 
35 #ifdef NEED_LIBPORT
36 # include "libport.h"
37 #endif
38 
39 #include "tiffiop.h"
40 #include "tiffio.h"
41 
42 #define	streq(a,b)	(strcmp(a,b) == 0)
43 #define	CopyField(tag, v) \
44     if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
45 
46 #ifndef howmany
47 #define	howmany(x, y)	(((x)+((y)-1))/(y))
48 #endif
49 #define	roundup(x, y)	(howmany(x,y)*((uint32)(y)))
50 
51 #define	LumaRed		ycbcrCoeffs[0]
52 #define	LumaGreen	ycbcrCoeffs[1]
53 #define	LumaBlue	ycbcrCoeffs[2]
54 
55 uint16	compression = COMPRESSION_PACKBITS;
56 uint32	rowsperstrip = (uint32) -1;
57 
58 uint16	horizSubSampling = 2;		/* YCbCr horizontal subsampling */
59 uint16	vertSubSampling = 2;		/* YCbCr vertical subsampling */
60 float	ycbcrCoeffs[3] = { .299F, .587F, .114F };
61 /* default coding range is CCIR Rec 601-1 with no headroom/footroom */
62 float	refBlackWhite[6] = { 0.F, 255.F, 128.F, 255.F, 128.F, 255.F };
63 
64 static	int tiffcvt(TIFF* in, TIFF* out);
65 static	void usage(int code);
66 static	void setupLumaTables(void);
67 
68 int
main(int argc,char * argv[])69 main(int argc, char* argv[])
70 {
71 	TIFF *in, *out;
72 	int c;
73 #if !HAVE_DECL_OPTARG
74 	extern int optind;
75 	extern char *optarg;
76 #endif
77 
78 	while ((c = getopt(argc, argv, "c:h:r:v:z")) != -1)
79 		switch (c) {
80 		case 'c':
81 			if (streq(optarg, "none"))
82 			    compression = COMPRESSION_NONE;
83 			else if (streq(optarg, "packbits"))
84 			    compression = COMPRESSION_PACKBITS;
85 			else if (streq(optarg, "lzw"))
86 			    compression = COMPRESSION_LZW;
87 			else if (streq(optarg, "jpeg"))
88 			    compression = COMPRESSION_JPEG;
89 			else if (streq(optarg, "zip"))
90 			    compression = COMPRESSION_ADOBE_DEFLATE;
91 			else
92 			    usage(-1);
93 			break;
94 		case 'h':
95 			horizSubSampling = atoi(optarg);
96             if( horizSubSampling != 1 && horizSubSampling != 2 && horizSubSampling != 4 )
97                 usage(-1);
98 			break;
99 		case 'v':
100 			vertSubSampling = atoi(optarg);
101             if( vertSubSampling != 1 && vertSubSampling != 2 && vertSubSampling != 4 )
102                 usage(-1);
103 			break;
104 		case 'r':
105 			rowsperstrip = atoi(optarg);
106 			break;
107 		case 'z':	/* CCIR Rec 601-1 w/ headroom/footroom */
108 			refBlackWhite[0] = 16.;
109 			refBlackWhite[1] = 235.;
110 			refBlackWhite[2] = 128.;
111 			refBlackWhite[3] = 240.;
112 			refBlackWhite[4] = 128.;
113 			refBlackWhite[5] = 240.;
114 			break;
115 		case '?':
116 			usage(0);
117 			/*NOTREACHED*/
118 		}
119 	if (argc - optind < 2)
120 		usage(-1);
121 	out = TIFFOpen(argv[argc-1], "w");
122 	if (out == NULL)
123 		return (-2);
124 	setupLumaTables();
125 	for (; optind < argc-1; optind++) {
126 		in = TIFFOpen(argv[optind], "r");
127 		if (in != NULL) {
128 			do {
129 				if (!tiffcvt(in, out) ||
130 				    !TIFFWriteDirectory(out)) {
131 					(void) TIFFClose(out);
132 					return (1);
133 				}
134 			} while (TIFFReadDirectory(in));
135 			(void) TIFFClose(in);
136 		}
137 	}
138 	(void) TIFFClose(out);
139 	return (0);
140 }
141 
142 float	*lumaRed;
143 float	*lumaGreen;
144 float	*lumaBlue;
145 float	D1, D2;
146 int	Yzero;
147 
148 static float*
setupLuma(float c)149 setupLuma(float c)
150 {
151 	float *v = (float *)_TIFFmalloc(256 * sizeof (float));
152 	int i;
153 	for (i = 0; i < 256; i++)
154 		v[i] = c * i;
155 	return (v);
156 }
157 
158 static unsigned
V2Code(float f,float RB,float RW,int CR)159 V2Code(float f, float RB, float RW, int CR)
160 {
161 	unsigned int c = (unsigned int)((((f)*(RW-RB)/CR)+RB)+.5);
162 	return (c > 255 ? 255 : c);
163 }
164 
165 static void
setupLumaTables(void)166 setupLumaTables(void)
167 {
168 	lumaRed = setupLuma(LumaRed);
169 	lumaGreen = setupLuma(LumaGreen);
170 	lumaBlue = setupLuma(LumaBlue);
171 	D1 = 1.F/(2.F - 2.F*LumaBlue);
172 	D2 = 1.F/(2.F - 2.F*LumaRed);
173 	Yzero = V2Code(0, refBlackWhite[0], refBlackWhite[1], 255);
174 }
175 
176 static void
cvtClump(unsigned char * op,uint32 * raster,uint32 ch,uint32 cw,uint32 w)177 cvtClump(unsigned char* op, uint32* raster, uint32 ch, uint32 cw, uint32 w)
178 {
179 	float Y, Cb = 0, Cr = 0;
180 	uint32 j, k;
181 	/*
182 	 * Convert ch-by-cw block of RGB
183 	 * to YCbCr and sample accordingly.
184 	 */
185 	for (k = 0; k < ch; k++) {
186 		for (j = 0; j < cw; j++) {
187 			uint32 RGB = (raster - k*w)[j];
188 			Y = lumaRed[TIFFGetR(RGB)] +
189 			    lumaGreen[TIFFGetG(RGB)] +
190 			    lumaBlue[TIFFGetB(RGB)];
191 			/* accumulate chrominance */
192 			Cb += (TIFFGetB(RGB) - Y) * D1;
193 			Cr += (TIFFGetR(RGB) - Y) * D2;
194 			/* emit luminence */
195 			*op++ = V2Code(Y,
196 			    refBlackWhite[0], refBlackWhite[1], 255);
197 		}
198 		for (; j < horizSubSampling; j++)
199 			*op++ = Yzero;
200 	}
201 	for (; k < vertSubSampling; k++) {
202 		for (j = 0; j < horizSubSampling; j++)
203 			*op++ = Yzero;
204 	}
205 	/* emit sampled chrominance values */
206 	*op++ = V2Code(Cb / (ch*cw), refBlackWhite[2], refBlackWhite[3], 127);
207 	*op++ = V2Code(Cr / (ch*cw), refBlackWhite[4], refBlackWhite[5], 127);
208 }
209 #undef LumaRed
210 #undef LumaGreen
211 #undef LumaBlue
212 #undef V2Code
213 
214 /*
215  * Convert a strip of RGB data to YCbCr and
216  * sample to generate the output data.
217  */
218 static void
cvtStrip(unsigned char * op,uint32 * raster,uint32 nrows,uint32 width)219 cvtStrip(unsigned char* op, uint32* raster, uint32 nrows, uint32 width)
220 {
221 	uint32 x;
222 	int clumpSize = vertSubSampling * horizSubSampling + 2;
223 	uint32 *tp;
224 
225 	for (; nrows >= vertSubSampling; nrows -= vertSubSampling) {
226 		tp = raster;
227 		for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
228 			cvtClump(op, tp,
229 			    vertSubSampling, horizSubSampling, width);
230 			op += clumpSize;
231 			tp += horizSubSampling;
232 		}
233 		if (x > 0) {
234 			cvtClump(op, tp, vertSubSampling, x, width);
235 			op += clumpSize;
236 		}
237 		raster -= vertSubSampling*width;
238 	}
239 	if (nrows > 0) {
240 		tp = raster;
241 		for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
242 			cvtClump(op, tp, nrows, horizSubSampling, width);
243 			op += clumpSize;
244 			tp += horizSubSampling;
245 		}
246 		if (x > 0)
247 			cvtClump(op, tp, nrows, x, width);
248 	}
249 }
250 
251 static int
cvtRaster(TIFF * tif,uint32 * raster,uint32 width,uint32 height)252 cvtRaster(TIFF* tif, uint32* raster, uint32 width, uint32 height)
253 {
254 	uint32 y;
255 	tstrip_t strip = 0;
256 	tsize_t cc, acc;
257 	unsigned char* buf;
258 	uint32 rwidth = roundup(width, horizSubSampling);
259 	uint32 rheight = roundup(height, vertSubSampling);
260 	uint32 nrows = (rowsperstrip > rheight ? rheight : rowsperstrip);
261         uint32 rnrows = roundup(nrows,vertSubSampling);
262 
263 	cc = rnrows*rwidth +
264 	    2*((rnrows*rwidth) / (horizSubSampling*vertSubSampling));
265 	buf = (unsigned char*)_TIFFmalloc(cc);
266 	// FIXME unchecked malloc
267 	for (y = height; (int32) y > 0; y -= nrows) {
268 		uint32 nr = (y > nrows ? nrows : y);
269 		cvtStrip(buf, raster + (y-1)*width, nr, width);
270 		nr = roundup(nr, vertSubSampling);
271 		acc = nr*rwidth +
272 			2*((nr*rwidth)/(horizSubSampling*vertSubSampling));
273 		if (!TIFFWriteEncodedStrip(tif, strip++, buf, acc)) {
274 			_TIFFfree(buf);
275 			return (0);
276 		}
277 	}
278 	_TIFFfree(buf);
279 	return (1);
280 }
281 
282 static int
tiffcvt(TIFF * in,TIFF * out)283 tiffcvt(TIFF* in, TIFF* out)
284 {
285 	uint32 width, height;		/* image width & height */
286 	uint32* raster;			/* retrieve RGBA image */
287 	uint16 shortv;
288 	float floatv;
289 	char *stringv;
290 	uint32 longv;
291 	int result;
292 	size_t pixel_count;
293 
294 	TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
295 	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
296 	pixel_count = width * height;
297 
298  	/* XXX: Check the integer overflow. */
299  	if (!width || !height || pixel_count / width != height) {
300  		TIFFError(TIFFFileName(in),
301  			  "Malformed input file; "
302  			  "can't allocate buffer for raster of %lux%lu size",
303  			  (unsigned long)width, (unsigned long)height);
304  		return 0;
305  	}
306 
307  	raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32),
308  					   "raster buffer");
309   	if (raster == 0) {
310  		TIFFError(TIFFFileName(in),
311  			  "Failed to allocate buffer (%lu elements of %lu each)",
312  			  (unsigned long)pixel_count,
313  			  (unsigned long)sizeof(uint32));
314   		return (0);
315   	}
316 
317 	if (!TIFFReadRGBAImage(in, width, height, raster, 0)) {
318 		_TIFFfree(raster);
319 		return (0);
320 	}
321 
322 	CopyField(TIFFTAG_SUBFILETYPE, longv);
323 	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
324 	TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
325 	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
326 	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
327 	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
328 	if (compression == COMPRESSION_JPEG)
329 		TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);
330 	CopyField(TIFFTAG_FILLORDER, shortv);
331 	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
332 	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
333 	CopyField(TIFFTAG_XRESOLUTION, floatv);
334 	CopyField(TIFFTAG_YRESOLUTION, floatv);
335 	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
336 	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
337 	{ char buf[2048];
338 	  char *cp = strrchr(TIFFFileName(in), '/');
339 	  snprintf(buf, sizeof(buf), "YCbCr conversion of %s",
340 		   cp ? cp+1 : TIFFFileName(in));
341 	  TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, buf);
342 	}
343 	TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
344 	CopyField(TIFFTAG_DOCUMENTNAME, stringv);
345 
346 	TIFFSetField(out, TIFFTAG_REFERENCEBLACKWHITE, refBlackWhite);
347 	TIFFSetField(out, TIFFTAG_YCBCRSUBSAMPLING,
348 	    horizSubSampling, vertSubSampling);
349 	TIFFSetField(out, TIFFTAG_YCBCRPOSITIONING, YCBCRPOSITION_CENTERED);
350 	TIFFSetField(out, TIFFTAG_YCBCRCOEFFICIENTS, ycbcrCoeffs);
351 	rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
352 	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
353 
354 	result = cvtRaster(out, raster, width, height);
355         _TIFFfree(raster);
356         return result;
357 }
358 
359 char* stuff[] = {
360     "usage: rgb2ycbcr [-c comp] [-r rows] [-h N] [-v N] input... output\n",
361     "where comp is one of the following compression algorithms:\n",
362     " jpeg\t\tJPEG encoding\n",
363     " lzw\t\tLempel-Ziv & Welch encoding\n",
364     " zip\t\tdeflate encoding\n",
365     " packbits\tPackBits encoding (default)\n",
366     " none\t\tno compression\n",
367     "and the other options are:\n",
368     " -r\trows/strip\n",
369     " -h\thorizontal sampling factor (1,2,4)\n",
370     " -v\tvertical sampling factor (1,2,4)\n",
371     NULL
372 };
373 
374 static void
usage(int code)375 usage(int code)
376 {
377 	char buf[BUFSIZ];
378 	int i;
379 
380 	setbuf(stderr, buf);
381 
382  fprintf(stderr, "%s\n\n", TIFFGetVersion());
383 	for (i = 0; stuff[i] != NULL; i++)
384 		fprintf(stderr, "%s\n", stuff[i]);
385 	exit(code);
386 }
387 
388 /* vim: set ts=8 sts=8 sw=8 noet: */
389 /*
390  * Local Variables:
391  * mode: c
392  * c-basic-offset: 8
393  * fill-column: 78
394  * End:
395  */
396