istereo

changeset 26:862a3329a8f0

wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 06:28:38 +0300
parents 206348366635
children fd39c0198935
files Makefile libs/Makefile libs/imago2/conv.c libs/imago2/file_jpeg.c libs/imago2/file_png.c libs/imago2/file_ppm.c libs/imago2/ftype_module.c libs/imago2/ftype_module.h libs/imago2/imago2.c libs/imago2/imago2.h libs/imago2/modules.c libs/libjpeg/cderror.h libs/libjpeg/jcapimin.c libs/libjpeg/jcapistd.c libs/libjpeg/jccoefct.c libs/libjpeg/jccolor.c libs/libjpeg/jcdctmgr.c libs/libjpeg/jchuff.c libs/libjpeg/jchuff.h libs/libjpeg/jcinit.c libs/libjpeg/jcmainct.c libs/libjpeg/jcmarker.c libs/libjpeg/jcmaster.c libs/libjpeg/jcomapi.c libs/libjpeg/jconfig.h libs/libjpeg/jcparam.c libs/libjpeg/jcphuff.c libs/libjpeg/jcprepct.c libs/libjpeg/jcsample.c libs/libjpeg/jctrans.c libs/libjpeg/jdapimin.c libs/libjpeg/jdapistd.c libs/libjpeg/jdatadst.c libs/libjpeg/jdatasrc.c libs/libjpeg/jdcoefct.c libs/libjpeg/jdcolor.c libs/libjpeg/jdct.h libs/libjpeg/jddctmgr.c libs/libjpeg/jdhuff.c libs/libjpeg/jdhuff.h libs/libjpeg/jdinput.c libs/libjpeg/jdmainct.c libs/libjpeg/jdmarker.c libs/libjpeg/jdmaster.c libs/libjpeg/jdmerge.c libs/libjpeg/jdphuff.c libs/libjpeg/jdpostct.c libs/libjpeg/jdsample.c libs/libjpeg/jdtrans.c libs/libjpeg/jerror.c libs/libjpeg/jerror.h libs/libjpeg/jfdctflt.c libs/libjpeg/jfdctfst.c libs/libjpeg/jfdctint.c libs/libjpeg/jidctflt.c libs/libjpeg/jidctfst.c libs/libjpeg/jidctint.c libs/libjpeg/jidctred.c libs/libjpeg/jinclude.h libs/libjpeg/jmemmgr.c libs/libjpeg/jmemnobs.c libs/libjpeg/jmemsys.h libs/libjpeg/jmorecfg.h libs/libjpeg/jpegint.h libs/libjpeg/jpeglib.h libs/libjpeg/jquant1.c libs/libjpeg/jquant2.c libs/libjpeg/jutils.c libs/libjpeg/jversion.h libs/libpng/png.c libs/libpng/png.h libs/libpng/pngconf.h libs/libpng/pngerror.c libs/libpng/pnggccrd.c libs/libpng/pngget.c libs/libpng/pngmem.c libs/libpng/pngpread.c libs/libpng/pngread.c libs/libpng/pngrio.c libs/libpng/pngrtran.c libs/libpng/pngrutil.c libs/libpng/pngset.c libs/libpng/pngtrans.c libs/libpng/pngvcrd.c libs/libpng/pngwio.c libs/libpng/pngwrite.c libs/libpng/pngwtran.c libs/libpng/pngwutil.c libs/zlib/adler32.c libs/zlib/compress.c libs/zlib/crc32.c libs/zlib/crc32.h libs/zlib/deflate.c libs/zlib/deflate.h libs/zlib/gzio.c libs/zlib/infback.c libs/zlib/inffast.c libs/zlib/inffast.h libs/zlib/inffixed.h libs/zlib/inflate.c libs/zlib/inflate.h libs/zlib/inftrees.c libs/zlib/inftrees.h libs/zlib/trees.c libs/zlib/trees.h libs/zlib/uncompr.c libs/zlib/zconf.h libs/zlib/zlib.h libs/zlib/zutil.c libs/zlib/zutil.h src/istereo.c src/tex.c
diffstat 112 files changed, 62060 insertions(+), 47 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Sep 08 04:23:56 2011 +0300
     1.2 +++ b/Makefile	Thu Sep 08 06:28:38 2011 +0300
     1.3 @@ -1,14 +1,28 @@
     1.4  src = $(wildcard src/*.c)
     1.5 +
     1.6  obj = $(src:.c=.o)
     1.7 +dep = $(obj:.o=.d)
     1.8  bin = test
     1.9  
    1.10  CC = gcc
    1.11 -CFLAGS = -pedantic -Wall -g
    1.12 +CFLAGS = -pedantic -Wall -g $(incdir)
    1.13  LDFLAGS = -lGL -lGLU -lglut -lGLEW -lm
    1.14  
    1.15 +include libs/Makefile
    1.16 +
    1.17  $(bin): $(obj)
    1.18  	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.19  
    1.20 +-include $(dep)
    1.21 +
    1.22 +%.d: %.c
    1.23 +	@$(CPP) $(CFLAGS) -MM -MT $(@:.d=.o) $< >$@
    1.24 +
    1.25 +
    1.26  .PHONY: clean
    1.27  clean:
    1.28  	rm -f $(obj) $(bin)
    1.29 +
    1.30 +.PHONY: cleandep
    1.31 +cleandep:
    1.32 +	rm -f $(dep)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/libs/Makefile	Thu Sep 08 06:28:38 2011 +0300
     2.3 @@ -0,0 +1,6 @@
     2.4 +src += $(wildcard libs/zlib/*.c) \
     2.5 +	   $(wildcard libs/libpng/*.c) \
     2.6 +	   $(wildcard libs/libjpeg/*.c) \
     2.7 +	   $(wildcard libs/imago2/*.c)
     2.8 +
     2.9 +incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/libs/imago2/conv.c	Thu Sep 08 06:28:38 2011 +0300
     3.3 @@ -0,0 +1,251 @@
     3.4 +/*
     3.5 +libimago - a multi-format image file input/output library.
     3.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     3.7 +
     3.8 +This program is free software: you can redistribute it and/or modify
     3.9 +it under the terms of the GNU Lesser General Public License as published
    3.10 +by the Free Software Foundation, either version 3 of the License, or
    3.11 +(at your option) any later version.
    3.12 +
    3.13 +This program is distributed in the hope that it will be useful,
    3.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.16 +GNU Lesser General Public License for more details.
    3.17 +
    3.18 +You should have received a copy of the GNU Lesser General Public License
    3.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    3.20 +*/
    3.21 +#include <string.h>
    3.22 +#include "imago2.h"
    3.23 +
    3.24 +/* pixel-format conversions are sub-optimal at the moment to avoid
    3.25 + * writing a lot of code. optimize at some point ?
    3.26 + */
    3.27 +
    3.28 +#define CLAMP(x, a, b)	((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
    3.29 +
    3.30 +struct pixel {
    3.31 +	float r, g, b, a;
    3.32 +};
    3.33 +
    3.34 +static void unpack_grey8(struct pixel *unp, void *pptr, int count);
    3.35 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
    3.36 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
    3.37 +static void unpack_greyf(struct pixel *unp, void *pptr, int count);
    3.38 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
    3.39 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
    3.40 +
    3.41 +static void pack_grey8(void *pptr, struct pixel *unp, int count);
    3.42 +static void pack_rgb24(void *pptr, struct pixel *unp, int count);
    3.43 +static void pack_rgba32(void *pptr, struct pixel *unp, int count);
    3.44 +static void pack_greyf(void *pptr, struct pixel *unp, int count);
    3.45 +static void pack_rgbf(void *pptr, struct pixel *unp, int count);
    3.46 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
    3.47 +
    3.48 +/* XXX keep in sync with enum img_fmt at imago2.h */
    3.49 +static void (*unpack[])(struct pixel*, void*, int) = {
    3.50 +	unpack_grey8,
    3.51 +	unpack_rgb24,
    3.52 +	unpack_rgba32,
    3.53 +	unpack_greyf,
    3.54 +	unpack_rgbf,
    3.55 +	unpack_rgbaf
    3.56 +};
    3.57 +
    3.58 +/* XXX keep in sync with enum img_fmt at imago2.h */
    3.59 +static void (*pack[])(void*, struct pixel*, int) = {
    3.60 +	pack_grey8,
    3.61 +	pack_rgb24,
    3.62 +	pack_rgba32,
    3.63 +	pack_greyf,
    3.64 +	pack_rgbf,
    3.65 +	pack_rgbaf
    3.66 +};
    3.67 +
    3.68 +
    3.69 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
    3.70 +{
    3.71 +	struct pixel pbuf[8];
    3.72 +	int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1);
    3.73 +	int i, num_pix = img->width * img->height;
    3.74 +	int num_iter = num_pix / bufsz;
    3.75 +	char *sptr, *dptr;
    3.76 +	struct img_pixmap nimg;
    3.77 +
    3.78 +	if(img->fmt == tofmt) {
    3.79 +		return 0;	/* nothing to do */
    3.80 +	}
    3.81 +
    3.82 +	img_init(&nimg);
    3.83 +	if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) {
    3.84 +		img_destroy(&nimg);
    3.85 +		return -1;
    3.86 +	}
    3.87 +
    3.88 +	sptr = img->pixels;
    3.89 +	dptr = nimg.pixels;
    3.90 +
    3.91 +	for(i=0; i<num_iter; i++) {
    3.92 +		unpack[img->fmt](pbuf, sptr, bufsz);
    3.93 +		pack[tofmt](dptr, pbuf, bufsz);
    3.94 +
    3.95 +		sptr += bufsz * img->pixelsz;
    3.96 +		dptr += bufsz * nimg.pixelsz;
    3.97 +	}
    3.98 +
    3.99 +	img_copy(img, &nimg);
   3.100 +	img_destroy(&nimg);
   3.101 +	return 0;
   3.102 +}
   3.103 +
   3.104 +/* the following functions *could* benefit from SIMD */
   3.105 +
   3.106 +static void unpack_grey8(struct pixel *unp, void *pptr, int count)
   3.107 +{
   3.108 +	int i;
   3.109 +	unsigned char *pix = pptr;
   3.110 +
   3.111 +	for(i=0; i<count; i++) {
   3.112 +		unp->r = unp->g = unp->b = (float)*pix++ / 255.0;
   3.113 +		unp->a = 1.0;
   3.114 +		unp++;
   3.115 +	}
   3.116 +}
   3.117 +
   3.118 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count)
   3.119 +{
   3.120 +	int i;
   3.121 +	unsigned char *pix = pptr;
   3.122 +
   3.123 +	for(i=0; i<count; i++) {
   3.124 +		unp->r = (float)*pix++ / 255.0;
   3.125 +		unp->g = (float)*pix++ / 255.0;
   3.126 +		unp->b = (float)*pix++ / 255.0;
   3.127 +		unp->a = 1.0;
   3.128 +		unp++;
   3.129 +	}
   3.130 +}
   3.131 +
   3.132 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
   3.133 +{
   3.134 +	memcpy(unp, pptr, count * sizeof *unp);
   3.135 +}
   3.136 +
   3.137 +static void unpack_greyf(struct pixel *unp, void *pptr, int count)
   3.138 +{
   3.139 +	int i;
   3.140 +	float *pix = pptr;
   3.141 +
   3.142 +	for(i=0; i<count; i++) {
   3.143 +		unp->r = unp->g = unp->b = *pix++;
   3.144 +		unp->a = 1.0;
   3.145 +		unp++;
   3.146 +	}
   3.147 +}
   3.148 +
   3.149 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count)
   3.150 +{
   3.151 +	int i;
   3.152 +	float *pix = pptr;
   3.153 +
   3.154 +	for(i=0; i<count; i++) {
   3.155 +		unp->r = *pix++;
   3.156 +		unp->g = *pix++;
   3.157 +		unp->b = *pix++;
   3.158 +		unp->a = 1.0;
   3.159 +		unp++;
   3.160 +	}
   3.161 +}
   3.162 +
   3.163 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
   3.164 +{
   3.165 +	int i;
   3.166 +	float *pix = pptr;
   3.167 +
   3.168 +	for(i=0; i<count; i++) {
   3.169 +		unp->r = *pix++;
   3.170 +		unp->g = *pix++;
   3.171 +		unp->b = *pix++;
   3.172 +		unp->a = *pix++;
   3.173 +		unp++;
   3.174 +	}
   3.175 +}
   3.176 +
   3.177 +
   3.178 +static void pack_grey8(void *pptr, struct pixel *unp, int count)
   3.179 +{
   3.180 +	int i;
   3.181 +	unsigned char *pix = pptr;
   3.182 +
   3.183 +	for(i=0; i<count; i++) {
   3.184 +		int lum = (int)(255.0 * (unp->r + unp->g + unp->b) / 3.0);
   3.185 +		*pix++ = CLAMP(lum, 0, 255);
   3.186 +		unp++;
   3.187 +	}
   3.188 +}
   3.189 +
   3.190 +static void pack_rgb24(void *pptr, struct pixel *unp, int count)
   3.191 +{
   3.192 +	int i;
   3.193 +	unsigned char *pix = pptr;
   3.194 +
   3.195 +	for(i=0; i<count; i++) {
   3.196 +		int r = (int)(unp->r * 255.0);
   3.197 +		int g = (int)(unp->g * 255.0);
   3.198 +		int b = (int)(unp->b * 255.0);
   3.199 +
   3.200 +		*pix++ = CLAMP(r, 0, 255);
   3.201 +		*pix++ = CLAMP(g, 0, 255);
   3.202 +		*pix++ = CLAMP(b, 0, 255);
   3.203 +		unp++;
   3.204 +	}
   3.205 +}
   3.206 +
   3.207 +static void pack_rgba32(void *pptr, struct pixel *unp, int count)
   3.208 +{
   3.209 +	int i;
   3.210 +	unsigned char *pix = pptr;
   3.211 +
   3.212 +	for(i=0; i<count; i++) {
   3.213 +		int r = (int)(unp->r * 255.0);
   3.214 +		int g = (int)(unp->g * 255.0);
   3.215 +		int b = (int)(unp->b * 255.0);
   3.216 +		int a = (int)(unp->a * 255.0);
   3.217 +
   3.218 +		*pix++ = CLAMP(r, 0, 255);
   3.219 +		*pix++ = CLAMP(g, 0, 255);
   3.220 +		*pix++ = CLAMP(b, 0, 255);
   3.221 +		*pix++ = CLAMP(a, 0, 255);
   3.222 +		unp++;
   3.223 +	}
   3.224 +}
   3.225 +
   3.226 +static void pack_greyf(void *pptr, struct pixel *unp, int count)
   3.227 +{
   3.228 +	int i;
   3.229 +	float *pix = pptr;
   3.230 +
   3.231 +	for(i=0; i<count; i++) {
   3.232 +		*pix++ = (unp->r + unp->g + unp->b) / 3.0;
   3.233 +		unp++;
   3.234 +	}
   3.235 +}
   3.236 +
   3.237 +static void pack_rgbf(void *pptr, struct pixel *unp, int count)
   3.238 +{
   3.239 +	int i;
   3.240 +	float *pix = pptr;
   3.241 +
   3.242 +	for(i=0; i<count; i++) {
   3.243 +		*pix++ = unp->r;
   3.244 +		*pix++ = unp->g;
   3.245 +		*pix++ = unp->b;
   3.246 +		unp++;
   3.247 +	}
   3.248 +}
   3.249 +
   3.250 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
   3.251 +{
   3.252 +	memcpy(pptr, unp, count * sizeof *unp);
   3.253 +}
   3.254 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/libs/imago2/file_jpeg.c	Thu Sep 08 06:28:38 2011 +0300
     4.3 @@ -0,0 +1,291 @@
     4.4 +/*
     4.5 +libimago - a multi-format image file input/output library.
     4.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     4.7 +
     4.8 +This program is free software: you can redistribute it and/or modify
     4.9 +it under the terms of the GNU Lesser General Public License as published
    4.10 +by the Free Software Foundation, either version 3 of the License, or
    4.11 +(at your option) any later version.
    4.12 +
    4.13 +This program is distributed in the hope that it will be useful,
    4.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.16 +GNU Lesser General Public License for more details.
    4.17 +
    4.18 +You should have received a copy of the GNU Lesser General Public License
    4.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    4.20 +*/
    4.21 +
    4.22 +/* -- JPEG module -- */
    4.23 +
    4.24 +#include <stdio.h>
    4.25 +#include <stdlib.h>
    4.26 +#include <string.h>
    4.27 +
    4.28 +#ifdef WIN32
    4.29 +#include <windows.h>
    4.30 +#define HAVE_BOOLEAN
    4.31 +#endif
    4.32 +
    4.33 +#include <jpeglib.h>
    4.34 +#include "imago2.h"
    4.35 +#include "ftype_module.h"
    4.36 +
    4.37 +#define INPUT_BUF_SIZE	512
    4.38 +#define OUTPUT_BUF_SIZE	512
    4.39 +
    4.40 +/* data source manager: adapted from jdatasrc.c */
    4.41 +struct src_mgr {
    4.42 +	struct jpeg_source_mgr pub;
    4.43 +
    4.44 +	struct img_io *io;
    4.45 +	unsigned char buffer[INPUT_BUF_SIZE];
    4.46 +	int start_of_file;
    4.47 +};
    4.48 +
    4.49 +/* data destination manager: adapted from jdatadst.c */
    4.50 +struct dst_mgr {
    4.51 +	struct jpeg_destination_mgr pub;
    4.52 +
    4.53 +	struct img_io *io;
    4.54 +	unsigned char buffer[OUTPUT_BUF_SIZE];
    4.55 +};
    4.56 +
    4.57 +static int check(struct img_io *io);
    4.58 +static int read(struct img_pixmap *img, struct img_io *io);
    4.59 +static int write(struct img_pixmap *img, struct img_io *io);
    4.60 +
    4.61 +/* read source functions */
    4.62 +static void init_source(j_decompress_ptr jd);
    4.63 +static int fill_input_buffer(j_decompress_ptr jd);
    4.64 +static void skip_input_data(j_decompress_ptr jd, long num_bytes);
    4.65 +static void term_source(j_decompress_ptr jd);
    4.66 +
    4.67 +/* write destination functions */
    4.68 +static void init_destination(j_compress_ptr jc);
    4.69 +static int empty_output_buffer(j_compress_ptr jc);
    4.70 +static void term_destination(j_compress_ptr jc);
    4.71 +
    4.72 +int img_register_jpeg(void)
    4.73 +{
    4.74 +	static struct ftype_module mod = {".jpg", check, read, write};
    4.75 +	return img_register_module(&mod);
    4.76 +}
    4.77 +
    4.78 +
    4.79 +static int check(struct img_io *io)
    4.80 +{
    4.81 +	unsigned char sig[10];
    4.82 +
    4.83 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    4.84 +
    4.85 +	if(io->read(sig, 10, io->uptr) < 10) {
    4.86 +		io->seek(pos, SEEK_SET, io->uptr);
    4.87 +		return -1;
    4.88 +	}
    4.89 +
    4.90 +	if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 || memcmp(sig + 6, "JFIF", 4) != 0) {
    4.91 +		io->seek(pos, SEEK_SET, io->uptr);
    4.92 +		return -1;
    4.93 +	}
    4.94 +	io->seek(pos, SEEK_SET, io->uptr);
    4.95 +	return 0;
    4.96 +}
    4.97 +
    4.98 +static int read(struct img_pixmap *img, struct img_io *io)
    4.99 +{
   4.100 +	int i, nlines = 0;
   4.101 +	struct jpeg_decompress_struct cinfo;
   4.102 +	struct jpeg_error_mgr jerr;
   4.103 +	struct src_mgr src;
   4.104 +	unsigned char **scanlines;
   4.105 +
   4.106 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX change... */
   4.107 +	jpeg_create_decompress(&cinfo);
   4.108 +
   4.109 +	src.pub.init_source = init_source;
   4.110 +	src.pub.fill_input_buffer = fill_input_buffer;
   4.111 +	src.pub.skip_input_data = skip_input_data;
   4.112 +	src.pub.resync_to_restart = jpeg_resync_to_restart;
   4.113 +	src.pub.term_source = term_source;
   4.114 +	src.pub.next_input_byte = 0;
   4.115 +	src.pub.bytes_in_buffer = 0;
   4.116 +	src.io = io;
   4.117 +	cinfo.src = (struct jpeg_source_mgr*)&src;
   4.118 +
   4.119 +	jpeg_read_header(&cinfo, 1);
   4.120 +	cinfo.out_color_space = JCS_RGB;
   4.121 +
   4.122 +	if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
   4.123 +		jpeg_destroy_decompress(&cinfo);
   4.124 +		return -1;
   4.125 +	}
   4.126 +
   4.127 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   4.128 +		jpeg_destroy_decompress(&cinfo);
   4.129 +		return -1;
   4.130 +	}
   4.131 +	scanlines[0] = img->pixels;
   4.132 +	for(i=1; i<img->height; i++) {
   4.133 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   4.134 +	}
   4.135 +
   4.136 +	jpeg_start_decompress(&cinfo);
   4.137 +	while(nlines < img->height) {
   4.138 +		int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   4.139 +		nlines += res;
   4.140 +	}
   4.141 +	jpeg_finish_decompress(&cinfo);
   4.142 +	jpeg_destroy_decompress(&cinfo);
   4.143 +
   4.144 +	free(scanlines);
   4.145 +	return 0;
   4.146 +}
   4.147 +
   4.148 +static int write(struct img_pixmap *img, struct img_io *io)
   4.149 +{
   4.150 +	int i, nlines = 0;
   4.151 +	struct jpeg_compress_struct cinfo;
   4.152 +	struct jpeg_error_mgr jerr;
   4.153 +	struct dst_mgr dest;
   4.154 +	struct img_pixmap tmpimg;
   4.155 +	unsigned char **scanlines;
   4.156 +
   4.157 +	img_init(&tmpimg);
   4.158 +
   4.159 +	if(img->fmt != IMG_FMT_RGB24) {
   4.160 +		if(img_copy(&tmpimg, img) == -1) {
   4.161 +			return -1;
   4.162 +		}
   4.163 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   4.164 +			img_destroy(&tmpimg);
   4.165 +			return -1;
   4.166 +		}
   4.167 +		img = &tmpimg;
   4.168 +	}
   4.169 +
   4.170 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   4.171 +		img_destroy(&tmpimg);
   4.172 +		return -1;
   4.173 +	}
   4.174 +	scanlines[0] = img->pixels;
   4.175 +	for(i=1; i<img->height; i++) {
   4.176 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   4.177 +	}
   4.178 +
   4.179 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX */
   4.180 +	jpeg_create_compress(&cinfo);
   4.181 +
   4.182 +	dest.pub.init_destination = init_destination;
   4.183 +	dest.pub.empty_output_buffer = empty_output_buffer;
   4.184 +	dest.pub.term_destination = term_destination;
   4.185 +	dest.io = io;
   4.186 +	cinfo.dest = (struct jpeg_destination_mgr*)&dest;
   4.187 +
   4.188 +	cinfo.image_width = img->width;
   4.189 +	cinfo.image_height = img->height;
   4.190 +	cinfo.input_components = 3;
   4.191 +	cinfo.in_color_space = JCS_RGB;
   4.192 +
   4.193 +	jpeg_set_defaults(&cinfo);
   4.194 +
   4.195 +	jpeg_start_compress(&cinfo, 1);
   4.196 +	while(nlines < img->height) {
   4.197 +		int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   4.198 +		nlines += res;
   4.199 +	}
   4.200 +	jpeg_finish_compress(&cinfo);
   4.201 +	jpeg_destroy_compress(&cinfo);
   4.202 +
   4.203 +	free(scanlines);
   4.204 +	img_destroy(&tmpimg);
   4.205 +	return 0;
   4.206 +}
   4.207 +
   4.208 +/* -- read source functions --
   4.209 + * the following functions are adapted from jdatasrc.c in jpeglib
   4.210 + */
   4.211 +static void init_source(j_decompress_ptr jd)
   4.212 +{
   4.213 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   4.214 +	src->start_of_file = 1;
   4.215 +}
   4.216 +
   4.217 +static int fill_input_buffer(j_decompress_ptr jd)
   4.218 +{
   4.219 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   4.220 +	size_t nbytes;
   4.221 +
   4.222 +	nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
   4.223 +
   4.224 +	if(nbytes <= 0) {
   4.225 +		if(src->start_of_file) {
   4.226 +			return 0;
   4.227 +		}
   4.228 +		/* insert a fake EOI marker */
   4.229 +		src->buffer[0] = 0xff;
   4.230 +		src->buffer[1] = JPEG_EOI;
   4.231 +		nbytes = 2;
   4.232 +	}
   4.233 +
   4.234 +	src->pub.next_input_byte = src->buffer;
   4.235 +	src->pub.bytes_in_buffer = nbytes;
   4.236 +	src->start_of_file = 0;
   4.237 +	return 1;
   4.238 +}
   4.239 +
   4.240 +static void skip_input_data(j_decompress_ptr jd, long num_bytes)
   4.241 +{
   4.242 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   4.243 +
   4.244 +	if(num_bytes > 0) {
   4.245 +		while(num_bytes > (long)src->pub.bytes_in_buffer) {
   4.246 +			num_bytes -= (long)src->pub.bytes_in_buffer;
   4.247 +			fill_input_buffer(jd);
   4.248 +		}
   4.249 +		src->pub.next_input_byte += (size_t)num_bytes;
   4.250 +		src->pub.bytes_in_buffer -= (size_t)num_bytes;
   4.251 +	}
   4.252 +}
   4.253 +
   4.254 +static void term_source(j_decompress_ptr jd)
   4.255 +{
   4.256 +	/* nothing to see here, move along */
   4.257 +}
   4.258 +
   4.259 +
   4.260 +/* -- write destination functions --
   4.261 + * the following functions are adapted from jdatadst.c in jpeglib
   4.262 + */
   4.263 +static void init_destination(j_compress_ptr jc)
   4.264 +{
   4.265 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   4.266 +
   4.267 +	dest->pub.next_output_byte = dest->buffer;
   4.268 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   4.269 +}
   4.270 +
   4.271 +static int empty_output_buffer(j_compress_ptr jc)
   4.272 +{
   4.273 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   4.274 +
   4.275 +	if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
   4.276 +		return 0;
   4.277 +	}
   4.278 +
   4.279 +	dest->pub.next_output_byte = dest->buffer;
   4.280 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   4.281 +	return 1;
   4.282 +}
   4.283 +
   4.284 +static void term_destination(j_compress_ptr jc)
   4.285 +{
   4.286 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   4.287 +	size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
   4.288 +
   4.289 +	/* write any remaining data in the buffer */
   4.290 +	if(datacount > 0) {
   4.291 +		dest->io->write(dest->buffer, datacount, dest->io->uptr);
   4.292 +	}
   4.293 +	/* XXX flush? ... */
   4.294 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/libs/imago2/file_png.c	Thu Sep 08 06:28:38 2011 +0300
     5.3 @@ -0,0 +1,244 @@
     5.4 +/*
     5.5 +libimago - a multi-format image file input/output library.
     5.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     5.7 +
     5.8 +This program is free software: you can redistribute it and/or modify
     5.9 +it under the terms of the GNU Lesser General Public License as published
    5.10 +by the Free Software Foundation, either version 3 of the License, or
    5.11 +(at your option) any later version.
    5.12 +
    5.13 +This program is distributed in the hope that it will be useful,
    5.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.16 +GNU Lesser General Public License for more details.
    5.17 +
    5.18 +You should have received a copy of the GNU Lesser General Public License
    5.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    5.20 +*/
    5.21 +
    5.22 +/* -- PNG module -- */
    5.23 +
    5.24 +#include <stdlib.h>
    5.25 +#include <png.h>
    5.26 +#include "imago2.h"
    5.27 +#include "ftype_module.h"
    5.28 +
    5.29 +static int check_file(struct img_io *io);
    5.30 +static int read_file(struct img_pixmap *img, struct img_io *io);
    5.31 +static int write_file(struct img_pixmap *img, struct img_io *io);
    5.32 +
    5.33 +static void read_func(png_struct *png, unsigned char *data, size_t len);
    5.34 +static void write_func(png_struct *png, unsigned char *data, size_t len);
    5.35 +static void flush_func(png_struct *png);
    5.36 +
    5.37 +static int png_type_to_fmt(int color_type, int channel_bits);
    5.38 +static int fmt_to_png_type(enum img_fmt fmt);
    5.39 +
    5.40 +
    5.41 +int img_register_png(void)
    5.42 +{
    5.43 +	static struct ftype_module mod = {".png", check_file, read_file, write_file};
    5.44 +	return img_register_module(&mod);
    5.45 +}
    5.46 +
    5.47 +static int check_file(struct img_io *io)
    5.48 +{
    5.49 +	unsigned char sig[8];
    5.50 +	int res;
    5.51 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    5.52 +
    5.53 +	if(io->read(sig, 8, io->uptr) < 8) {
    5.54 +		io->seek(pos, SEEK_SET, io->uptr);
    5.55 +		return -1;
    5.56 +	}
    5.57 +
    5.58 +	res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
    5.59 +	io->seek(pos, SEEK_SET, io->uptr);
    5.60 +	return res;
    5.61 +}
    5.62 +
    5.63 +static int read_file(struct img_pixmap *img, struct img_io *io)
    5.64 +{
    5.65 +	png_struct *png;
    5.66 +	png_info *info;
    5.67 +	unsigned char **lineptr, *dest;
    5.68 +	int i, channel_bits, color_type, ilace_type, compression, filtering, fmt;
    5.69 +	unsigned long xsz, ysz;
    5.70 +
    5.71 +	if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
    5.72 +		return -1;
    5.73 +	}
    5.74 +
    5.75 +	if(!(info = png_create_info_struct(png))) {
    5.76 +		png_destroy_read_struct(&png, 0, 0);
    5.77 +		return -1;
    5.78 +	}
    5.79 +
    5.80 +	if(setjmp(png_jmpbuf(png))) {
    5.81 +		png_destroy_read_struct(&png, &info, 0);
    5.82 +		return -1;
    5.83 +	}
    5.84 +
    5.85 +	png_set_read_fn(png, io, read_func);
    5.86 +	png_set_sig_bytes(png, 0);
    5.87 +	png_read_png(png, info, 0, 0);
    5.88 +
    5.89 +	png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
    5.90 +			&compression, &filtering);
    5.91 +	if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
    5.92 +		png_destroy_read_struct(&png, &info, 0);
    5.93 +		return -1;
    5.94 +	}
    5.95 +
    5.96 +	if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
    5.97 +		png_destroy_read_struct(&png, &info, 0);
    5.98 +		return -1;
    5.99 +	}
   5.100 +
   5.101 +	lineptr = (unsigned char**)png_get_rows(png, info);
   5.102 +
   5.103 +	dest = img->pixels;
   5.104 +	for(i=0; i<ysz; i++) {
   5.105 +		memcpy(dest, lineptr[i], xsz * img->pixelsz);
   5.106 +		dest += xsz * img->pixelsz;
   5.107 +	}
   5.108 +	png_destroy_read_struct(&png, &info, 0);
   5.109 +	return 0;
   5.110 +}
   5.111 +
   5.112 +
   5.113 +static int write_file(struct img_pixmap *img, struct img_io *io)
   5.114 +{
   5.115 +	png_struct *png;
   5.116 +	png_info *info;
   5.117 +	png_text txt;
   5.118 +	struct img_pixmap tmpimg;
   5.119 +	unsigned char **rows;
   5.120 +	unsigned char *pixptr;
   5.121 +	int i, coltype;
   5.122 +
   5.123 +	img_init(&tmpimg);
   5.124 +
   5.125 +	if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
   5.126 +		return -1;
   5.127 +	}
   5.128 +	if(!(info = png_create_info_struct(png))) {
   5.129 +		png_destroy_write_struct(&png, 0);
   5.130 +		return -1;
   5.131 +	}
   5.132 +
   5.133 +	/* if the input image is floating-point, we need to convert it to integer */
   5.134 +	if(img_is_float(img)) {
   5.135 +		if(img_copy(&tmpimg, img) == -1) {
   5.136 +			return -1;
   5.137 +		}
   5.138 +		if(img_to_integer(&tmpimg) == -1) {
   5.139 +			img_destroy(&tmpimg);
   5.140 +			return -1;
   5.141 +		}
   5.142 +		img = &tmpimg;
   5.143 +	}
   5.144 +
   5.145 +	txt.compression = PNG_TEXT_COMPRESSION_NONE;
   5.146 +	txt.key = "Software";
   5.147 +	txt.text = "libimago2";
   5.148 +	txt.text_length = 0;
   5.149 +
   5.150 +	if(setjmp(png_jmpbuf(png))) {
   5.151 +		png_destroy_write_struct(&png, &info);
   5.152 +		img_destroy(&tmpimg);
   5.153 +		return -1;
   5.154 +	}
   5.155 +	png_set_write_fn(png, io, write_func, flush_func);
   5.156 +
   5.157 +	coltype = fmt_to_png_type(img->fmt);
   5.158 +	png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
   5.159 +			PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
   5.160 +	png_set_text(png, info, &txt, 1);
   5.161 +
   5.162 +	if(!(rows = malloc(img->height * sizeof *rows))) {
   5.163 +		png_destroy_write_struct(&png, &info);
   5.164 +		img_destroy(&tmpimg);
   5.165 +		return -1;
   5.166 +	}
   5.167 +
   5.168 +	pixptr = img->pixels;
   5.169 +	for(i=0; i<img->height; i++) {
   5.170 +		rows[i] = pixptr;
   5.171 +		pixptr += img->width * img->pixelsz;
   5.172 +	}
   5.173 +	png_set_rows(png, info, rows);
   5.174 +
   5.175 +	png_write_png(png, info, 0, 0);
   5.176 +	png_write_end(png, info);
   5.177 +	png_destroy_write_struct(&png, &info);
   5.178 +
   5.179 +	free(rows);
   5.180 +
   5.181 +	img_destroy(&tmpimg);
   5.182 +	return 0;
   5.183 +}
   5.184 +
   5.185 +static void read_func(png_struct *png, unsigned char *data, size_t len)
   5.186 +{
   5.187 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   5.188 +
   5.189 +	if(io->read(data, len, io->uptr) == -1) {
   5.190 +		longjmp(png_jmpbuf(png), 1);
   5.191 +	}
   5.192 +}
   5.193 +
   5.194 +static void write_func(png_struct *png, unsigned char *data, size_t len)
   5.195 +{
   5.196 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   5.197 +
   5.198 +	if(io->write(data, len, io->uptr) == -1) {
   5.199 +		longjmp(png_jmpbuf(png), 1);
   5.200 +	}
   5.201 +}
   5.202 +
   5.203 +static void flush_func(png_struct *png)
   5.204 +{
   5.205 +	/* XXX does it matter that we can't flush? */
   5.206 +}
   5.207 +
   5.208 +static int png_type_to_fmt(int color_type, int channel_bits)
   5.209 +{
   5.210 +	/* we don't support non-8bit-per-channel images yet */
   5.211 +	if(channel_bits > 8) {
   5.212 +		return -1;
   5.213 +	}
   5.214 +
   5.215 +	switch(color_type) {
   5.216 +	case PNG_COLOR_TYPE_RGB:
   5.217 +		return IMG_FMT_RGB24;
   5.218 +
   5.219 +	case PNG_COLOR_TYPE_RGB_ALPHA:
   5.220 +		return IMG_FMT_RGBA32;
   5.221 +
   5.222 +	case PNG_COLOR_TYPE_GRAY:
   5.223 +		return IMG_FMT_GREY8;
   5.224 +
   5.225 +	default:
   5.226 +		break;
   5.227 +	}
   5.228 +	return -1;
   5.229 +}
   5.230 +
   5.231 +static int fmt_to_png_type(enum img_fmt fmt)
   5.232 +{
   5.233 +	switch(fmt) {
   5.234 +	case IMG_FMT_GREY8:
   5.235 +		return PNG_COLOR_TYPE_GRAY;
   5.236 +
   5.237 +	case IMG_FMT_RGB24:
   5.238 +		return PNG_COLOR_TYPE_RGB;
   5.239 +
   5.240 +	case IMG_FMT_RGBA32:
   5.241 +		return PNG_COLOR_TYPE_RGBA;
   5.242 +
   5.243 +	default:
   5.244 +		break;
   5.245 +	}
   5.246 +	return -1;
   5.247 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/libs/imago2/file_ppm.c	Thu Sep 08 06:28:38 2011 +0300
     6.3 @@ -0,0 +1,153 @@
     6.4 +/*
     6.5 +libimago - a multi-format image file input/output library.
     6.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     6.7 +
     6.8 +This program is free software: you can redistribute it and/or modify
     6.9 +it under the terms of the GNU Lesser General Public License as published
    6.10 +by the Free Software Foundation, either version 3 of the License, or
    6.11 +(at your option) any later version.
    6.12 +
    6.13 +This program is distributed in the hope that it will be useful,
    6.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.16 +GNU Lesser General Public License for more details.
    6.17 +
    6.18 +You should have received a copy of the GNU Lesser General Public License
    6.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    6.20 +*/
    6.21 +
    6.22 +/* -- Portable Pixmap (PPM) module -- */
    6.23 +
    6.24 +#include <string.h>
    6.25 +#include "imago2.h"
    6.26 +#include "ftype_module.h"
    6.27 +
    6.28 +static int check(struct img_io *io);
    6.29 +static int read(struct img_pixmap *img, struct img_io *io);
    6.30 +static int write(struct img_pixmap *img, struct img_io *io);
    6.31 +
    6.32 +int img_register_ppm(void)
    6.33 +{
    6.34 +	static struct ftype_module mod = {".ppm", check, read, write};
    6.35 +	return img_register_module(&mod);
    6.36 +}
    6.37 +
    6.38 +
    6.39 +static int check(struct img_io *io)
    6.40 +{
    6.41 +	char id[2];
    6.42 +	int res = -1;
    6.43 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    6.44 +
    6.45 +	if(io->read(id, 2, io->uptr) < 2) {
    6.46 +		io->seek(pos, SEEK_SET, io->uptr);
    6.47 +		return -1;
    6.48 +	}
    6.49 +
    6.50 +	if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
    6.51 +		res = 0;
    6.52 +	}
    6.53 +	io->seek(pos, SEEK_SET, io->uptr);
    6.54 +	return res;
    6.55 +}
    6.56 +
    6.57 +static int iofgetc(struct img_io *io)
    6.58 +{
    6.59 +	char c;
    6.60 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
    6.61 +}
    6.62 +
    6.63 +static char *iofgets(char *buf, int size, struct img_io *io)
    6.64 +{
    6.65 +	int c;
    6.66 +	char *ptr = buf;
    6.67 +
    6.68 +	while(--size > 0 && (c = iofgetc(io)) != -1) {
    6.69 +		*ptr++ = c;
    6.70 +		if(c == '\n') break;
    6.71 +	}
    6.72 +	*ptr = 0;
    6.73 +
    6.74 +	return ptr == buf ? 0 : buf;
    6.75 +}
    6.76 +
    6.77 +/* TODO: implement P3 reading */
    6.78 +static int read(struct img_pixmap *img, struct img_io *io)
    6.79 +{
    6.80 +	char buf[256];
    6.81 +	int xsz, ysz, maxval, got_hdrlines = 1;
    6.82 +
    6.83 +	if(!iofgets(buf, sizeof buf, io)) {
    6.84 +		return -1;
    6.85 +	}
    6.86 +	if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
    6.87 +		return -1;
    6.88 +	}
    6.89 +
    6.90 +	while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
    6.91 +		if(buf[0] == '#') continue;
    6.92 +
    6.93 +		switch(got_hdrlines) {
    6.94 +		case 1:
    6.95 +			if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
    6.96 +				return -1;
    6.97 +			}
    6.98 +			break;
    6.99 +
   6.100 +		case 2:
   6.101 +			if(sscanf(buf, "%d\n", &maxval) < 1) {
   6.102 +				return -1;
   6.103 +			}
   6.104 +		default:
   6.105 +			break;
   6.106 +		}
   6.107 +		got_hdrlines++;
   6.108 +	}
   6.109 +
   6.110 +	if(xsz < 1 || ysz < 1 || maxval != 255) {
   6.111 +		return -1;
   6.112 +	}
   6.113 +
   6.114 +	if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
   6.115 +		return -1;
   6.116 +	}
   6.117 +
   6.118 +	if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
   6.119 +		return -1;
   6.120 +	}
   6.121 +	return 0;
   6.122 +}
   6.123 +
   6.124 +static int write(struct img_pixmap *img, struct img_io *io)
   6.125 +{
   6.126 +	int sz;
   6.127 +	char buf[256];
   6.128 +	struct img_pixmap tmpimg;
   6.129 +
   6.130 +	img_init(&tmpimg);
   6.131 +
   6.132 +	if(img->fmt != IMG_FMT_RGB24) {
   6.133 +		if(img_copy(&tmpimg, img) == -1) {
   6.134 +			return -1;
   6.135 +		}
   6.136 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   6.137 +			return -1;
   6.138 +		}
   6.139 +		img = &tmpimg;
   6.140 +	}
   6.141 +
   6.142 +	sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
   6.143 +	if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
   6.144 +		img_destroy(&tmpimg);
   6.145 +		return -1;
   6.146 +	}
   6.147 +
   6.148 +	sz = img->width * img->height * 3;
   6.149 +	if(io->write(img->pixels, sz, io->uptr) < sz) {
   6.150 +		img_destroy(&tmpimg);
   6.151 +		return -1;
   6.152 +	}
   6.153 +
   6.154 +	img_destroy(&tmpimg);
   6.155 +	return 0;
   6.156 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/libs/imago2/ftype_module.c	Thu Sep 08 06:28:38 2011 +0300
     7.3 @@ -0,0 +1,118 @@
     7.4 +/*
     7.5 +libimago - a multi-format image file input/output library.
     7.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     7.7 +
     7.8 +This program is free software: you can redistribute it and/or modify
     7.9 +it under the terms of the GNU Lesser General Public License as published
    7.10 +by the Free Software Foundation, either version 3 of the License, or
    7.11 +(at your option) any later version.
    7.12 +
    7.13 +This program is distributed in the hope that it will be useful,
    7.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.16 +GNU Lesser General Public License for more details.
    7.17 +
    7.18 +You should have received a copy of the GNU Lesser General Public License
    7.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    7.20 +*/
    7.21 +
    7.22 +#include <stdlib.h>
    7.23 +#include <string.h>
    7.24 +#include "ftype_module.h"
    7.25 +
    7.26 +static struct list_node {
    7.27 +	struct ftype_module *module;
    7.28 +	struct list_node *next;
    7.29 +} *modules;
    7.30 +
    7.31 +/* defined in modules.c which is generated by configure */
    7.32 +void img_modules_init();
    7.33 +
    7.34 +static int done_init;
    7.35 +
    7.36 +int img_register_module(struct ftype_module *mod)
    7.37 +{
    7.38 +	struct list_node *node;
    7.39 +
    7.40 +	if(!(node = malloc(sizeof *node))) {
    7.41 +		return -1;
    7.42 +	}
    7.43 +
    7.44 +	node->module = mod;
    7.45 +	node->next = modules;
    7.46 +	modules = node;
    7.47 +	return 0;
    7.48 +}
    7.49 +
    7.50 +struct ftype_module *img_find_format_module(struct img_io *io)
    7.51 +{
    7.52 +	struct list_node *node;
    7.53 +
    7.54 +	if(!done_init) {
    7.55 +		img_modules_init();
    7.56 +		done_init = 1;
    7.57 +	}
    7.58 +
    7.59 +	node = modules;
    7.60 +	while(node) {
    7.61 +		if(node->module->check(io) != -1) {
    7.62 +			return node->module;
    7.63 +		}
    7.64 +		node = node->next;
    7.65 +	}
    7.66 +	return 0;
    7.67 +}
    7.68 +
    7.69 +struct ftype_module *img_guess_format(const char *fname)
    7.70 +{
    7.71 +	struct list_node *node;
    7.72 +	char *suffix;
    7.73 +	int suffix_len;
    7.74 +
    7.75 +	if(!done_init) {
    7.76 +		img_modules_init();
    7.77 +		done_init = 1;
    7.78 +	}
    7.79 +
    7.80 +	if(!(suffix = strrchr(fname, '.'))) {
    7.81 +		return 0;	/* no suffix, can't guess ... */
    7.82 +	}
    7.83 +	suffix_len = strlen(suffix);
    7.84 +
    7.85 +	node = modules;
    7.86 +	while(node) {
    7.87 +		char *suflist = node->module->suffix;
    7.88 +		char *start, *end;
    7.89 +
    7.90 +		while(*suflist) {
    7.91 +			if(!(start = strstr(suflist, suffix))) {
    7.92 +				break;
    7.93 +			}
    7.94 +			end = start + suffix_len;
    7.95 +
    7.96 +			if(*end == ':' || *end == 0) {
    7.97 +				return node->module;	/* found it */
    7.98 +			}
    7.99 +			suflist = end;
   7.100 +		}
   7.101 +
   7.102 +		node = node->next;
   7.103 +	}
   7.104 +	return 0;
   7.105 +}
   7.106 +
   7.107 +struct ftype_module *img_get_module(int idx)
   7.108 +{
   7.109 +	struct list_node *node;
   7.110 +
   7.111 +	if(!done_init) {
   7.112 +		img_modules_init();
   7.113 +		done_init = 1;
   7.114 +	}
   7.115 +
   7.116 +	node = modules;
   7.117 +	while(node && idx--) {
   7.118 +		node = node->next;
   7.119 +	}
   7.120 +	return node->module;
   7.121 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/libs/imago2/ftype_module.h	Thu Sep 08 06:28:38 2011 +0300
     8.3 @@ -0,0 +1,39 @@
     8.4 +/*
     8.5 +libimago - a multi-format image file input/output library.
     8.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     8.7 +
     8.8 +This program is free software: you can redistribute it and/or modify
     8.9 +it under the terms of the GNU Lesser General Public License as published
    8.10 +by the Free Software Foundation, either version 3 of the License, or
    8.11 +(at your option) any later version.
    8.12 +
    8.13 +This program is distributed in the hope that it will be useful,
    8.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.16 +GNU Lesser General Public License for more details.
    8.17 +
    8.18 +You should have received a copy of the GNU Lesser General Public License
    8.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    8.20 +*/
    8.21 +
    8.22 +#ifndef FTYPE_MODULE_H_
    8.23 +#define FTYPE_MODULE_H_
    8.24 +
    8.25 +#include "imago2.h"
    8.26 +
    8.27 +struct ftype_module {
    8.28 +	char *suffix;	/* used for format autodetection during saving only */
    8.29 +
    8.30 +	int (*check)(struct img_io *io);
    8.31 +	int (*read)(struct img_pixmap *img, struct img_io *io);
    8.32 +	int (*write)(struct img_pixmap *img, struct img_io *io);
    8.33 +};
    8.34 +
    8.35 +int img_register_module(struct ftype_module *mod);
    8.36 +
    8.37 +struct ftype_module *img_find_format_module(struct img_io *io);
    8.38 +struct ftype_module *img_guess_format(const char *fname);
    8.39 +struct ftype_module *img_get_module(int idx);
    8.40 +
    8.41 +
    8.42 +#endif	/* FTYPE_MODULE_H_ */
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/libs/imago2/imago2.c	Thu Sep 08 06:28:38 2011 +0300
     9.3 @@ -0,0 +1,343 @@
     9.4 +/*
     9.5 +libimago - a multi-format image file input/output library.
     9.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     9.7 +
     9.8 +This program is free software: you can redistribute it and/or modify
     9.9 +it under the terms of the GNU Lesser General Public License as published
    9.10 +by the Free Software Foundation, either version 3 of the License, or
    9.11 +(at your option) any later version.
    9.12 +
    9.13 +This program is distributed in the hope that it will be useful,
    9.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    9.16 +GNU Lesser General Public License for more details.
    9.17 +
    9.18 +You should have received a copy of the GNU Lesser General Public License
    9.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    9.20 +*/
    9.21 +
    9.22 +#include <stdio.h>
    9.23 +#include <stdlib.h>
    9.24 +#include <string.h>
    9.25 +#include "imago2.h"
    9.26 +#include "ftype_module.h"
    9.27 +
    9.28 +static int pixel_size(enum img_fmt fmt);
    9.29 +static size_t def_read(void *buf, size_t bytes, void *uptr);
    9.30 +static size_t def_write(void *buf, size_t bytes, void *uptr);
    9.31 +static long def_seek(long offset, int whence, void *uptr);
    9.32 +
    9.33 +
    9.34 +void img_init(struct img_pixmap *img)
    9.35 +{
    9.36 +	img->pixels = 0;
    9.37 +	img->width = img->height = 0;
    9.38 +	img->fmt = IMG_FMT_RGBA32;
    9.39 +	img->pixelsz = pixel_size(img->fmt);
    9.40 +	img->name = 0;
    9.41 +}
    9.42 +
    9.43 +
    9.44 +void img_destroy(struct img_pixmap *img)
    9.45 +{
    9.46 +	free(img->pixels);
    9.47 +	img->pixels = 0;	/* just in case... */
    9.48 +	img->width = img->height = 0xbadbeef;
    9.49 +	free(img->name);
    9.50 +}
    9.51 +
    9.52 +struct img_pixmap *img_create(void)
    9.53 +{
    9.54 +	struct img_pixmap *p;
    9.55 +
    9.56 +	if(!(p = malloc(sizeof *p))) {
    9.57 +		return 0;
    9.58 +	}
    9.59 +	img_init(p);
    9.60 +	return p;
    9.61 +}
    9.62 +
    9.63 +void img_free(struct img_pixmap *img)
    9.64 +{
    9.65 +	img_destroy(img);
    9.66 +	free(img);
    9.67 +}
    9.68 +
    9.69 +int img_set_name(struct img_pixmap *img, const char *name)
    9.70 +{
    9.71 +	char *tmp;
    9.72 +
    9.73 +	if(!(tmp = malloc(strlen(name) + 1))) {
    9.74 +		return -1;
    9.75 +	}
    9.76 +	strcpy(tmp, name);
    9.77 +	img->name = tmp;
    9.78 +	return 0;
    9.79 +}
    9.80 +
    9.81 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
    9.82 +{
    9.83 +	if(img->pixels) {
    9.84 +		return img_convert(img, fmt);
    9.85 +	}
    9.86 +	img->fmt = fmt;
    9.87 +	return 0;
    9.88 +}
    9.89 +
    9.90 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
    9.91 +{
    9.92 +	return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
    9.93 +}
    9.94 +
    9.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
    9.96 +{
    9.97 +	void *newpix;
    9.98 +	int pixsz = pixel_size(fmt);
    9.99 +
   9.100 +	if(!(newpix = malloc(w * h * pixsz))) {
   9.101 +		return -1;
   9.102 +	}
   9.103 +
   9.104 +	if(pix) {
   9.105 +		memcpy(newpix, pix, w * h * pixsz);
   9.106 +	} else {
   9.107 +		memset(newpix, 0, w * h * pixsz);
   9.108 +	}
   9.109 +
   9.110 +	free(img->pixels);
   9.111 +	img->pixels = newpix;
   9.112 +	img->width = w;
   9.113 +	img->height = h;
   9.114 +	img->pixelsz = pixsz;
   9.115 +	img->fmt = fmt;
   9.116 +	return 0;
   9.117 +}
   9.118 +
   9.119 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
   9.120 +{
   9.121 +	struct img_pixmap img;
   9.122 +
   9.123 +	img_init(&img);
   9.124 +
   9.125 +	if(img_load(&img, fname) == -1) {
   9.126 +		return 0;
   9.127 +	}
   9.128 +	if(img.fmt != fmt) {
   9.129 +		if(img_convert(&img, fmt) == -1) {
   9.130 +			img_destroy(&img);
   9.131 +			return 0;
   9.132 +		}
   9.133 +	}
   9.134 +
   9.135 +	*xsz = img.width;
   9.136 +	*ysz = img.height;
   9.137 +	return img.pixels;
   9.138 +}
   9.139 +
   9.140 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
   9.141 +{
   9.142 +	struct img_pixmap img;
   9.143 +
   9.144 +	img_init(&img);
   9.145 +	img.fmt = fmt;
   9.146 +	img.name = (char*)fname;
   9.147 +	img.width = xsz;
   9.148 +	img.height = ysz;
   9.149 +	img.pixels = pix;
   9.150 +
   9.151 +	return img_save(&img, fname);
   9.152 +}
   9.153 +
   9.154 +void img_free_pixels(void *pix)
   9.155 +{
   9.156 +	free(pix);
   9.157 +}
   9.158 +
   9.159 +int img_load(struct img_pixmap *img, const char *fname)
   9.160 +{
   9.161 +	int res;
   9.162 +	FILE *fp;
   9.163 +
   9.164 +	if(!(fp = fopen(fname, "rb"))) {
   9.165 +		return -1;
   9.166 +	}
   9.167 +	res = img_read_file(img, fp);
   9.168 +	fclose(fp);
   9.169 +	return res;
   9.170 +}
   9.171 +
   9.172 +/* TODO implement filetype selection */
   9.173 +int img_save(struct img_pixmap *img, const char *fname)
   9.174 +{
   9.175 +	int res;
   9.176 +	FILE *fp;
   9.177 +
   9.178 +	img_set_name(img, fname);
   9.179 +
   9.180 +	if(!(fp = fopen(fname, "wb"))) {
   9.181 +		return -1;
   9.182 +	}
   9.183 +	res = img_write_file(img, fp);
   9.184 +	fclose(fp);
   9.185 +	return res;
   9.186 +}
   9.187 +
   9.188 +int img_read_file(struct img_pixmap *img, FILE *fp)
   9.189 +{
   9.190 +	struct img_io io = {0, def_read, def_write, def_seek};
   9.191 +
   9.192 +	io.uptr = fp;
   9.193 +	return img_read(img, &io);
   9.194 +}
   9.195 +
   9.196 +int img_write_file(struct img_pixmap *img, FILE *fp)
   9.197 +{
   9.198 +	struct img_io io = {0, def_read, def_write, def_seek};
   9.199 +
   9.200 +	io.uptr = fp;
   9.201 +	return img_write(img, &io);
   9.202 +}
   9.203 +
   9.204 +int img_read(struct img_pixmap *img, struct img_io *io)
   9.205 +{
   9.206 +	struct ftype_module *mod;
   9.207 +
   9.208 +	if((mod = img_find_format_module(io))) {
   9.209 +		return mod->read(img, io);
   9.210 +	}
   9.211 +	return -1;
   9.212 +}
   9.213 +
   9.214 +int img_write(struct img_pixmap *img, struct img_io *io)
   9.215 +{
   9.216 +	struct ftype_module *mod;
   9.217 +
   9.218 +	if(!img->name || !(mod = img_guess_format(img->name))) {
   9.219 +		/* TODO throw some sort of warning? */
   9.220 +		/* TODO implement some sort of module priority or let the user specify? */
   9.221 +		if(!(mod = img_get_module(0))) {
   9.222 +			return -1;
   9.223 +		}
   9.224 +	}
   9.225 +
   9.226 +	return mod->write(img, io);
   9.227 +}
   9.228 +
   9.229 +int img_to_float(struct img_pixmap *img)
   9.230 +{
   9.231 +	enum img_fmt targ_fmt;
   9.232 +
   9.233 +	switch(img->fmt) {
   9.234 +	case IMG_FMT_GREY8:
   9.235 +		targ_fmt = IMG_FMT_GREYF;
   9.236 +		break;
   9.237 +
   9.238 +	case IMG_FMT_RGB24:
   9.239 +		targ_fmt = IMG_FMT_RGBF;
   9.240 +		break;
   9.241 +
   9.242 +	case IMG_FMT_RGBA32:
   9.243 +		targ_fmt = IMG_FMT_RGBAF;
   9.244 +		break;
   9.245 +
   9.246 +	default:
   9.247 +		return 0;	/* already float */
   9.248 +	}
   9.249 +
   9.250 +	return img_convert(img, targ_fmt);
   9.251 +}
   9.252 +
   9.253 +int img_to_integer(struct img_pixmap *img)
   9.254 +{
   9.255 +	enum img_fmt targ_fmt;
   9.256 +
   9.257 +	switch(img->fmt) {
   9.258 +	case IMG_FMT_GREYF:
   9.259 +		targ_fmt = IMG_FMT_GREY8;
   9.260 +		break;
   9.261 +
   9.262 +	case IMG_FMT_RGBF:
   9.263 +		targ_fmt = IMG_FMT_RGB24;
   9.264 +		break;
   9.265 +
   9.266 +	case IMG_FMT_RGBAF:
   9.267 +		targ_fmt = IMG_FMT_RGBA32;
   9.268 +		break;
   9.269 +
   9.270 +	default:
   9.271 +		return 0;	/* already integer */
   9.272 +	}
   9.273 +
   9.274 +	return img_convert(img, targ_fmt);
   9.275 +}
   9.276 +
   9.277 +int img_is_float(struct img_pixmap *img)
   9.278 +{
   9.279 +	return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
   9.280 +}
   9.281 +
   9.282 +int img_has_alpha(struct img_pixmap *img)
   9.283 +{
   9.284 +	return img->fmt >= IMG_FMT_GREY8 && img->fmt <= IMG_FMT_RGBA32;
   9.285 +}
   9.286 +
   9.287 +void img_io_set_user_data(struct img_io *io, void *uptr)
   9.288 +{
   9.289 +	io->uptr = uptr;
   9.290 +}
   9.291 +
   9.292 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
   9.293 +{
   9.294 +	io->read = read;
   9.295 +}
   9.296 +
   9.297 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
   9.298 +{
   9.299 +	io->write = write;
   9.300 +}
   9.301 +
   9.302 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
   9.303 +{
   9.304 +	io->seek = seek;
   9.305 +}
   9.306 +
   9.307 +
   9.308 +static int pixel_size(enum img_fmt fmt)
   9.309 +{
   9.310 +	switch(fmt) {
   9.311 +	case IMG_FMT_GREY8:
   9.312 +		return 1;
   9.313 +	case IMG_FMT_RGB24:
   9.314 +		return 3;
   9.315 +	case IMG_FMT_RGBA32:
   9.316 +		return 4;
   9.317 +	case IMG_FMT_GREYF:
   9.318 +		return sizeof(float);
   9.319 +	case IMG_FMT_RGBF:
   9.320 +		return 3 * sizeof(float);
   9.321 +	case IMG_FMT_RGBAF:
   9.322 +		return 4 * sizeof(float);
   9.323 +	default:
   9.324 +		break;
   9.325 +	}
   9.326 +	return 0;
   9.327 +}
   9.328 +
   9.329 +static size_t def_read(void *buf, size_t bytes, void *uptr)
   9.330 +{
   9.331 +	return uptr ? fread(buf, 1, bytes, uptr) : 0;
   9.332 +}
   9.333 +
   9.334 +static size_t def_write(void *buf, size_t bytes, void *uptr)
   9.335 +{
   9.336 +	return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
   9.337 +}
   9.338 +
   9.339 +static long def_seek(long offset, int whence, void *uptr)
   9.340 +{
   9.341 +	if(!uptr || fseek(uptr, offset, whence) == -1) {
   9.342 +		return -1;
   9.343 +	}
   9.344 +	return ftell(uptr);
   9.345 +}
   9.346 +
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/libs/imago2/imago2.h	Thu Sep 08 06:28:38 2011 +0300
    10.3 @@ -0,0 +1,185 @@
    10.4 +/*
    10.5 +libimago - a multi-format image file input/output library.
    10.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    10.7 +
    10.8 +This program is free software: you can redistribute it and/or modify
    10.9 +it under the terms of the GNU Lesser General Public License as published
   10.10 +by the Free Software Foundation, either version 3 of the License, or
   10.11 +(at your option) any later version.
   10.12 +
   10.13 +This program is distributed in the hope that it will be useful,
   10.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.16 +GNU Lesser General Public License for more details.
   10.17 +
   10.18 +You should have received a copy of the GNU Lesser General Public License
   10.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   10.20 +*/
   10.21 +
   10.22 +#ifndef IMAGO2_H_
   10.23 +#define IMAGO2_H_
   10.24 +
   10.25 +#include <stdio.h>
   10.26 +
   10.27 +#ifdef __cplusplus
   10.28 +#define IMG_OPTARG(arg, val)	arg = val
   10.29 +#else
   10.30 +#define IMG_OPTARG(arg, val)	arg
   10.31 +#endif
   10.32 +
   10.33 +/* XXX if you change this make sure to also change pack/unpack arrays in conv.c */
   10.34 +enum img_fmt {
   10.35 +	IMG_FMT_GREY8,
   10.36 +	IMG_FMT_RGB24,
   10.37 +	IMG_FMT_RGBA32,
   10.38 +	IMG_FMT_GREYF,
   10.39 +	IMG_FMT_RGBF,
   10.40 +	IMG_FMT_RGBAF,
   10.41 +
   10.42 +	NUM_IMG_FMT
   10.43 +};
   10.44 +
   10.45 +struct img_pixmap {
   10.46 +	void *pixels;
   10.47 +	int width, height;
   10.48 +	enum img_fmt fmt;
   10.49 +	int pixelsz;
   10.50 +	char *name;
   10.51 +};
   10.52 +
   10.53 +struct img_io {
   10.54 +	void *uptr;	/* user-data */
   10.55 +
   10.56 +	size_t (*read)(void *buf, size_t bytes, void *uptr);
   10.57 +	size_t (*write)(void *buf, size_t bytes, void *uptr);
   10.58 +	long (*seek)(long offs, int whence, void *uptr);
   10.59 +};
   10.60 +
   10.61 +#ifdef __cplusplus
   10.62 +extern "C" {
   10.63 +#endif
   10.64 +
   10.65 +/* initialize the img_pixmap structure */
   10.66 +void img_init(struct img_pixmap *img);
   10.67 +/* destroys the img_pixmap structure, freeing the pixel buffer (if available)
   10.68 + * and any other memory held by the pixmap.
   10.69 + */
   10.70 +void img_destroy(struct img_pixmap *img);
   10.71 +
   10.72 +/* convenience function that allocates an img_pixmap struct and then initializes it.
   10.73 + * returns null if the malloc fails.
   10.74 + */
   10.75 +struct img_pixmap *img_create(void);
   10.76 +/* frees a pixmap previously allocated with img_create (free followed by img_destroy) */
   10.77 +void img_free(struct img_pixmap *img);
   10.78 +
   10.79 +int img_set_name(struct img_pixmap *img, const char *name);
   10.80 +
   10.81 +/* set the image pixel format */
   10.82 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt);
   10.83 +
   10.84 +/* copies one pixmap to another.
   10.85 + * equivalent to: img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels)
   10.86 + */
   10.87 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src);
   10.88 +
   10.89 +/* allocates a pixel buffer of the specified dimensions and format, and copies the
   10.90 + * pixels given through the pix pointer into it.
   10.91 + * the pix pointer can be null, in which case there's no copy, just allocation.
   10.92 + *
   10.93 + * C++: fmt and pix have default parameters IMG_FMT_RGBA32 and null respectively.
   10.94 + */
   10.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32), IMG_OPTARG(void *pix, 0));
   10.96 +
   10.97 +/* Simplified image loading
   10.98 + * Loads the specified file, and returns a pointer to an array of pixels of the
   10.99 + * requested pixel format. The width and height of the image are returned through
  10.100 + * the xsz and ysz pointers.
  10.101 + * If the image cannot be loaded, the function returns null.
  10.102 + *
  10.103 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  10.104 + */
  10.105 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  10.106 +
  10.107 +/* Simplified image saving
  10.108 + * Reads an array of pixels supplied through the pix pointer, of dimensions xsz
  10.109 + * and ysz, and pixel-format fmt, and saves it to a file.
  10.110 + * The output filetype is guessed by the filename suffix.
  10.111 + *
  10.112 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  10.113 + */
  10.114 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  10.115 +
  10.116 +/* Frees the memory allocated by img_load_pixels */
  10.117 +void img_free_pixels(void *pix);
  10.118 +
  10.119 +/* Loads an image file into the supplied pixmap */
  10.120 +int img_load(struct img_pixmap *img, const char *fname);
  10.121 +/* Saves the supplied pixmap to a file. The output filetype is guessed by the filename suffix */
  10.122 +int img_save(struct img_pixmap *img, const char *fname);
  10.123 +
  10.124 +/* Reads an image from an open FILE* into the supplied pixmap */
  10.125 +int img_read_file(struct img_pixmap *img, FILE *fp);
  10.126 +/* Writes the supplied pixmap to an open FILE* */
  10.127 +int img_write_file(struct img_pixmap *img, FILE *fp);
  10.128 +
  10.129 +/* Reads an image using user-defined file-i/o functions (see img_io_set_*) */
  10.130 +int img_read(struct img_pixmap *img, struct img_io *io);
  10.131 +/* Writes an image using user-defined file-i/o functions (see img_io_set_*) */
  10.132 +int img_write(struct img_pixmap *img, struct img_io *io);
  10.133 +
  10.134 +/* Converts an image to the specified pixel format */
  10.135 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt);
  10.136 +
  10.137 +/* Converts an image from an integer pixel format to the corresponding floating point one */
  10.138 +int img_to_float(struct img_pixmap *img);
  10.139 +/* Converts an image from a floating point pixel format to the corresponding integer one */
  10.140 +int img_to_integer(struct img_pixmap *img);
  10.141 +
  10.142 +/* Returns non-zero (true) if the supplied image is in a floating point pixel format */
  10.143 +int img_is_float(struct img_pixmap *img);
  10.144 +/* Returns non-zero (true) if the supplied image has an alpha channel */
  10.145 +int img_has_alpha(struct img_pixmap *img);
  10.146 +
  10.147 +
  10.148 +/* These functions can be used to fill an img_io struct before it's passed to
  10.149 + * one of the user-defined i/o image reading/writing functions (img_read/img_write).
  10.150 + *
  10.151 + * User-defined i/o functions:
  10.152 + *
  10.153 + * - size_t read_func(void *buffer, size_t bytes, void *user_ptr)
  10.154 + * Must try to fill the buffer with the specified number of bytes, and return
  10.155 + * the number of bytes actually read.
  10.156 + *
  10.157 + * - size_t write_func(void *buffer, size_t bytes, void *user_ptr)
  10.158 + * Must write the specified number of bytes from the supplied buffer and return
  10.159 + * the number of bytes actually written.
  10.160 + *
  10.161 + * - long seek_func(long offset, int whence, void *user_ptr)
  10.162 + * Must seek offset bytes from: the beginning of the file if whence is SEEK_SET,
  10.163 + * the current position if whence is SEEK_CUR, or the end of the file if whence is
  10.164 + * SEEK_END, and return the resulting file offset from the beginning of the file.
  10.165 + * (i.e. seek_func(0, SEEK_CUR, user_ptr); must be equivalent to an ftell).
  10.166 + *
  10.167 + * All three functions get the user-data pointer set through img_io_set_user_data
  10.168 + * as their last argument.
  10.169 + *
  10.170 + * Note: obviously you don't need to set a write function if you're only going
  10.171 + * to call img_read, or the read and seek function if you're only going to call
  10.172 + * img_write.
  10.173 + *
  10.174 + * Note: if the user-supplied write function is buffered, make sure to flush
  10.175 + * (or close the file) after img_write returns.
  10.176 + */
  10.177 +void img_io_set_user_data(struct img_io *io, void *uptr);
  10.178 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*));
  10.179 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*));
  10.180 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*));
  10.181 +
  10.182 +
  10.183 +#ifdef __cplusplus
  10.184 +}
  10.185 +#endif
  10.186 +
  10.187 +
  10.188 +#endif	/* IMAGO_H_ */
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/libs/imago2/modules.c	Thu Sep 08 06:28:38 2011 +0300
    11.3 @@ -0,0 +1,11 @@
    11.4 +/* this file is generated by ./configure, do not edit */
    11.5 +int img_register_jpeg();
    11.6 +int img_register_png();
    11.7 +int img_register_ppm();
    11.8 +
    11.9 +void img_modules_init(void)
   11.10 +{
   11.11 +	img_register_jpeg();
   11.12 +	img_register_png();
   11.13 +	img_register_ppm();
   11.14 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/libs/libjpeg/cderror.h	Thu Sep 08 06:28:38 2011 +0300
    12.3 @@ -0,0 +1,132 @@
    12.4 +/*
    12.5 + * cderror.h
    12.6 + *
    12.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    12.8 + * This file is part of the Independent JPEG Group's software.
    12.9 + * For conditions of distribution and use, see the accompanying README file.
   12.10 + *
   12.11 + * This file defines the error and message codes for the cjpeg/djpeg
   12.12 + * applications.  These strings are not needed as part of the JPEG library
   12.13 + * proper.
   12.14 + * Edit this file to add new codes, or to translate the message strings to
   12.15 + * some other language.
   12.16 + */
   12.17 +
   12.18 +/*
   12.19 + * To define the enum list of message codes, include this file without
   12.20 + * defining macro JMESSAGE.  To create a message string table, include it
   12.21 + * again with a suitable JMESSAGE definition (see jerror.c for an example).
   12.22 + */
   12.23 +#ifndef JMESSAGE
   12.24 +#ifndef CDERROR_H
   12.25 +#define CDERROR_H
   12.26 +/* First time through, define the enum list */
   12.27 +#define JMAKE_ENUM_LIST
   12.28 +#else
   12.29 +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
   12.30 +#define JMESSAGE(code,string)
   12.31 +#endif /* CDERROR_H */
   12.32 +#endif /* JMESSAGE */
   12.33 +
   12.34 +#ifdef JMAKE_ENUM_LIST
   12.35 +
   12.36 +typedef enum {
   12.37 +
   12.38 +#define JMESSAGE(code,string)	code ,
   12.39 +
   12.40 +#endif /* JMAKE_ENUM_LIST */
   12.41 +
   12.42 +JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
   12.43 +
   12.44 +#ifdef BMP_SUPPORTED
   12.45 +JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
   12.46 +JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
   12.47 +JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
   12.48 +JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
   12.49 +JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
   12.50 +JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
   12.51 +JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
   12.52 +JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
   12.53 +JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
   12.54 +JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
   12.55 +JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
   12.56 +#endif /* BMP_SUPPORTED */
   12.57 +
   12.58 +#ifdef GIF_SUPPORTED
   12.59 +JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
   12.60 +JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
   12.61 +JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
   12.62 +JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
   12.63 +JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
   12.64 +JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
   12.65 +JMESSAGE(JTRC_GIF_BADVERSION,
   12.66 +	 "Warning: unexpected GIF version number '%c%c%c'")
   12.67 +JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
   12.68 +JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
   12.69 +JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
   12.70 +JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
   12.71 +JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
   12.72 +JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
   12.73 +#endif /* GIF_SUPPORTED */
   12.74 +
   12.75 +#ifdef PPM_SUPPORTED
   12.76 +JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
   12.77 +JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
   12.78 +JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
   12.79 +JMESSAGE(JTRC_PGM, "%ux%u PGM image")
   12.80 +JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
   12.81 +JMESSAGE(JTRC_PPM, "%ux%u PPM image")
   12.82 +JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
   12.83 +#endif /* PPM_SUPPORTED */
   12.84 +
   12.85 +#ifdef RLE_SUPPORTED
   12.86 +JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
   12.87 +JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
   12.88 +JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
   12.89 +JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
   12.90 +JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
   12.91 +JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
   12.92 +JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
   12.93 +JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
   12.94 +JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
   12.95 +JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
   12.96 +JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
   12.97 +JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
   12.98 +JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
   12.99 +JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
  12.100 +#endif /* RLE_SUPPORTED */
  12.101 +
  12.102 +#ifdef TARGA_SUPPORTED
  12.103 +JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
  12.104 +JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
  12.105 +JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
  12.106 +JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
  12.107 +JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
  12.108 +JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
  12.109 +#else
  12.110 +JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
  12.111 +#endif /* TARGA_SUPPORTED */
  12.112 +
  12.113 +JMESSAGE(JERR_BAD_CMAP_FILE,
  12.114 +	 "Color map file is invalid or of unsupported format")
  12.115 +JMESSAGE(JERR_TOO_MANY_COLORS,
  12.116 +	 "Output file format cannot handle %d colormap entries")
  12.117 +JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
  12.118 +#ifdef TARGA_SUPPORTED
  12.119 +JMESSAGE(JERR_UNKNOWN_FORMAT,
  12.120 +	 "Unrecognized input file format --- perhaps you need -targa")
  12.121 +#else
  12.122 +JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
  12.123 +#endif
  12.124 +JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
  12.125 +
  12.126 +#ifdef JMAKE_ENUM_LIST
  12.127 +
  12.128 +  JMSG_LASTADDONCODE
  12.129 +} ADDON_MESSAGE_CODE;
  12.130 +
  12.131 +#undef JMAKE_ENUM_LIST
  12.132 +#endif /* JMAKE_ENUM_LIST */
  12.133 +
  12.134 +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
  12.135 +#undef JMESSAGE
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/libs/libjpeg/jcapimin.c	Thu Sep 08 06:28:38 2011 +0300
    13.3 @@ -0,0 +1,280 @@
    13.4 +/*
    13.5 + * jcapimin.c
    13.6 + *
    13.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    13.8 + * This file is part of the Independent JPEG Group's software.
    13.9 + * For conditions of distribution and use, see the accompanying README file.
   13.10 + *
   13.11 + * This file contains application interface code for the compression half
   13.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   13.13 + * needed in either the normal full-compression case or the transcoding-only
   13.14 + * case.
   13.15 + *
   13.16 + * Most of the routines intended to be called directly by an application
   13.17 + * are in this file or in jcapistd.c.  But also see jcparam.c for
   13.18 + * parameter-setup helper routines, jcomapi.c for routines shared by
   13.19 + * compression and decompression, and jctrans.c for the transcoding case.
   13.20 + */
   13.21 +
   13.22 +#define JPEG_INTERNALS
   13.23 +#include "jinclude.h"
   13.24 +#include "jpeglib.h"
   13.25 +
   13.26 +
   13.27 +/*
   13.28 + * Initialization of a JPEG compression object.
   13.29 + * The error manager must already be set up (in case memory manager fails).
   13.30 + */
   13.31 +
   13.32 +GLOBAL(void)
   13.33 +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
   13.34 +{
   13.35 +  int i;
   13.36 +
   13.37 +  /* Guard against version mismatches between library and caller. */
   13.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   13.39 +  if (version != JPEG_LIB_VERSION)
   13.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   13.41 +  if (structsize != SIZEOF(struct jpeg_compress_struct))
   13.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   13.43 +	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
   13.44 +
   13.45 +  /* For debugging purposes, we zero the whole master structure.
   13.46 +   * But the application has already set the err pointer, and may have set
   13.47 +   * client_data, so we have to save and restore those fields.
   13.48 +   * Note: if application hasn't set client_data, tools like Purify may
   13.49 +   * complain here.
   13.50 +   */
   13.51 +  {
   13.52 +    struct jpeg_error_mgr * err = cinfo->err;
   13.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   13.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
   13.55 +    cinfo->err = err;
   13.56 +    cinfo->client_data = client_data;
   13.57 +  }
   13.58 +  cinfo->is_decompressor = FALSE;
   13.59 +
   13.60 +  /* Initialize a memory manager instance for this object */
   13.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   13.62 +
   13.63 +  /* Zero out pointers to permanent structures. */
   13.64 +  cinfo->progress = NULL;
   13.65 +  cinfo->dest = NULL;
   13.66 +
   13.67 +  cinfo->comp_info = NULL;
   13.68 +
   13.69 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   13.70 +    cinfo->quant_tbl_ptrs[i] = NULL;
   13.71 +
   13.72 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   13.73 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   13.74 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   13.75 +  }
   13.76 +
   13.77 +  cinfo->script_space = NULL;
   13.78 +
   13.79 +  cinfo->input_gamma = 1.0;	/* in case application forgets */
   13.80 +
   13.81 +  /* OK, I'm ready */
   13.82 +  cinfo->global_state = CSTATE_START;
   13.83 +}
   13.84 +
   13.85 +
   13.86 +/*
   13.87 + * Destruction of a JPEG compression object
   13.88 + */
   13.89 +
   13.90 +GLOBAL(void)
   13.91 +jpeg_destroy_compress (j_compress_ptr cinfo)
   13.92 +{
   13.93 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   13.94 +}
   13.95 +
   13.96 +
   13.97 +/*
   13.98 + * Abort processing of a JPEG compression operation,
   13.99 + * but don't destroy the object itself.
  13.100 + */
  13.101 +
  13.102 +GLOBAL(void)
  13.103 +jpeg_abort_compress (j_compress_ptr cinfo)
  13.104 +{
  13.105 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  13.106 +}
  13.107 +
  13.108 +
  13.109 +/*
  13.110 + * Forcibly suppress or un-suppress all quantization and Huffman tables.
  13.111 + * Marks all currently defined tables as already written (if suppress)
  13.112 + * or not written (if !suppress).  This will control whether they get emitted
  13.113 + * by a subsequent jpeg_start_compress call.
  13.114 + *
  13.115 + * This routine is exported for use by applications that want to produce
  13.116 + * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  13.117 + * since it is called by jpeg_start_compress, we put it here --- otherwise
  13.118 + * jcparam.o would be linked whether the application used it or not.
  13.119 + */
  13.120 +
  13.121 +GLOBAL(void)
  13.122 +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  13.123 +{
  13.124 +  int i;
  13.125 +  JQUANT_TBL * qtbl;
  13.126 +  JHUFF_TBL * htbl;
  13.127 +
  13.128 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  13.129 +    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  13.130 +      qtbl->sent_table = suppress;
  13.131 +  }
  13.132 +
  13.133 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  13.134 +    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  13.135 +      htbl->sent_table = suppress;
  13.136 +    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  13.137 +      htbl->sent_table = suppress;
  13.138 +  }
  13.139 +}
  13.140 +
  13.141 +
  13.142 +/*
  13.143 + * Finish JPEG compression.
  13.144 + *
  13.145 + * If a multipass operating mode was selected, this may do a great deal of
  13.146 + * work including most of the actual output.
  13.147 + */
  13.148 +
  13.149 +GLOBAL(void)
  13.150 +jpeg_finish_compress (j_compress_ptr cinfo)
  13.151 +{
  13.152 +  JDIMENSION iMCU_row;
  13.153 +
  13.154 +  if (cinfo->global_state == CSTATE_SCANNING ||
  13.155 +      cinfo->global_state == CSTATE_RAW_OK) {
  13.156 +    /* Terminate first pass */
  13.157 +    if (cinfo->next_scanline < cinfo->image_height)
  13.158 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  13.159 +    (*cinfo->master->finish_pass) (cinfo);
  13.160 +  } else if (cinfo->global_state != CSTATE_WRCOEFS)
  13.161 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  13.162 +  /* Perform any remaining passes */
  13.163 +  while (! cinfo->master->is_last_pass) {
  13.164 +    (*cinfo->master->prepare_for_pass) (cinfo);
  13.165 +    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  13.166 +      if (cinfo->progress != NULL) {
  13.167 +	cinfo->progress->pass_counter = (long) iMCU_row;
  13.168 +	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  13.169 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  13.170 +      }
  13.171 +      /* We bypass the main controller and invoke coef controller directly;
  13.172 +       * all work is being done from the coefficient buffer.
  13.173 +       */
  13.174 +      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  13.175 +	ERREXIT(cinfo, JERR_CANT_SUSPEND);
  13.176 +    }
  13.177 +    (*cinfo->master->finish_pass) (cinfo);
  13.178 +  }
  13.179 +  /* Write EOI, do final cleanup */
  13.180 +  (*cinfo->marker->write_file_trailer) (cinfo);
  13.181 +  (*cinfo->dest->term_destination) (cinfo);
  13.182 +  /* We can use jpeg_abort to release memory and reset global_state */
  13.183 +  jpeg_abort((j_common_ptr) cinfo);
  13.184 +}
  13.185 +
  13.186 +
  13.187 +/*
  13.188 + * Write a special marker.
  13.189 + * This is only recommended for writing COM or APPn markers.
  13.190 + * Must be called after jpeg_start_compress() and before
  13.191 + * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  13.192 + */
  13.193 +
  13.194 +GLOBAL(void)
  13.195 +jpeg_write_marker (j_compress_ptr cinfo, int marker,
  13.196 +		   const JOCTET *dataptr, unsigned int datalen)
  13.197 +{
  13.198 +  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
  13.199 +
  13.200 +  if (cinfo->next_scanline != 0 ||
  13.201 +      (cinfo->global_state != CSTATE_SCANNING &&
  13.202 +       cinfo->global_state != CSTATE_RAW_OK &&
  13.203 +       cinfo->global_state != CSTATE_WRCOEFS))
  13.204 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  13.205 +
  13.206 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  13.207 +  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
  13.208 +  while (datalen--) {
  13.209 +    (*write_marker_byte) (cinfo, *dataptr);
  13.210 +    dataptr++;
  13.211 +  }
  13.212 +}
  13.213 +
  13.214 +/* Same, but piecemeal. */
  13.215 +
  13.216 +GLOBAL(void)
  13.217 +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  13.218 +{
  13.219 +  if (cinfo->next_scanline != 0 ||
  13.220 +      (cinfo->global_state != CSTATE_SCANNING &&
  13.221 +       cinfo->global_state != CSTATE_RAW_OK &&
  13.222 +       cinfo->global_state != CSTATE_WRCOEFS))
  13.223 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  13.224 +
  13.225 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  13.226 +}
  13.227 +
  13.228 +GLOBAL(void)
  13.229 +jpeg_write_m_byte (j_compress_ptr cinfo, int val)
  13.230 +{
  13.231 +  (*cinfo->marker->write_marker_byte) (cinfo, val);
  13.232 +}
  13.233 +
  13.234 +
  13.235 +/*
  13.236 + * Alternate compression function: just write an abbreviated table file.
  13.237 + * Before calling this, all parameters and a data destination must be set up.
  13.238 + *
  13.239 + * To produce a pair of files containing abbreviated tables and abbreviated
  13.240 + * image data, one would proceed as follows:
  13.241 + *
  13.242 + *		initialize JPEG object
  13.243 + *		set JPEG parameters
  13.244 + *		set destination to table file
  13.245 + *		jpeg_write_tables(cinfo);
  13.246 + *		set destination to image file
  13.247 + *		jpeg_start_compress(cinfo, FALSE);
  13.248 + *		write data...
  13.249 + *		jpeg_finish_compress(cinfo);
  13.250 + *
  13.251 + * jpeg_write_tables has the side effect of marking all tables written
  13.252 + * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  13.253 + * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  13.254 + */
  13.255 +
  13.256 +GLOBAL(void)
  13.257 +jpeg_write_tables (j_compress_ptr cinfo)
  13.258 +{
  13.259 +  if (cinfo->global_state != CSTATE_START)
  13.260 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  13.261 +
  13.262 +  /* (Re)initialize error mgr and destination modules */
  13.263 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  13.264 +  (*cinfo->dest->init_destination) (cinfo);
  13.265 +  /* Initialize the marker writer ... bit of a crock to do it here. */
  13.266 +  jinit_marker_writer(cinfo);
  13.267 +  /* Write them tables! */
  13.268 +  (*cinfo->marker->write_tables_only) (cinfo);
  13.269 +  /* And clean up. */
  13.270 +  (*cinfo->dest->term_destination) (cinfo);
  13.271 +  /*
  13.272 +   * In library releases up through v6a, we called jpeg_abort() here to free
  13.273 +   * any working memory allocated by the destination manager and marker
  13.274 +   * writer.  Some applications had a problem with that: they allocated space
  13.275 +   * of their own from the library memory manager, and didn't want it to go
  13.276 +   * away during write_tables.  So now we do nothing.  This will cause a
  13.277 +   * memory leak if an app calls write_tables repeatedly without doing a full
  13.278 +   * compression cycle or otherwise resetting the JPEG object.  However, that
  13.279 +   * seems less bad than unexpectedly freeing memory in the normal case.
  13.280 +   * An app that prefers the old behavior can call jpeg_abort for itself after
  13.281 +   * each call to jpeg_write_tables().
  13.282 +   */
  13.283 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/libs/libjpeg/jcapistd.c	Thu Sep 08 06:28:38 2011 +0300
    14.3 @@ -0,0 +1,161 @@
    14.4 +/*
    14.5 + * jcapistd.c
    14.6 + *
    14.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    14.8 + * This file is part of the Independent JPEG Group's software.
    14.9 + * For conditions of distribution and use, see the accompanying README file.
   14.10 + *
   14.11 + * This file contains application interface code for the compression half
   14.12 + * of the JPEG library.  These are the "standard" API routines that are
   14.13 + * used in the normal full-compression case.  They are not used by a
   14.14 + * transcoding-only application.  Note that if an application links in
   14.15 + * jpeg_start_compress, it will end up linking in the entire compressor.
   14.16 + * We thus must separate this file from jcapimin.c to avoid linking the
   14.17 + * whole compression library into a transcoder.
   14.18 + */
   14.19 +
   14.20 +#define JPEG_INTERNALS
   14.21 +#include "jinclude.h"
   14.22 +#include "jpeglib.h"
   14.23 +
   14.24 +
   14.25 +/*
   14.26 + * Compression initialization.
   14.27 + * Before calling this, all parameters and a data destination must be set up.
   14.28 + *
   14.29 + * We require a write_all_tables parameter as a failsafe check when writing
   14.30 + * multiple datastreams from the same compression object.  Since prior runs
   14.31 + * will have left all the tables marked sent_table=TRUE, a subsequent run
   14.32 + * would emit an abbreviated stream (no tables) by default.  This may be what
   14.33 + * is wanted, but for safety's sake it should not be the default behavior:
   14.34 + * programmers should have to make a deliberate choice to emit abbreviated
   14.35 + * images.  Therefore the documentation and examples should encourage people
   14.36 + * to pass write_all_tables=TRUE; then it will take active thought to do the
   14.37 + * wrong thing.
   14.38 + */
   14.39 +
   14.40 +GLOBAL(void)
   14.41 +jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
   14.42 +{
   14.43 +  if (cinfo->global_state != CSTATE_START)
   14.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   14.45 +
   14.46 +  if (write_all_tables)
   14.47 +    jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
   14.48 +
   14.49 +  /* (Re)initialize error mgr and destination modules */
   14.50 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   14.51 +  (*cinfo->dest->init_destination) (cinfo);
   14.52 +  /* Perform master selection of active modules */
   14.53 +  jinit_compress_master(cinfo);
   14.54 +  /* Set up for the first pass */
   14.55 +  (*cinfo->master->prepare_for_pass) (cinfo);
   14.56 +  /* Ready for application to drive first pass through jpeg_write_scanlines
   14.57 +   * or jpeg_write_raw_data.
   14.58 +   */
   14.59 +  cinfo->next_scanline = 0;
   14.60 +  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
   14.61 +}
   14.62 +
   14.63 +
   14.64 +/*
   14.65 + * Write some scanlines of data to the JPEG compressor.
   14.66 + *
   14.67 + * The return value will be the number of lines actually written.
   14.68 + * This should be less than the supplied num_lines only in case that
   14.69 + * the data destination module has requested suspension of the compressor,
   14.70 + * or if more than image_height scanlines are passed in.
   14.71 + *
   14.72 + * Note: we warn about excess calls to jpeg_write_scanlines() since
   14.73 + * this likely signals an application programmer error.  However,
   14.74 + * excess scanlines passed in the last valid call are *silently* ignored,
   14.75 + * so that the application need not adjust num_lines for end-of-image
   14.76 + * when using a multiple-scanline buffer.
   14.77 + */
   14.78 +
   14.79 +GLOBAL(JDIMENSION)
   14.80 +jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
   14.81 +		      JDIMENSION num_lines)
   14.82 +{
   14.83 +  JDIMENSION row_ctr, rows_left;
   14.84 +
   14.85 +  if (cinfo->global_state != CSTATE_SCANNING)
   14.86 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   14.87 +  if (cinfo->next_scanline >= cinfo->image_height)
   14.88 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
   14.89 +
   14.90 +  /* Call progress monitor hook if present */
   14.91 +  if (cinfo->progress != NULL) {
   14.92 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
   14.93 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
   14.94 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   14.95 +  }
   14.96 +
   14.97 +  /* Give master control module another chance if this is first call to
   14.98 +   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
   14.99 +   * delayed so that application can write COM, etc, markers between
  14.100 +   * jpeg_start_compress and jpeg_write_scanlines.
  14.101 +   */
  14.102 +  if (cinfo->master->call_pass_startup)
  14.103 +    (*cinfo->master->pass_startup) (cinfo);
  14.104 +
  14.105 +  /* Ignore any extra scanlines at bottom of image. */
  14.106 +  rows_left = cinfo->image_height - cinfo->next_scanline;
  14.107 +  if (num_lines > rows_left)
  14.108 +    num_lines = rows_left;
  14.109 +
  14.110 +  row_ctr = 0;
  14.111 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  14.112 +  cinfo->next_scanline += row_ctr;
  14.113 +  return row_ctr;
  14.114 +}
  14.115 +
  14.116 +
  14.117 +/*
  14.118 + * Alternate entry point to write raw data.
  14.119 + * Processes exactly one iMCU row per call, unless suspended.
  14.120 + */
  14.121 +
  14.122 +GLOBAL(JDIMENSION)
  14.123 +jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  14.124 +		     JDIMENSION num_lines)
  14.125 +{
  14.126 +  JDIMENSION lines_per_iMCU_row;
  14.127 +
  14.128 +  if (cinfo->global_state != CSTATE_RAW_OK)
  14.129 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  14.130 +  if (cinfo->next_scanline >= cinfo->image_height) {
  14.131 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  14.132 +    return 0;
  14.133 +  }
  14.134 +
  14.135 +  /* Call progress monitor hook if present */
  14.136 +  if (cinfo->progress != NULL) {
  14.137 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  14.138 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
  14.139 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  14.140 +  }
  14.141 +
  14.142 +  /* Give master control module another chance if this is first call to
  14.143 +   * jpeg_write_raw_data.  This lets output of the frame/scan headers be
  14.144 +   * delayed so that application can write COM, etc, markers between
  14.145 +   * jpeg_start_compress and jpeg_write_raw_data.
  14.146 +   */
  14.147 +  if (cinfo->master->call_pass_startup)
  14.148 +    (*cinfo->master->pass_startup) (cinfo);
  14.149 +
  14.150 +  /* Verify that at least one iMCU row has been passed. */
  14.151 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  14.152 +  if (num_lines < lines_per_iMCU_row)
  14.153 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
  14.154 +
  14.155 +  /* Directly compress the row. */
  14.156 +  if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  14.157 +    /* If compressor did not consume the whole row, suspend processing. */
  14.158 +    return 0;
  14.159 +  }
  14.160 +
  14.161 +  /* OK, we processed one iMCU row. */
  14.162 +  cinfo->next_scanline += lines_per_iMCU_row;
  14.163 +  return lines_per_iMCU_row;
  14.164 +}
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/libs/libjpeg/jccoefct.c	Thu Sep 08 06:28:38 2011 +0300
    15.3 @@ -0,0 +1,449 @@
    15.4 +/*
    15.5 + * jccoefct.c
    15.6 + *
    15.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    15.8 + * This file is part of the Independent JPEG Group's software.
    15.9 + * For conditions of distribution and use, see the accompanying README file.
   15.10 + *
   15.11 + * This file contains the coefficient buffer controller for compression.
   15.12 + * This controller is the top level of the JPEG compressor proper.
   15.13 + * The coefficient buffer lies between forward-DCT and entropy encoding steps.
   15.14 + */
   15.15 +
   15.16 +#define JPEG_INTERNALS
   15.17 +#include "jinclude.h"
   15.18 +#include "jpeglib.h"
   15.19 +
   15.20 +
   15.21 +/* We use a full-image coefficient buffer when doing Huffman optimization,
   15.22 + * and also for writing multiple-scan JPEG files.  In all cases, the DCT
   15.23 + * step is run during the first pass, and subsequent passes need only read
   15.24 + * the buffered coefficients.
   15.25 + */
   15.26 +#ifdef ENTROPY_OPT_SUPPORTED
   15.27 +#define FULL_COEF_BUFFER_SUPPORTED
   15.28 +#else
   15.29 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   15.30 +#define FULL_COEF_BUFFER_SUPPORTED
   15.31 +#endif
   15.32 +#endif
   15.33 +
   15.34 +
   15.35 +/* Private buffer controller object */
   15.36 +
   15.37 +typedef struct {
   15.38 +  struct jpeg_c_coef_controller pub; /* public fields */
   15.39 +
   15.40 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
   15.41 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
   15.42 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
   15.43 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
   15.44 +
   15.45 +  /* For single-pass compression, it's sufficient to buffer just one MCU
   15.46 +   * (although this may prove a bit slow in practice).  We allocate a
   15.47 +   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
   15.48 +   * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
   15.49 +   * it's not really very big; this is to keep the module interfaces unchanged
   15.50 +   * when a large coefficient buffer is necessary.)
   15.51 +   * In multi-pass modes, this array points to the current MCU's blocks
   15.52 +   * within the virtual arrays.
   15.53 +   */
   15.54 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
   15.55 +
   15.56 +  /* In multi-pass modes, we need a virtual block array for each component. */
   15.57 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
   15.58 +} my_coef_controller;
   15.59 +
   15.60 +typedef my_coef_controller * my_coef_ptr;
   15.61 +
   15.62 +
   15.63 +/* Forward declarations */
   15.64 +METHODDEF(boolean) compress_data
   15.65 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   15.66 +#ifdef FULL_COEF_BUFFER_SUPPORTED
   15.67 +METHODDEF(boolean) compress_first_pass
   15.68 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   15.69 +METHODDEF(boolean) compress_output
   15.70 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   15.71 +#endif
   15.72 +
   15.73 +
   15.74 +LOCAL(void)
   15.75 +start_iMCU_row (j_compress_ptr cinfo)
   15.76 +/* Reset within-iMCU-row counters for a new row */
   15.77 +{
   15.78 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   15.79 +
   15.80 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
   15.81 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
   15.82 +   * But at the bottom of the image, process only what's left.
   15.83 +   */
   15.84 +  if (cinfo->comps_in_scan > 1) {
   15.85 +    coef->MCU_rows_per_iMCU_row = 1;
   15.86 +  } else {
   15.87 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
   15.88 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
   15.89 +    else
   15.90 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
   15.91 +  }
   15.92 +
   15.93 +  coef->mcu_ctr = 0;
   15.94 +  coef->MCU_vert_offset = 0;
   15.95 +}
   15.96 +
   15.97 +
   15.98 +/*
   15.99 + * Initialize for a processing pass.
  15.100 + */
  15.101 +
  15.102 +METHODDEF(void)
  15.103 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  15.104 +{
  15.105 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  15.106 +
  15.107 +  coef->iMCU_row_num = 0;
  15.108 +  start_iMCU_row(cinfo);
  15.109 +
  15.110 +  switch (pass_mode) {
  15.111 +  case JBUF_PASS_THRU:
  15.112 +    if (coef->whole_image[0] != NULL)
  15.113 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  15.114 +    coef->pub.compress_data = compress_data;
  15.115 +    break;
  15.116 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  15.117 +  case JBUF_SAVE_AND_PASS:
  15.118 +    if (coef->whole_image[0] == NULL)
  15.119 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  15.120 +    coef->pub.compress_data = compress_first_pass;
  15.121 +    break;
  15.122 +  case JBUF_CRANK_DEST:
  15.123 +    if (coef->whole_image[0] == NULL)
  15.124 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  15.125 +    coef->pub.compress_data = compress_output;
  15.126 +    break;
  15.127 +#endif
  15.128 +  default:
  15.129 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  15.130 +    break;
  15.131 +  }
  15.132 +}
  15.133 +
  15.134 +
  15.135 +/*
  15.136 + * Process some data in the single-pass case.
  15.137 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  15.138 + * per call, ie, v_samp_factor block rows for each component in the image.
  15.139 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  15.140 + *
  15.141 + * NB: input_buf contains a plane for each component in image,
  15.142 + * which we index according to the component's SOF position.
  15.143 + */
  15.144 +
  15.145 +METHODDEF(boolean)
  15.146 +compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  15.147 +{
  15.148 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  15.149 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  15.150 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  15.151 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  15.152 +  int blkn, bi, ci, yindex, yoffset, blockcnt;
  15.153 +  JDIMENSION ypos, xpos;
  15.154 +  jpeg_component_info *compptr;
  15.155 +
  15.156 +  /* Loop to write as much as one whole iMCU row */
  15.157 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  15.158 +       yoffset++) {
  15.159 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  15.160 +	 MCU_col_num++) {
  15.161 +      /* Determine where data comes from in input_buf and do the DCT thing.
  15.162 +       * Each call on forward_DCT processes a horizontal row of DCT blocks
  15.163 +       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  15.164 +       * sequentially.  Dummy blocks at the right or bottom edge are filled in
  15.165 +       * specially.  The data in them does not matter for image reconstruction,
  15.166 +       * so we fill them with values that will encode to the smallest amount of
  15.167 +       * data, viz: all zeroes in the AC entries, DC entries equal to previous
  15.168 +       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
  15.169 +       */
  15.170 +      blkn = 0;
  15.171 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  15.172 +	compptr = cinfo->cur_comp_info[ci];
  15.173 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  15.174 +						: compptr->last_col_width;
  15.175 +	xpos = MCU_col_num * compptr->MCU_sample_width;
  15.176 +	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  15.177 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  15.178 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  15.179 +	      yoffset+yindex < compptr->last_row_height) {
  15.180 +	    (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  15.181 +					 input_buf[compptr->component_index],
  15.182 +					 coef->MCU_buffer[blkn],
  15.183 +					 ypos, xpos, (JDIMENSION) blockcnt);
  15.184 +	    if (blockcnt < compptr->MCU_width) {
  15.185 +	      /* Create some dummy blocks at the right edge of the image. */
  15.186 +	      jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  15.187 +			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  15.188 +	      for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  15.189 +		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  15.190 +	      }
  15.191 +	    }
  15.192 +	  } else {
  15.193 +	    /* Create a row of dummy blocks at the bottom of the image. */
  15.194 +	    jzero_far((void FAR *) coef->MCU_buffer[blkn],
  15.195 +		      compptr->MCU_width * SIZEOF(JBLOCK));
  15.196 +	    for (bi = 0; bi < compptr->MCU_width; bi++) {
  15.197 +	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  15.198 +	    }
  15.199 +	  }
  15.200 +	  blkn += compptr->MCU_width;
  15.201 +	  ypos += DCTSIZE;
  15.202 +	}
  15.203 +      }
  15.204 +      /* Try to write the MCU.  In event of a suspension failure, we will
  15.205 +       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  15.206 +       */
  15.207 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  15.208 +	/* Suspension forced; update state counters and exit */
  15.209 +	coef->MCU_vert_offset = yoffset;
  15.210 +	coef->mcu_ctr = MCU_col_num;
  15.211 +	return FALSE;
  15.212 +      }
  15.213 +    }
  15.214 +    /* Completed an MCU row, but perhaps not an iMCU row */
  15.215 +    coef->mcu_ctr = 0;
  15.216 +  }
  15.217 +  /* Completed the iMCU row, advance counters for next one */
  15.218 +  coef->iMCU_row_num++;
  15.219 +  start_iMCU_row(cinfo);
  15.220 +  return TRUE;
  15.221 +}
  15.222 +
  15.223 +
  15.224 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  15.225 +
  15.226 +/*
  15.227 + * Process some data in the first pass of a multi-pass case.
  15.228 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  15.229 + * per call, ie, v_samp_factor block rows for each component in the image.
  15.230 + * This amount of data is read from the source buffer, DCT'd and quantized,
  15.231 + * and saved into the virtual arrays.  We also generate suitable dummy blocks
  15.232 + * as needed at the right and lower edges.  (The dummy blocks are constructed
  15.233 + * in the virtual arrays, which have been padded appropriately.)  This makes
  15.234 + * it possible for subsequent passes not to worry about real vs. dummy blocks.
  15.235 + *
  15.236 + * We must also emit the data to the entropy encoder.  This is conveniently
  15.237 + * done by calling compress_output() after we've loaded the current strip
  15.238 + * of the virtual arrays.
  15.239 + *
  15.240 + * NB: input_buf contains a plane for each component in image.  All
  15.241 + * components are DCT'd and loaded into the virtual arrays in this pass.
  15.242 + * However, it may be that only a subset of the components are emitted to
  15.243 + * the entropy encoder during this first pass; be careful about looking
  15.244 + * at the scan-dependent variables (MCU dimensions, etc).
  15.245 + */
  15.246 +
  15.247 +METHODDEF(boolean)
  15.248 +compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  15.249 +{
  15.250 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  15.251 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  15.252 +  JDIMENSION blocks_across, MCUs_across, MCUindex;
  15.253 +  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  15.254 +  JCOEF lastDC;
  15.255 +  jpeg_component_info *compptr;
  15.256 +  JBLOCKARRAY buffer;
  15.257 +  JBLOCKROW thisblockrow, lastblockrow;
  15.258 +
  15.259 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  15.260 +       ci++, compptr++) {
  15.261 +    /* Align the virtual buffer for this component. */
  15.262 +    buffer = (*cinfo->mem->access_virt_barray)
  15.263 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
  15.264 +       coef->iMCU_row_num * compptr->v_samp_factor,
  15.265 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
  15.266 +    /* Count non-dummy DCT block rows in this iMCU row. */
  15.267 +    if (coef->iMCU_row_num < last_iMCU_row)
  15.268 +      block_rows = compptr->v_samp_factor;
  15.269 +    else {
  15.270 +      /* NB: can't use last_row_height here, since may not be set! */
  15.271 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  15.272 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  15.273 +    }
  15.274 +    blocks_across = compptr->width_in_blocks;
  15.275 +    h_samp_factor = compptr->h_samp_factor;
  15.276 +    /* Count number of dummy blocks to be added at the right margin. */
  15.277 +    ndummy = (int) (blocks_across % h_samp_factor);
  15.278 +    if (ndummy > 0)
  15.279 +      ndummy = h_samp_factor - ndummy;
  15.280 +    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
  15.281 +     * on forward_DCT processes a complete horizontal row of DCT blocks.
  15.282 +     */
  15.283 +    for (block_row = 0; block_row < block_rows; block_row++) {
  15.284 +      thisblockrow = buffer[block_row];
  15.285 +      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  15.286 +				   input_buf[ci], thisblockrow,
  15.287 +				   (JDIMENSION) (block_row * DCTSIZE),
  15.288 +				   (JDIMENSION) 0, blocks_across);
  15.289 +      if (ndummy > 0) {
  15.290 +	/* Create dummy blocks at the right edge of the image. */
  15.291 +	thisblockrow += blocks_across; /* => first dummy block */
  15.292 +	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  15.293 +	lastDC = thisblockrow[-1][0];
  15.294 +	for (bi = 0; bi < ndummy; bi++) {
  15.295 +	  thisblockrow[bi][0] = lastDC;
  15.296 +	}
  15.297 +      }
  15.298 +    }
  15.299 +    /* If at end of image, create dummy block rows as needed.
  15.300 +     * The tricky part here is that within each MCU, we want the DC values
  15.301 +     * of the dummy blocks to match the last real block's DC value.
  15.302 +     * This squeezes a few more bytes out of the resulting file...
  15.303 +     */
  15.304 +    if (coef->iMCU_row_num == last_iMCU_row) {
  15.305 +      blocks_across += ndummy;	/* include lower right corner */
  15.306 +      MCUs_across = blocks_across / h_samp_factor;
  15.307 +      for (block_row = block_rows; block_row < compptr->v_samp_factor;
  15.308 +	   block_row++) {
  15.309 +	thisblockrow = buffer[block_row];
  15.310 +	lastblockrow = buffer[block_row-1];
  15.311 +	jzero_far((void FAR *) thisblockrow,
  15.312 +		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
  15.313 +	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  15.314 +	  lastDC = lastblockrow[h_samp_factor-1][0];
  15.315 +	  for (bi = 0; bi < h_samp_factor; bi++) {
  15.316 +	    thisblockrow[bi][0] = lastDC;
  15.317 +	  }
  15.318 +	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
  15.319 +	  lastblockrow += h_samp_factor;
  15.320 +	}
  15.321 +      }
  15.322 +    }
  15.323 +  }
  15.324 +  /* NB: compress_output will increment iMCU_row_num if successful.
  15.325 +   * A suspension return will result in redoing all the work above next time.
  15.326 +   */
  15.327 +
  15.328 +  /* Emit data to the entropy encoder, sharing code with subsequent passes */
  15.329 +  return compress_output(cinfo, input_buf);
  15.330 +}
  15.331 +
  15.332 +
  15.333 +/*
  15.334 + * Process some data in subsequent passes of a multi-pass case.
  15.335 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  15.336 + * per call, ie, v_samp_factor block rows for each component in the scan.
  15.337 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  15.338 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  15.339 + *
  15.340 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  15.341 + */
  15.342 +
  15.343 +METHODDEF(boolean)
  15.344 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  15.345 +{
  15.346 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  15.347 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  15.348 +  int blkn, ci, xindex, yindex, yoffset;
  15.349 +  JDIMENSION start_col;
  15.350 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  15.351 +  JBLOCKROW buffer_ptr;
  15.352 +  jpeg_component_info *compptr;
  15.353 +
  15.354 +  /* Align the virtual buffers for the components used in this scan.
  15.355 +   * NB: during first pass, this is safe only because the buffers will
  15.356 +   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  15.357 +   */
  15.358 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  15.359 +    compptr = cinfo->cur_comp_info[ci];
  15.360 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  15.361 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  15.362 +       coef->iMCU_row_num * compptr->v_samp_factor,
  15.363 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  15.364 +  }
  15.365 +
  15.366 +  /* Loop to process one whole iMCU row */
  15.367 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  15.368 +       yoffset++) {
  15.369 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  15.370 +	 MCU_col_num++) {
  15.371 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  15.372 +      blkn = 0;			/* index of current DCT block within MCU */
  15.373 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  15.374 +	compptr = cinfo->cur_comp_info[ci];
  15.375 +	start_col = MCU_col_num * compptr->MCU_width;
  15.376 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  15.377 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  15.378 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  15.379 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
  15.380 +	  }
  15.381 +	}
  15.382 +      }
  15.383 +      /* Try to write the MCU. */
  15.384 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  15.385 +	/* Suspension forced; update state counters and exit */
  15.386 +	coef->MCU_vert_offset = yoffset;
  15.387 +	coef->mcu_ctr = MCU_col_num;
  15.388 +	return FALSE;
  15.389 +      }
  15.390 +    }
  15.391 +    /* Completed an MCU row, but perhaps not an iMCU row */
  15.392 +    coef->mcu_ctr = 0;
  15.393 +  }
  15.394 +  /* Completed the iMCU row, advance counters for next one */
  15.395 +  coef->iMCU_row_num++;
  15.396 +  start_iMCU_row(cinfo);
  15.397 +  return TRUE;
  15.398 +}
  15.399 +
  15.400 +#endif /* FULL_COEF_BUFFER_SUPPORTED */
  15.401 +
  15.402 +
  15.403 +/*
  15.404 + * Initialize coefficient buffer controller.
  15.405 + */
  15.406 +
  15.407 +GLOBAL(void)
  15.408 +jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  15.409 +{
  15.410 +  my_coef_ptr coef;
  15.411 +
  15.412 +  coef = (my_coef_ptr)
  15.413 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  15.414 +				SIZEOF(my_coef_controller));
  15.415 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  15.416 +  coef->pub.start_pass = start_pass_coef;
  15.417 +
  15.418 +  /* Create the coefficient buffer. */
  15.419 +  if (need_full_buffer) {
  15.420 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  15.421 +    /* Allocate a full-image virtual array for each component, */
  15.422 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
  15.423 +    int ci;
  15.424 +    jpeg_component_info *compptr;
  15.425 +
  15.426 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  15.427 +	 ci++, compptr++) {
  15.428 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  15.429 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  15.430 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  15.431 +				(long) compptr->h_samp_factor),
  15.432 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  15.433 +				(long) compptr->v_samp_factor),
  15.434 +	 (JDIMENSION) compptr->v_samp_factor);
  15.435 +    }
  15.436 +#else
  15.437 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  15.438 +#endif
  15.439 +  } else {
  15.440 +    /* We only need a single-MCU buffer. */
  15.441 +    JBLOCKROW buffer;
  15.442 +    int i;
  15.443 +
  15.444 +    buffer = (JBLOCKROW)
  15.445 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  15.446 +				  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  15.447 +    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  15.448 +      coef->MCU_buffer[i] = buffer + i;
  15.449 +    }
  15.450 +    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  15.451 +  }
  15.452 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/libs/libjpeg/jccolor.c	Thu Sep 08 06:28:38 2011 +0300
    16.3 @@ -0,0 +1,459 @@
    16.4 +/*
    16.5 + * jccolor.c
    16.6 + *
    16.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    16.8 + * This file is part of the Independent JPEG Group's software.
    16.9 + * For conditions of distribution and use, see the accompanying README file.
   16.10 + *
   16.11 + * This file contains input colorspace conversion routines.
   16.12 + */
   16.13 +
   16.14 +#define JPEG_INTERNALS
   16.15 +#include "jinclude.h"
   16.16 +#include "jpeglib.h"
   16.17 +
   16.18 +
   16.19 +/* Private subobject */
   16.20 +
   16.21 +typedef struct {
   16.22 +  struct jpeg_color_converter pub; /* public fields */
   16.23 +
   16.24 +  /* Private state for RGB->YCC conversion */
   16.25 +  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
   16.26 +} my_color_converter;
   16.27 +
   16.28 +typedef my_color_converter * my_cconvert_ptr;
   16.29 +
   16.30 +
   16.31 +/**************** RGB -> YCbCr conversion: most common case **************/
   16.32 +
   16.33 +/*
   16.34 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
   16.35 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
   16.36 + * The conversion equations to be implemented are therefore
   16.37 + *	Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
   16.38 + *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
   16.39 + *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
   16.40 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
   16.41 + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
   16.42 + * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
   16.43 + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
   16.44 + * were not represented exactly.  Now we sacrifice exact representation of
   16.45 + * maximum red and maximum blue in order to get exact grayscales.
   16.46 + *
   16.47 + * To avoid floating-point arithmetic, we represent the fractional constants
   16.48 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
   16.49 + * the products by 2^16, with appropriate rounding, to get the correct answer.
   16.50 + *
   16.51 + * For even more speed, we avoid doing any multiplications in the inner loop
   16.52 + * by precalculating the constants times R,G,B for all possible values.
   16.53 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
   16.54 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
   16.55 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
   16.56 + * colorspace anyway.
   16.57 + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
   16.58 + * in the tables to save adding them separately in the inner loop.
   16.59 + */
   16.60 +
   16.61 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   16.62 +#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
   16.63 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   16.64 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   16.65 +
   16.66 +/* We allocate one big table and divide it up into eight parts, instead of
   16.67 + * doing eight alloc_small requests.  This lets us use a single table base
   16.68 + * address, which can be held in a register in the inner loops on many
   16.69 + * machines (more than can hold all eight addresses, anyway).
   16.70 + */
   16.71 +
   16.72 +#define R_Y_OFF		0			/* offset to R => Y section */
   16.73 +#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
   16.74 +#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
   16.75 +#define R_CB_OFF	(3*(MAXJSAMPLE+1))
   16.76 +#define G_CB_OFF	(4*(MAXJSAMPLE+1))
   16.77 +#define B_CB_OFF	(5*(MAXJSAMPLE+1))
   16.78 +#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */
   16.79 +#define G_CR_OFF	(6*(MAXJSAMPLE+1))
   16.80 +#define B_CR_OFF	(7*(MAXJSAMPLE+1))
   16.81 +#define TABLE_SIZE	(8*(MAXJSAMPLE+1))
   16.82 +
   16.83 +
   16.84 +/*
   16.85 + * Initialize for RGB->YCC colorspace conversion.
   16.86 + */
   16.87 +
   16.88 +METHODDEF(void)
   16.89 +rgb_ycc_start (j_compress_ptr cinfo)
   16.90 +{
   16.91 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   16.92 +  INT32 * rgb_ycc_tab;
   16.93 +  INT32 i;
   16.94 +
   16.95 +  /* Allocate and fill in the conversion tables. */
   16.96 +  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
   16.97 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   16.98 +				(TABLE_SIZE * SIZEOF(INT32)));
   16.99 +
  16.100 +  for (i = 0; i <= MAXJSAMPLE; i++) {
  16.101 +    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  16.102 +    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  16.103 +    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
  16.104 +    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  16.105 +    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  16.106 +    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  16.107 +     * This ensures that the maximum output will round to MAXJSAMPLE
  16.108 +     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  16.109 +     */
  16.110 +    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  16.111 +/*  B=>Cb and R=>Cr tables are the same
  16.112 +    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  16.113 +*/
  16.114 +    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  16.115 +    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  16.116 +  }
  16.117 +}
  16.118 +
  16.119 +
  16.120 +/*
  16.121 + * Convert some rows of samples to the JPEG colorspace.
  16.122 + *
  16.123 + * Note that we change from the application's interleaved-pixel format
  16.124 + * to our internal noninterleaved, one-plane-per-component format.
  16.125 + * The input buffer is therefore three times as wide as the output buffer.
  16.126 + *
  16.127 + * A starting row offset is provided only for the output buffer.  The caller
  16.128 + * can easily adjust the passed input_buf value to accommodate any row
  16.129 + * offset required on that side.
  16.130 + */
  16.131 +
  16.132 +METHODDEF(void)
  16.133 +rgb_ycc_convert (j_compress_ptr cinfo,
  16.134 +		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  16.135 +		 JDIMENSION output_row, int num_rows)
  16.136 +{
  16.137 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  16.138 +  register int r, g, b;
  16.139 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  16.140 +  register JSAMPROW inptr;
  16.141 +  register JSAMPROW outptr0, outptr1, outptr2;
  16.142 +  register JDIMENSION col;
  16.143 +  JDIMENSION num_cols = cinfo->image_width;
  16.144 +
  16.145 +  while (--num_rows >= 0) {
  16.146 +    inptr = *input_buf++;
  16.147 +    outptr0 = output_buf[0][output_row];
  16.148 +    outptr1 = output_buf[1][output_row];
  16.149 +    outptr2 = output_buf[2][output_row];
  16.150 +    output_row++;
  16.151 +    for (col = 0; col < num_cols; col++) {
  16.152 +      r = GETJSAMPLE(inptr[RGB_RED]);
  16.153 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  16.154 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  16.155 +      inptr += RGB_PIXELSIZE;
  16.156 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  16.157 +       * must be too; we do not need an explicit range-limiting operation.
  16.158 +       * Hence the value being shifted is never negative, and we don't
  16.159 +       * need the general RIGHT_SHIFT macro.
  16.160 +       */
  16.161 +      /* Y */
  16.162 +      outptr0[col] = (JSAMPLE)
  16.163 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  16.164 +		 >> SCALEBITS);
  16.165 +      /* Cb */
  16.166 +      outptr1[col] = (JSAMPLE)
  16.167 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  16.168 +		 >> SCALEBITS);
  16.169 +      /* Cr */
  16.170 +      outptr2[col] = (JSAMPLE)
  16.171 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  16.172 +		 >> SCALEBITS);
  16.173 +    }
  16.174 +  }
  16.175 +}
  16.176 +
  16.177 +
  16.178 +/**************** Cases other than RGB -> YCbCr **************/
  16.179 +
  16.180 +
  16.181 +/*
  16.182 + * Convert some rows of samples to the JPEG colorspace.
  16.183 + * This version handles RGB->grayscale conversion, which is the same
  16.184 + * as the RGB->Y portion of RGB->YCbCr.
  16.185 + * We assume rgb_ycc_start has been called (we only use the Y tables).
  16.186 + */
  16.187 +
  16.188 +METHODDEF(void)
  16.189 +rgb_gray_convert (j_compress_ptr cinfo,
  16.190 +		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  16.191 +		  JDIMENSION output_row, int num_rows)
  16.192 +{
  16.193 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  16.194 +  register int r, g, b;
  16.195 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  16.196 +  register JSAMPROW inptr;
  16.197 +  register JSAMPROW outptr;
  16.198 +  register JDIMENSION col;
  16.199 +  JDIMENSION num_cols = cinfo->image_width;
  16.200 +
  16.201 +  while (--num_rows >= 0) {
  16.202 +    inptr = *input_buf++;
  16.203 +    outptr = output_buf[0][output_row];
  16.204 +    output_row++;
  16.205 +    for (col = 0; col < num_cols; col++) {
  16.206 +      r = GETJSAMPLE(inptr[RGB_RED]);
  16.207 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  16.208 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  16.209 +      inptr += RGB_PIXELSIZE;
  16.210 +      /* Y */
  16.211 +      outptr[col] = (JSAMPLE)
  16.212 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  16.213 +		 >> SCALEBITS);
  16.214 +    }
  16.215 +  }
  16.216 +}
  16.217 +
  16.218 +
  16.219 +/*
  16.220 + * Convert some rows of samples to the JPEG colorspace.
  16.221 + * This version handles Adobe-style CMYK->YCCK conversion,
  16.222 + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  16.223 + * conversion as above, while passing K (black) unchanged.
  16.224 + * We assume rgb_ycc_start has been called.
  16.225 + */
  16.226 +
  16.227 +METHODDEF(void)
  16.228 +cmyk_ycck_convert (j_compress_ptr cinfo,
  16.229 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  16.230 +		   JDIMENSION output_row, int num_rows)
  16.231 +{
  16.232 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  16.233 +  register int r, g, b;
  16.234 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  16.235 +  register JSAMPROW inptr;
  16.236 +  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  16.237 +  register JDIMENSION col;
  16.238 +  JDIMENSION num_cols = cinfo->image_width;
  16.239 +
  16.240 +  while (--num_rows >= 0) {
  16.241 +    inptr = *input_buf++;
  16.242 +    outptr0 = output_buf[0][output_row];
  16.243 +    outptr1 = output_buf[1][output_row];
  16.244 +    outptr2 = output_buf[2][output_row];
  16.245 +    outptr3 = output_buf[3][output_row];
  16.246 +    output_row++;
  16.247 +    for (col = 0; col < num_cols; col++) {
  16.248 +      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  16.249 +      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  16.250 +      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  16.251 +      /* K passes through as-is */
  16.252 +      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
  16.253 +      inptr += 4;
  16.254 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  16.255 +       * must be too; we do not need an explicit range-limiting operation.
  16.256 +       * Hence the value being shifted is never negative, and we don't
  16.257 +       * need the general RIGHT_SHIFT macro.
  16.258 +       */
  16.259 +      /* Y */
  16.260 +      outptr0[col] = (JSAMPLE)
  16.261 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  16.262 +		 >> SCALEBITS);
  16.263 +      /* Cb */
  16.264 +      outptr1[col] = (JSAMPLE)
  16.265 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  16.266 +		 >> SCALEBITS);
  16.267 +      /* Cr */
  16.268 +      outptr2[col] = (JSAMPLE)
  16.269 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  16.270 +		 >> SCALEBITS);
  16.271 +    }
  16.272 +  }
  16.273 +}
  16.274 +
  16.275 +
  16.276 +/*
  16.277 + * Convert some rows of samples to the JPEG colorspace.
  16.278 + * This version handles grayscale output with no conversion.
  16.279 + * The source can be either plain grayscale or YCbCr (since Y == gray).
  16.280 + */
  16.281 +
  16.282 +METHODDEF(void)
  16.283 +grayscale_convert (j_compress_ptr cinfo,
  16.284 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  16.285 +		   JDIMENSION output_row, int num_rows)
  16.286 +{
  16.287 +  register JSAMPROW inptr;
  16.288 +  register JSAMPROW outptr;
  16.289 +  register JDIMENSION col;
  16.290 +  JDIMENSION num_cols = cinfo->image_width;
  16.291 +  int instride = cinfo->input_components;
  16.292 +
  16.293 +  while (--num_rows >= 0) {
  16.294 +    inptr = *input_buf++;
  16.295 +    outptr = output_buf[0][output_row];
  16.296 +    output_row++;
  16.297 +    for (col = 0; col < num_cols; col++) {
  16.298 +      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
  16.299 +      inptr += instride;
  16.300 +    }
  16.301 +  }
  16.302 +}
  16.303 +
  16.304 +
  16.305 +/*
  16.306 + * Convert some rows of samples to the JPEG colorspace.
  16.307 + * This version handles multi-component colorspaces without conversion.
  16.308 + * We assume input_components == num_components.
  16.309 + */
  16.310 +
  16.311 +METHODDEF(void)
  16.312 +null_convert (j_compress_ptr cinfo,
  16.313 +	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  16.314 +	      JDIMENSION output_row, int num_rows)
  16.315 +{
  16.316 +  register JSAMPROW inptr;
  16.317 +  register JSAMPROW outptr;
  16.318 +  register JDIMENSION col;
  16.319 +  register int ci;
  16.320 +  int nc = cinfo->num_components;
  16.321 +  JDIMENSION num_cols = cinfo->image_width;
  16.322 +
  16.323 +  while (--num_rows >= 0) {
  16.324 +    /* It seems fastest to make a separate pass for each component. */
  16.325 +    for (ci = 0; ci < nc; ci++) {
  16.326 +      inptr = *input_buf;
  16.327 +      outptr = output_buf[ci][output_row];
  16.328 +      for (col = 0; col < num_cols; col++) {
  16.329 +	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  16.330 +	inptr += nc;
  16.331 +      }
  16.332 +    }
  16.333 +    input_buf++;
  16.334 +    output_row++;
  16.335 +  }
  16.336 +}
  16.337 +
  16.338 +
  16.339 +/*
  16.340 + * Empty method for start_pass.
  16.341 + */
  16.342 +
  16.343 +METHODDEF(void)
  16.344 +null_method (j_compress_ptr cinfo)
  16.345 +{
  16.346 +  /* no work needed */
  16.347 +}
  16.348 +
  16.349 +
  16.350 +/*
  16.351 + * Module initialization routine for input colorspace conversion.
  16.352 + */
  16.353 +
  16.354 +GLOBAL(void)
  16.355 +jinit_color_converter (j_compress_ptr cinfo)
  16.356 +{
  16.357 +  my_cconvert_ptr cconvert;
  16.358 +
  16.359 +  cconvert = (my_cconvert_ptr)
  16.360 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  16.361 +				SIZEOF(my_color_converter));
  16.362 +  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  16.363 +  /* set start_pass to null method until we find out differently */
  16.364 +  cconvert->pub.start_pass = null_method;
  16.365 +
  16.366 +  /* Make sure input_components agrees with in_color_space */
  16.367 +  switch (cinfo->in_color_space) {
  16.368 +  case JCS_GRAYSCALE:
  16.369 +    if (cinfo->input_components != 1)
  16.370 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  16.371 +    break;
  16.372 +
  16.373 +  case JCS_RGB:
  16.374 +#if RGB_PIXELSIZE != 3
  16.375 +    if (cinfo->input_components != RGB_PIXELSIZE)
  16.376 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  16.377 +    break;
  16.378 +#endif /* else share code with YCbCr */
  16.379 +
  16.380 +  case JCS_YCbCr:
  16.381 +    if (cinfo->input_components != 3)
  16.382 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  16.383 +    break;
  16.384 +
  16.385 +  case JCS_CMYK:
  16.386 +  case JCS_YCCK:
  16.387 +    if (cinfo->input_components != 4)
  16.388 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  16.389 +    break;
  16.390 +
  16.391 +  default:			/* JCS_UNKNOWN can be anything */
  16.392 +    if (cinfo->input_components < 1)
  16.393 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  16.394 +    break;
  16.395 +  }
  16.396 +
  16.397 +  /* Check num_components, set conversion method based on requested space */
  16.398 +  switch (cinfo->jpeg_color_space) {
  16.399 +  case JCS_GRAYSCALE:
  16.400 +    if (cinfo->num_components != 1)
  16.401 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  16.402 +    if (cinfo->in_color_space == JCS_GRAYSCALE)
  16.403 +      cconvert->pub.color_convert = grayscale_convert;
  16.404 +    else if (cinfo->in_color_space == JCS_RGB) {
  16.405 +      cconvert->pub.start_pass = rgb_ycc_start;
  16.406 +      cconvert->pub.color_convert = rgb_gray_convert;
  16.407 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  16.408 +      cconvert->pub.color_convert = grayscale_convert;
  16.409 +    else
  16.410 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.411 +    break;
  16.412 +
  16.413 +  case JCS_RGB:
  16.414 +    if (cinfo->num_components != 3)
  16.415 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  16.416 +    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  16.417 +      cconvert->pub.color_convert = null_convert;
  16.418 +    else
  16.419 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.420 +    break;
  16.421 +
  16.422 +  case JCS_YCbCr:
  16.423 +    if (cinfo->num_components != 3)
  16.424 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  16.425 +    if (cinfo->in_color_space == JCS_RGB) {
  16.426 +      cconvert->pub.start_pass = rgb_ycc_start;
  16.427 +      cconvert->pub.color_convert = rgb_ycc_convert;
  16.428 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  16.429 +      cconvert->pub.color_convert = null_convert;
  16.430 +    else
  16.431 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.432 +    break;
  16.433 +
  16.434 +  case JCS_CMYK:
  16.435 +    if (cinfo->num_components != 4)
  16.436 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  16.437 +    if (cinfo->in_color_space == JCS_CMYK)
  16.438 +      cconvert->pub.color_convert = null_convert;
  16.439 +    else
  16.440 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.441 +    break;
  16.442 +
  16.443 +  case JCS_YCCK:
  16.444 +    if (cinfo->num_components != 4)
  16.445 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  16.446 +    if (cinfo->in_color_space == JCS_CMYK) {
  16.447 +      cconvert->pub.start_pass = rgb_ycc_start;
  16.448 +      cconvert->pub.color_convert = cmyk_ycck_convert;
  16.449 +    } else if (cinfo->in_color_space == JCS_YCCK)
  16.450 +      cconvert->pub.color_convert = null_convert;
  16.451 +    else
  16.452 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.453 +    break;
  16.454 +
  16.455 +  default:			/* allow null conversion of JCS_UNKNOWN */
  16.456 +    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  16.457 +	cinfo->num_components != cinfo->input_components)
  16.458 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  16.459 +    cconvert->pub.color_convert = null_convert;
  16.460 +    break;
  16.461 +  }
  16.462 +}
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/libs/libjpeg/jcdctmgr.c	Thu Sep 08 06:28:38 2011 +0300
    17.3 @@ -0,0 +1,387 @@
    17.4 +/*
    17.5 + * jcdctmgr.c
    17.6 + *
    17.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    17.8 + * This file is part of the Independent JPEG Group's software.
    17.9 + * For conditions of distribution and use, see the accompanying README file.
   17.10 + *
   17.11 + * This file contains the forward-DCT management logic.
   17.12 + * This code selects a particular DCT implementation to be used,
   17.13 + * and it performs related housekeeping chores including coefficient
   17.14 + * quantization.
   17.15 + */
   17.16 +
   17.17 +#define JPEG_INTERNALS
   17.18 +#include "jinclude.h"
   17.19 +#include "jpeglib.h"
   17.20 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   17.21 +
   17.22 +
   17.23 +/* Private subobject for this module */
   17.24 +
   17.25 +typedef struct {
   17.26 +  struct jpeg_forward_dct pub;	/* public fields */
   17.27 +
   17.28 +  /* Pointer to the DCT routine actually in use */
   17.29 +  forward_DCT_method_ptr do_dct;
   17.30 +
   17.31 +  /* The actual post-DCT divisors --- not identical to the quant table
   17.32 +   * entries, because of scaling (especially for an unnormalized DCT).
   17.33 +   * Each table is given in normal array order.
   17.34 +   */
   17.35 +  DCTELEM * divisors[NUM_QUANT_TBLS];
   17.36 +
   17.37 +#ifdef DCT_FLOAT_SUPPORTED
   17.38 +  /* Same as above for the floating-point case. */
   17.39 +  float_DCT_method_ptr do_float_dct;
   17.40 +  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
   17.41 +#endif
   17.42 +} my_fdct_controller;
   17.43 +
   17.44 +typedef my_fdct_controller * my_fdct_ptr;
   17.45 +
   17.46 +
   17.47 +/*
   17.48 + * Initialize for a processing pass.
   17.49 + * Verify that all referenced Q-tables are present, and set up
   17.50 + * the divisor table for each one.
   17.51 + * In the current implementation, DCT of all components is done during
   17.52 + * the first pass, even if only some components will be output in the
   17.53 + * first scan.  Hence all components should be examined here.
   17.54 + */
   17.55 +
   17.56 +METHODDEF(void)
   17.57 +start_pass_fdctmgr (j_compress_ptr cinfo)
   17.58 +{
   17.59 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
   17.60 +  int ci, qtblno, i;
   17.61 +  jpeg_component_info *compptr;
   17.62 +  JQUANT_TBL * qtbl;
   17.63 +  DCTELEM * dtbl;
   17.64 +
   17.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   17.66 +       ci++, compptr++) {
   17.67 +    qtblno = compptr->quant_tbl_no;
   17.68 +    /* Make sure specified quantization table is present */
   17.69 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
   17.70 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
   17.71 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
   17.72 +    qtbl = cinfo->quant_tbl_ptrs[qtblno];
   17.73 +    /* Compute divisors for this quant table */
   17.74 +    /* We may do this more than once for same table, but it's not a big deal */
   17.75 +    switch (cinfo->dct_method) {
   17.76 +#ifdef DCT_ISLOW_SUPPORTED
   17.77 +    case JDCT_ISLOW:
   17.78 +      /* For LL&M IDCT method, divisors are equal to raw quantization
   17.79 +       * coefficients multiplied by 8 (to counteract scaling).
   17.80 +       */
   17.81 +      if (fdct->divisors[qtblno] == NULL) {
   17.82 +	fdct->divisors[qtblno] = (DCTELEM *)
   17.83 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   17.84 +				      DCTSIZE2 * SIZEOF(DCTELEM));
   17.85 +      }
   17.86 +      dtbl = fdct->divisors[qtblno];
   17.87 +      for (i = 0; i < DCTSIZE2; i++) {
   17.88 +	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
   17.89 +      }
   17.90 +      break;
   17.91 +#endif
   17.92 +#ifdef DCT_IFAST_SUPPORTED
   17.93 +    case JDCT_IFAST:
   17.94 +      {
   17.95 +	/* For AA&N IDCT method, divisors are equal to quantization
   17.96 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   17.97 +	 *   scalefactor[0] = 1
   17.98 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   17.99 +	 * We apply a further scale factor of 8.
  17.100 +	 */
  17.101 +#define CONST_BITS 14
  17.102 +	static const INT16 aanscales[DCTSIZE2] = {
  17.103 +	  /* precomputed values scaled up by 14 bits */
  17.104 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  17.105 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  17.106 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  17.107 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  17.108 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  17.109 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  17.110 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  17.111 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  17.112 +	};
  17.113 +	SHIFT_TEMPS
  17.114 +
  17.115 +	if (fdct->divisors[qtblno] == NULL) {
  17.116 +	  fdct->divisors[qtblno] = (DCTELEM *)
  17.117 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  17.118 +					DCTSIZE2 * SIZEOF(DCTELEM));
  17.119 +	}
  17.120 +	dtbl = fdct->divisors[qtblno];
  17.121 +	for (i = 0; i < DCTSIZE2; i++) {
  17.122 +	  dtbl[i] = (DCTELEM)
  17.123 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  17.124 +				  (INT32) aanscales[i]),
  17.125 +		    CONST_BITS-3);
  17.126 +	}
  17.127 +      }
  17.128 +      break;
  17.129 +#endif
  17.130 +#ifdef DCT_FLOAT_SUPPORTED
  17.131 +    case JDCT_FLOAT:
  17.132 +      {
  17.133 +	/* For float AA&N IDCT method, divisors are equal to quantization
  17.134 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  17.135 +	 *   scalefactor[0] = 1
  17.136 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  17.137 +	 * We apply a further scale factor of 8.
  17.138 +	 * What's actually stored is 1/divisor so that the inner loop can
  17.139 +	 * use a multiplication rather than a division.
  17.140 +	 */
  17.141 +	FAST_FLOAT * fdtbl;
  17.142 +	int row, col;
  17.143 +	static const double aanscalefactor[DCTSIZE] = {
  17.144 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
  17.145 +	  1.0, 0.785694958, 0.541196100, 0.275899379
  17.146 +	};
  17.147 +
  17.148 +	if (fdct->float_divisors[qtblno] == NULL) {
  17.149 +	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  17.150 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  17.151 +					DCTSIZE2 * SIZEOF(FAST_FLOAT));
  17.152 +	}
  17.153 +	fdtbl = fdct->float_divisors[qtblno];
  17.154 +	i = 0;
  17.155 +	for (row = 0; row < DCTSIZE; row++) {
  17.156 +	  for (col = 0; col < DCTSIZE; col++) {
  17.157 +	    fdtbl[i] = (FAST_FLOAT)
  17.158 +	      (1.0 / (((double) qtbl->quantval[i] *
  17.159 +		       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  17.160 +	    i++;
  17.161 +	  }
  17.162 +	}
  17.163 +      }
  17.164 +      break;
  17.165 +#endif
  17.166 +    default:
  17.167 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  17.168 +      break;
  17.169 +    }
  17.170 +  }
  17.171 +}
  17.172 +
  17.173 +
  17.174 +/*
  17.175 + * Perform forward DCT on one or more blocks of a component.
  17.176 + *
  17.177 + * The input samples are taken from the sample_data[] array starting at
  17.178 + * position start_row/start_col, and moving to the right for any additional
  17.179 + * blocks. The quantized coefficients are returned in coef_blocks[].
  17.180 + */
  17.181 +
  17.182 +METHODDEF(void)
  17.183 +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  17.184 +	     JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  17.185 +	     JDIMENSION start_row, JDIMENSION start_col,
  17.186 +	     JDIMENSION num_blocks)
  17.187 +/* This version is used for integer DCT implementations. */
  17.188 +{
  17.189 +  /* This routine is heavily used, so it's worth coding it tightly. */
  17.190 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  17.191 +  forward_DCT_method_ptr do_dct = fdct->do_dct;
  17.192 +  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  17.193 +  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
  17.194 +  JDIMENSION bi;
  17.195 +
  17.196 +  sample_data += start_row;	/* fold in the vertical offset once */
  17.197 +
  17.198 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  17.199 +    /* Load data into workspace, applying unsigned->signed conversion */
  17.200 +    { register DCTELEM *workspaceptr;
  17.201 +      register JSAMPROW elemptr;
  17.202 +      register int elemr;
  17.203 +
  17.204 +      workspaceptr = workspace;
  17.205 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  17.206 +	elemptr = sample_data[elemr] + start_col;
  17.207 +#if DCTSIZE == 8		/* unroll the inner loop */
  17.208 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.209 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.210 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.211 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.212 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.213 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.214 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.215 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.216 +#else
  17.217 +	{ register int elemc;
  17.218 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  17.219 +	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  17.220 +	  }
  17.221 +	}
  17.222 +#endif
  17.223 +      }
  17.224 +    }
  17.225 +
  17.226 +    /* Perform the DCT */
  17.227 +    (*do_dct) (workspace);
  17.228 +
  17.229 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  17.230 +    { register DCTELEM temp, qval;
  17.231 +      register int i;
  17.232 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  17.233 +
  17.234 +      for (i = 0; i < DCTSIZE2; i++) {
  17.235 +	qval = divisors[i];
  17.236 +	temp = workspace[i];
  17.237 +	/* Divide the coefficient value by qval, ensuring proper rounding.
  17.238 +	 * Since C does not specify the direction of rounding for negative
  17.239 +	 * quotients, we have to force the dividend positive for portability.
  17.240 +	 *
  17.241 +	 * In most files, at least half of the output values will be zero
  17.242 +	 * (at default quantization settings, more like three-quarters...)
  17.243 +	 * so we should ensure that this case is fast.  On many machines,
  17.244 +	 * a comparison is enough cheaper than a divide to make a special test
  17.245 +	 * a win.  Since both inputs will be nonnegative, we need only test
  17.246 +	 * for a < b to discover whether a/b is 0.
  17.247 +	 * If your machine's division is fast enough, define FAST_DIVIDE.
  17.248 +	 */
  17.249 +#ifdef FAST_DIVIDE
  17.250 +#define DIVIDE_BY(a,b)	a /= b
  17.251 +#else
  17.252 +#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
  17.253 +#endif
  17.254 +	if (temp < 0) {
  17.255 +	  temp = -temp;
  17.256 +	  temp += qval>>1;	/* for rounding */
  17.257 +	  DIVIDE_BY(temp, qval);
  17.258 +	  temp = -temp;
  17.259 +	} else {
  17.260 +	  temp += qval>>1;	/* for rounding */
  17.261 +	  DIVIDE_BY(temp, qval);
  17.262 +	}
  17.263 +	output_ptr[i] = (JCOEF) temp;
  17.264 +      }
  17.265 +    }
  17.266 +  }
  17.267 +}
  17.268 +
  17.269 +
  17.270 +#ifdef DCT_FLOAT_SUPPORTED
  17.271 +
  17.272 +METHODDEF(void)
  17.273 +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  17.274 +		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  17.275 +		   JDIMENSION start_row, JDIMENSION start_col,
  17.276 +		   JDIMENSION num_blocks)
  17.277 +/* This version is used for floating-point DCT implementations. */
  17.278 +{
  17.279 +  /* This routine is heavily used, so it's worth coding it tightly. */
  17.280 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  17.281 +  float_DCT_method_ptr do_dct = fdct->do_float_dct;
  17.282 +  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  17.283 +  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  17.284 +  JDIMENSION bi;
  17.285 +
  17.286 +  sample_data += start_row;	/* fold in the vertical offset once */
  17.287 +
  17.288 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  17.289 +    /* Load data into workspace, applying unsigned->signed conversion */
  17.290 +    { register FAST_FLOAT *workspaceptr;
  17.291 +      register JSAMPROW elemptr;
  17.292 +      register int elemr;
  17.293 +
  17.294 +      workspaceptr = workspace;
  17.295 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  17.296 +	elemptr = sample_data[elemr] + start_col;
  17.297 +#if DCTSIZE == 8		/* unroll the inner loop */
  17.298 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.299 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.300 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.301 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.302 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.303 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.304 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.305 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.306 +#else
  17.307 +	{ register int elemc;
  17.308 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  17.309 +	    *workspaceptr++ = (FAST_FLOAT)
  17.310 +	      (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  17.311 +	  }
  17.312 +	}
  17.313 +#endif
  17.314 +      }
  17.315 +    }
  17.316 +
  17.317 +    /* Perform the DCT */
  17.318 +    (*do_dct) (workspace);
  17.319 +
  17.320 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  17.321 +    { register FAST_FLOAT temp;
  17.322 +      register int i;
  17.323 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  17.324 +
  17.325 +      for (i = 0; i < DCTSIZE2; i++) {
  17.326 +	/* Apply the quantization and scaling factor */
  17.327 +	temp = workspace[i] * divisors[i];
  17.328 +	/* Round to nearest integer.
  17.329 +	 * Since C does not specify the direction of rounding for negative
  17.330 +	 * quotients, we have to force the dividend positive for portability.
  17.331 +	 * The maximum coefficient size is +-16K (for 12-bit data), so this
  17.332 +	 * code should work for either 16-bit or 32-bit ints.
  17.333 +	 */
  17.334 +	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  17.335 +      }
  17.336 +    }
  17.337 +  }
  17.338 +}
  17.339 +
  17.340 +#endif /* DCT_FLOAT_SUPPORTED */
  17.341 +
  17.342 +
  17.343 +/*
  17.344 + * Initialize FDCT manager.
  17.345 + */
  17.346 +
  17.347 +GLOBAL(void)
  17.348 +jinit_forward_dct (j_compress_ptr cinfo)
  17.349 +{
  17.350 +  my_fdct_ptr fdct;
  17.351 +  int i;
  17.352 +
  17.353 +  fdct = (my_fdct_ptr)
  17.354 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  17.355 +				SIZEOF(my_fdct_controller));
  17.356 +  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  17.357 +  fdct->pub.start_pass = start_pass_fdctmgr;
  17.358 +
  17.359 +  switch (cinfo->dct_method) {
  17.360 +#ifdef DCT_ISLOW_SUPPORTED
  17.361 +  case JDCT_ISLOW:
  17.362 +    fdct->pub.forward_DCT = forward_DCT;
  17.363 +    fdct->do_dct = jpeg_fdct_islow;
  17.364 +    break;
  17.365 +#endif
  17.366 +#ifdef DCT_IFAST_SUPPORTED
  17.367 +  case JDCT_IFAST:
  17.368 +    fdct->pub.forward_DCT = forward_DCT;
  17.369 +    fdct->do_dct = jpeg_fdct_ifast;
  17.370 +    break;
  17.371 +#endif
  17.372 +#ifdef DCT_FLOAT_SUPPORTED
  17.373 +  case JDCT_FLOAT:
  17.374 +    fdct->pub.forward_DCT = forward_DCT_float;
  17.375 +    fdct->do_float_dct = jpeg_fdct_float;
  17.376 +    break;
  17.377 +#endif
  17.378 +  default:
  17.379 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  17.380 +    break;
  17.381 +  }
  17.382 +
  17.383 +  /* Mark divisor tables unallocated */
  17.384 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  17.385 +    fdct->divisors[i] = NULL;
  17.386 +#ifdef DCT_FLOAT_SUPPORTED
  17.387 +    fdct->float_divisors[i] = NULL;
  17.388 +#endif
  17.389 +  }
  17.390 +}
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/libs/libjpeg/jchuff.c	Thu Sep 08 06:28:38 2011 +0300
    18.3 @@ -0,0 +1,909 @@
    18.4 +/*
    18.5 + * jchuff.c
    18.6 + *
    18.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    18.8 + * This file is part of the Independent JPEG Group's software.
    18.9 + * For conditions of distribution and use, see the accompanying README file.
   18.10 + *
   18.11 + * This file contains Huffman entropy encoding routines.
   18.12 + *
   18.13 + * Much of the complexity here has to do with supporting output suspension.
   18.14 + * If the data destination module demands suspension, we want to be able to
   18.15 + * back up to the start of the current MCU.  To do this, we copy state
   18.16 + * variables into local working storage, and update them back to the
   18.17 + * permanent JPEG objects only upon successful completion of an MCU.
   18.18 + */
   18.19 +
   18.20 +#define JPEG_INTERNALS
   18.21 +#include "jinclude.h"
   18.22 +#include "jpeglib.h"
   18.23 +#include "jchuff.h"		/* Declarations shared with jcphuff.c */
   18.24 +
   18.25 +
   18.26 +/* Expanded entropy encoder object for Huffman encoding.
   18.27 + *
   18.28 + * The savable_state subrecord contains fields that change within an MCU,
   18.29 + * but must not be updated permanently until we complete the MCU.
   18.30 + */
   18.31 +
   18.32 +typedef struct {
   18.33 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   18.34 +  int put_bits;			/* # of bits now in it */
   18.35 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   18.36 +} savable_state;
   18.37 +
   18.38 +/* This macro is to work around compilers with missing or broken
   18.39 + * structure assignment.  You'll need to fix this code if you have
   18.40 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   18.41 + */
   18.42 +
   18.43 +#ifndef NO_STRUCT_ASSIGN
   18.44 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   18.45 +#else
   18.46 +#if MAX_COMPS_IN_SCAN == 4
   18.47 +#define ASSIGN_STATE(dest,src)  \
   18.48 +	((dest).put_buffer = (src).put_buffer, \
   18.49 +	 (dest).put_bits = (src).put_bits, \
   18.50 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
   18.51 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   18.52 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   18.53 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   18.54 +#endif
   18.55 +#endif
   18.56 +
   18.57 +
   18.58 +typedef struct {
   18.59 +  struct jpeg_entropy_encoder pub; /* public fields */
   18.60 +
   18.61 +  savable_state saved;		/* Bit buffer & DC state at start of MCU */
   18.62 +
   18.63 +  /* These fields are NOT loaded into local working state. */
   18.64 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   18.65 +  int next_restart_num;		/* next restart number to write (0-7) */
   18.66 +
   18.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   18.68 +  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   18.69 +  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   18.70 +
   18.71 +#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
   18.72 +  long * dc_count_ptrs[NUM_HUFF_TBLS];
   18.73 +  long * ac_count_ptrs[NUM_HUFF_TBLS];
   18.74 +#endif
   18.75 +} huff_entropy_encoder;
   18.76 +
   18.77 +typedef huff_entropy_encoder * huff_entropy_ptr;
   18.78 +
   18.79 +/* Working state while writing an MCU.
   18.80 + * This struct contains all the fields that are needed by subroutines.
   18.81 + */
   18.82 +
   18.83 +typedef struct {
   18.84 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   18.85 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   18.86 +  savable_state cur;		/* Current bit buffer & DC state */
   18.87 +  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
   18.88 +} working_state;
   18.89 +
   18.90 +
   18.91 +/* Forward declarations */
   18.92 +METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
   18.93 +					JBLOCKROW *MCU_data));
   18.94 +METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
   18.95 +#ifdef ENTROPY_OPT_SUPPORTED
   18.96 +METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
   18.97 +					  JBLOCKROW *MCU_data));
   18.98 +METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
   18.99 +#endif
  18.100 +
  18.101 +
  18.102 +/*
  18.103 + * Initialize for a Huffman-compressed scan.
  18.104 + * If gather_statistics is TRUE, we do not output anything during the scan,
  18.105 + * just count the Huffman symbols used and generate Huffman code tables.
  18.106 + */
  18.107 +
  18.108 +METHODDEF(void)
  18.109 +start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
  18.110 +{
  18.111 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  18.112 +  int ci, dctbl, actbl;
  18.113 +  jpeg_component_info * compptr;
  18.114 +
  18.115 +  if (gather_statistics) {
  18.116 +#ifdef ENTROPY_OPT_SUPPORTED
  18.117 +    entropy->pub.encode_mcu = encode_mcu_gather;
  18.118 +    entropy->pub.finish_pass = finish_pass_gather;
  18.119 +#else
  18.120 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  18.121 +#endif
  18.122 +  } else {
  18.123 +    entropy->pub.encode_mcu = encode_mcu_huff;
  18.124 +    entropy->pub.finish_pass = finish_pass_huff;
  18.125 +  }
  18.126 +
  18.127 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  18.128 +    compptr = cinfo->cur_comp_info[ci];
  18.129 +    dctbl = compptr->dc_tbl_no;
  18.130 +    actbl = compptr->ac_tbl_no;
  18.131 +    if (gather_statistics) {
  18.132 +#ifdef ENTROPY_OPT_SUPPORTED
  18.133 +      /* Check for invalid table indexes */
  18.134 +      /* (make_c_derived_tbl does this in the other path) */
  18.135 +      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
  18.136 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  18.137 +      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
  18.138 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  18.139 +      /* Allocate and zero the statistics tables */
  18.140 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  18.141 +      if (entropy->dc_count_ptrs[dctbl] == NULL)
  18.142 +	entropy->dc_count_ptrs[dctbl] = (long *)
  18.143 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.144 +				      257 * SIZEOF(long));
  18.145 +      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
  18.146 +      if (entropy->ac_count_ptrs[actbl] == NULL)
  18.147 +	entropy->ac_count_ptrs[actbl] = (long *)
  18.148 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.149 +				      257 * SIZEOF(long));
  18.150 +      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
  18.151 +#endif
  18.152 +    } else {
  18.153 +      /* Compute derived values for Huffman tables */
  18.154 +      /* We may do this more than once for a table, but it's not expensive */
  18.155 +      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
  18.156 +			      & entropy->dc_derived_tbls[dctbl]);
  18.157 +      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
  18.158 +			      & entropy->ac_derived_tbls[actbl]);
  18.159 +    }
  18.160 +    /* Initialize DC predictions to 0 */
  18.161 +    entropy->saved.last_dc_val[ci] = 0;
  18.162 +  }
  18.163 +
  18.164 +  /* Initialize bit buffer to empty */
  18.165 +  entropy->saved.put_buffer = 0;
  18.166 +  entropy->saved.put_bits = 0;
  18.167 +
  18.168 +  /* Initialize restart stuff */
  18.169 +  entropy->restarts_to_go = cinfo->restart_interval;
  18.170 +  entropy->next_restart_num = 0;
  18.171 +}
  18.172 +
  18.173 +
  18.174 +/*
  18.175 + * Compute the derived values for a Huffman table.
  18.176 + * This routine also performs some validation checks on the table.
  18.177 + *
  18.178 + * Note this is also used by jcphuff.c.
  18.179 + */
  18.180 +
  18.181 +GLOBAL(void)
  18.182 +jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
  18.183 +			 c_derived_tbl ** pdtbl)
  18.184 +{
  18.185 +  JHUFF_TBL *htbl;
  18.186 +  c_derived_tbl *dtbl;
  18.187 +  int p, i, l, lastp, si, maxsymbol;
  18.188 +  char huffsize[257];
  18.189 +  unsigned int huffcode[257];
  18.190 +  unsigned int code;
  18.191 +
  18.192 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
  18.193 +   * paralleling the order of the symbols themselves in htbl->huffval[].
  18.194 +   */
  18.195 +
  18.196 +  /* Find the input Huffman table */
  18.197 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  18.198 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  18.199 +  htbl =
  18.200 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  18.201 +  if (htbl == NULL)
  18.202 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  18.203 +
  18.204 +  /* Allocate a workspace if we haven't already done so. */
  18.205 +  if (*pdtbl == NULL)
  18.206 +    *pdtbl = (c_derived_tbl *)
  18.207 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.208 +				  SIZEOF(c_derived_tbl));
  18.209 +  dtbl = *pdtbl;
  18.210 +  
  18.211 +  /* Figure C.1: make table of Huffman code length for each symbol */
  18.212 +
  18.213 +  p = 0;
  18.214 +  for (l = 1; l <= 16; l++) {
  18.215 +    i = (int) htbl->bits[l];
  18.216 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
  18.217 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  18.218 +    while (i--)
  18.219 +      huffsize[p++] = (char) l;
  18.220 +  }
  18.221 +  huffsize[p] = 0;
  18.222 +  lastp = p;
  18.223 +  
  18.224 +  /* Figure C.2: generate the codes themselves */
  18.225 +  /* We also validate that the counts represent a legal Huffman code tree. */
  18.226 +
  18.227 +  code = 0;
  18.228 +  si = huffsize[0];
  18.229 +  p = 0;
  18.230 +  while (huffsize[p]) {
  18.231 +    while (((int) huffsize[p]) == si) {
  18.232 +      huffcode[p++] = code;
  18.233 +      code++;
  18.234 +    }
  18.235 +    /* code is now 1 more than the last code used for codelength si; but
  18.236 +     * it must still fit in si bits, since no code is allowed to be all ones.
  18.237 +     */
  18.238 +    if (((INT32) code) >= (((INT32) 1) << si))
  18.239 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  18.240 +    code <<= 1;
  18.241 +    si++;
  18.242 +  }
  18.243 +  
  18.244 +  /* Figure C.3: generate encoding tables */
  18.245 +  /* These are code and size indexed by symbol value */
  18.246 +
  18.247 +  /* Set all codeless symbols to have code length 0;
  18.248 +   * this lets us detect duplicate VAL entries here, and later
  18.249 +   * allows emit_bits to detect any attempt to emit such symbols.
  18.250 +   */
  18.251 +  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
  18.252 +
  18.253 +  /* This is also a convenient place to check for out-of-range
  18.254 +   * and duplicated VAL entries.  We allow 0..255 for AC symbols
  18.255 +   * but only 0..15 for DC.  (We could constrain them further
  18.256 +   * based on data depth and mode, but this seems enough.)
  18.257 +   */
  18.258 +  maxsymbol = isDC ? 15 : 255;
  18.259 +
  18.260 +  for (p = 0; p < lastp; p++) {
  18.261 +    i = htbl->huffval[p];
  18.262 +    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
  18.263 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  18.264 +    dtbl->ehufco[i] = huffcode[p];
  18.265 +    dtbl->ehufsi[i] = huffsize[p];
  18.266 +  }
  18.267 +}
  18.268 +
  18.269 +
  18.270 +/* Outputting bytes to the file */
  18.271 +
  18.272 +/* Emit a byte, taking 'action' if must suspend. */
  18.273 +#define emit_byte(state,val,action)  \
  18.274 +	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
  18.275 +	  if (--(state)->free_in_buffer == 0)  \
  18.276 +	    if (! dump_buffer(state))  \
  18.277 +	      { action; } }
  18.278 +
  18.279 +
  18.280 +LOCAL(boolean)
  18.281 +dump_buffer (working_state * state)
  18.282 +/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  18.283 +{
  18.284 +  struct jpeg_destination_mgr * dest = state->cinfo->dest;
  18.285 +
  18.286 +  if (! (*dest->empty_output_buffer) (state->cinfo))
  18.287 +    return FALSE;
  18.288 +  /* After a successful buffer dump, must reset buffer pointers */
  18.289 +  state->next_output_byte = dest->next_output_byte;
  18.290 +  state->free_in_buffer = dest->free_in_buffer;
  18.291 +  return TRUE;
  18.292 +}
  18.293 +
  18.294 +
  18.295 +/* Outputting bits to the file */
  18.296 +
  18.297 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  18.298 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  18.299 + * in one call, and we never retain more than 7 bits in put_buffer
  18.300 + * between calls, so 24 bits are sufficient.
  18.301 + */
  18.302 +
  18.303 +INLINE
  18.304 +LOCAL(boolean)
  18.305 +emit_bits (working_state * state, unsigned int code, int size)
  18.306 +/* Emit some bits; return TRUE if successful, FALSE if must suspend */
  18.307 +{
  18.308 +  /* This routine is heavily used, so it's worth coding tightly. */
  18.309 +  register INT32 put_buffer = (INT32) code;
  18.310 +  register int put_bits = state->cur.put_bits;
  18.311 +
  18.312 +  /* if size is 0, caller used an invalid Huffman table entry */
  18.313 +  if (size == 0)
  18.314 +    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
  18.315 +
  18.316 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  18.317 +  
  18.318 +  put_bits += size;		/* new number of bits in buffer */
  18.319 +  
  18.320 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  18.321 +
  18.322 +  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  18.323 +  
  18.324 +  while (put_bits >= 8) {
  18.325 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  18.326 +    
  18.327 +    emit_byte(state, c, return FALSE);
  18.328 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  18.329 +      emit_byte(state, 0, return FALSE);
  18.330 +    }
  18.331 +    put_buffer <<= 8;
  18.332 +    put_bits -= 8;
  18.333 +  }
  18.334 +
  18.335 +  state->cur.put_buffer = put_buffer; /* update state variables */
  18.336 +  state->cur.put_bits = put_bits;
  18.337 +
  18.338 +  return TRUE;
  18.339 +}
  18.340 +
  18.341 +
  18.342 +LOCAL(boolean)
  18.343 +flush_bits (working_state * state)
  18.344 +{
  18.345 +  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
  18.346 +    return FALSE;
  18.347 +  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
  18.348 +  state->cur.put_bits = 0;
  18.349 +  return TRUE;
  18.350 +}
  18.351 +
  18.352 +
  18.353 +/* Encode a single block's worth of coefficients */
  18.354 +
  18.355 +LOCAL(boolean)
  18.356 +encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
  18.357 +		  c_derived_tbl *dctbl, c_derived_tbl *actbl)
  18.358 +{
  18.359 +  register int temp, temp2;
  18.360 +  register int nbits;
  18.361 +  register int k, r, i;
  18.362 +  
  18.363 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  18.364 +  
  18.365 +  temp = temp2 = block[0] - last_dc_val;
  18.366 +
  18.367 +  if (temp < 0) {
  18.368 +    temp = -temp;		/* temp is abs value of input */
  18.369 +    /* For a negative input, want temp2 = bitwise complement of abs(input) */
  18.370 +    /* This code assumes we are on a two's complement machine */
  18.371 +    temp2--;
  18.372 +  }
  18.373 +  
  18.374 +  /* Find the number of bits needed for the magnitude of the coefficient */
  18.375 +  nbits = 0;
  18.376 +  while (temp) {
  18.377 +    nbits++;
  18.378 +    temp >>= 1;
  18.379 +  }
  18.380 +  /* Check for out-of-range coefficient values.
  18.381 +   * Since we're encoding a difference, the range limit is twice as much.
  18.382 +   */
  18.383 +  if (nbits > MAX_COEF_BITS+1)
  18.384 +    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  18.385 +  
  18.386 +  /* Emit the Huffman-coded symbol for the number of bits */
  18.387 +  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
  18.388 +    return FALSE;
  18.389 +
  18.390 +  /* Emit that number of bits of the value, if positive, */
  18.391 +  /* or the complement of its magnitude, if negative. */
  18.392 +  if (nbits)			/* emit_bits rejects calls with size 0 */
  18.393 +    if (! emit_bits(state, (unsigned int) temp2, nbits))
  18.394 +      return FALSE;
  18.395 +
  18.396 +  /* Encode the AC coefficients per section F.1.2.2 */
  18.397 +  
  18.398 +  r = 0;			/* r = run length of zeros */
  18.399 +  
  18.400 +  for (k = 1; k < DCTSIZE2; k++) {
  18.401 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  18.402 +      r++;
  18.403 +    } else {
  18.404 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  18.405 +      while (r > 15) {
  18.406 +	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
  18.407 +	  return FALSE;
  18.408 +	r -= 16;
  18.409 +      }
  18.410 +
  18.411 +      temp2 = temp;
  18.412 +      if (temp < 0) {
  18.413 +	temp = -temp;		/* temp is abs value of input */
  18.414 +	/* This code assumes we are on a two's complement machine */
  18.415 +	temp2--;
  18.416 +      }
  18.417 +      
  18.418 +      /* Find the number of bits needed for the magnitude of the coefficient */
  18.419 +      nbits = 1;		/* there must be at least one 1 bit */
  18.420 +      while ((temp >>= 1))
  18.421 +	nbits++;
  18.422 +      /* Check for out-of-range coefficient values */
  18.423 +      if (nbits > MAX_COEF_BITS)
  18.424 +	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  18.425 +      
  18.426 +      /* Emit Huffman symbol for run length / number of bits */
  18.427 +      i = (r << 4) + nbits;
  18.428 +      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
  18.429 +	return FALSE;
  18.430 +
  18.431 +      /* Emit that number of bits of the value, if positive, */
  18.432 +      /* or the complement of its magnitude, if negative. */
  18.433 +      if (! emit_bits(state, (unsigned int) temp2, nbits))
  18.434 +	return FALSE;
  18.435 +      
  18.436 +      r = 0;
  18.437 +    }
  18.438 +  }
  18.439 +
  18.440 +  /* If the last coef(s) were zero, emit an end-of-block code */
  18.441 +  if (r > 0)
  18.442 +    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
  18.443 +      return FALSE;
  18.444 +
  18.445 +  return TRUE;
  18.446 +}
  18.447 +
  18.448 +
  18.449 +/*
  18.450 + * Emit a restart marker & resynchronize predictions.
  18.451 + */
  18.452 +
  18.453 +LOCAL(boolean)
  18.454 +emit_restart (working_state * state, int restart_num)
  18.455 +{
  18.456 +  int ci;
  18.457 +
  18.458 +  if (! flush_bits(state))
  18.459 +    return FALSE;
  18.460 +
  18.461 +  emit_byte(state, 0xFF, return FALSE);
  18.462 +  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  18.463 +
  18.464 +  /* Re-initialize DC predictions to 0 */
  18.465 +  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  18.466 +    state->cur.last_dc_val[ci] = 0;
  18.467 +
  18.468 +  /* The restart counter is not updated until we successfully write the MCU. */
  18.469 +
  18.470 +  return TRUE;
  18.471 +}
  18.472 +
  18.473 +
  18.474 +/*
  18.475 + * Encode and output one MCU's worth of Huffman-compressed coefficients.
  18.476 + */
  18.477 +
  18.478 +METHODDEF(boolean)
  18.479 +encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  18.480 +{
  18.481 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  18.482 +  working_state state;
  18.483 +  int blkn, ci;
  18.484 +  jpeg_component_info * compptr;
  18.485 +
  18.486 +  /* Load up working state */
  18.487 +  state.next_output_byte = cinfo->dest->next_output_byte;
  18.488 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  18.489 +  ASSIGN_STATE(state.cur, entropy->saved);
  18.490 +  state.cinfo = cinfo;
  18.491 +
  18.492 +  /* Emit restart marker if needed */
  18.493 +  if (cinfo->restart_interval) {
  18.494 +    if (entropy->restarts_to_go == 0)
  18.495 +      if (! emit_restart(&state, entropy->next_restart_num))
  18.496 +	return FALSE;
  18.497 +  }
  18.498 +
  18.499 +  /* Encode the MCU data blocks */
  18.500 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  18.501 +    ci = cinfo->MCU_membership[blkn];
  18.502 +    compptr = cinfo->cur_comp_info[ci];
  18.503 +    if (! encode_one_block(&state,
  18.504 +			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
  18.505 +			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
  18.506 +			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  18.507 +      return FALSE;
  18.508 +    /* Update last_dc_val */
  18.509 +    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  18.510 +  }
  18.511 +
  18.512 +  /* Completed MCU, so update state */
  18.513 +  cinfo->dest->next_output_byte = state.next_output_byte;
  18.514 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  18.515 +  ASSIGN_STATE(entropy->saved, state.cur);
  18.516 +
  18.517 +  /* Update restart-interval state too */
  18.518 +  if (cinfo->restart_interval) {
  18.519 +    if (entropy->restarts_to_go == 0) {
  18.520 +      entropy->restarts_to_go = cinfo->restart_interval;
  18.521 +      entropy->next_restart_num++;
  18.522 +      entropy->next_restart_num &= 7;
  18.523 +    }
  18.524 +    entropy->restarts_to_go--;
  18.525 +  }
  18.526 +
  18.527 +  return TRUE;
  18.528 +}
  18.529 +
  18.530 +
  18.531 +/*
  18.532 + * Finish up at the end of a Huffman-compressed scan.
  18.533 + */
  18.534 +
  18.535 +METHODDEF(void)
  18.536 +finish_pass_huff (j_compress_ptr cinfo)
  18.537 +{
  18.538 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  18.539 +  working_state state;
  18.540 +
  18.541 +  /* Load up working state ... flush_bits needs it */
  18.542 +  state.next_output_byte = cinfo->dest->next_output_byte;
  18.543 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  18.544 +  ASSIGN_STATE(state.cur, entropy->saved);
  18.545 +  state.cinfo = cinfo;
  18.546 +
  18.547 +  /* Flush out the last data */
  18.548 +  if (! flush_bits(&state))
  18.549 +    ERREXIT(cinfo, JERR_CANT_SUSPEND);
  18.550 +
  18.551 +  /* Update state */
  18.552 +  cinfo->dest->next_output_byte = state.next_output_byte;
  18.553 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  18.554 +  ASSIGN_STATE(entropy->saved, state.cur);
  18.555 +}
  18.556 +
  18.557 +
  18.558 +/*
  18.559 + * Huffman coding optimization.
  18.560 + *
  18.561 + * We first scan the supplied data and count the number of uses of each symbol
  18.562 + * that is to be Huffman-coded. (This process MUST agree with the code above.)
  18.563 + * Then we build a Huffman coding tree for the observed counts.
  18.564 + * Symbols which are not needed at all for the particular image are not
  18.565 + * assigned any code, which saves space in the DHT marker as well as in
  18.566 + * the compressed data.
  18.567 + */
  18.568 +
  18.569 +#ifdef ENTROPY_OPT_SUPPORTED
  18.570 +
  18.571 +
  18.572 +/* Process a single block's worth of coefficients */
  18.573 +
  18.574 +LOCAL(void)
  18.575 +htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
  18.576 +		 long dc_counts[], long ac_counts[])
  18.577 +{
  18.578 +  register int temp;
  18.579 +  register int nbits;
  18.580 +  register int k, r;
  18.581 +  
  18.582 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  18.583 +  
  18.584 +  temp = block[0] - last_dc_val;
  18.585 +  if (temp < 0)
  18.586 +    temp = -temp;
  18.587 +  
  18.588 +  /* Find the number of bits needed for the magnitude of the coefficient */
  18.589 +  nbits = 0;
  18.590 +  while (temp) {
  18.591 +    nbits++;
  18.592 +    temp >>= 1;
  18.593 +  }
  18.594 +  /* Check for out-of-range coefficient values.
  18.595 +   * Since we're encoding a difference, the range limit is twice as much.
  18.596 +   */
  18.597 +  if (nbits > MAX_COEF_BITS+1)
  18.598 +    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  18.599 +
  18.600 +  /* Count the Huffman symbol for the number of bits */
  18.601 +  dc_counts[nbits]++;
  18.602 +  
  18.603 +  /* Encode the AC coefficients per section F.1.2.2 */
  18.604 +  
  18.605 +  r = 0;			/* r = run length of zeros */
  18.606 +  
  18.607 +  for (k = 1; k < DCTSIZE2; k++) {
  18.608 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  18.609 +      r++;
  18.610 +    } else {
  18.611 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  18.612 +      while (r > 15) {
  18.613 +	ac_counts[0xF0]++;
  18.614 +	r -= 16;
  18.615 +      }
  18.616 +      
  18.617 +      /* Find the number of bits needed for the magnitude of the coefficient */
  18.618 +      if (temp < 0)
  18.619 +	temp = -temp;
  18.620 +      
  18.621 +      /* Find the number of bits needed for the magnitude of the coefficient */
  18.622 +      nbits = 1;		/* there must be at least one 1 bit */
  18.623 +      while ((temp >>= 1))
  18.624 +	nbits++;
  18.625 +      /* Check for out-of-range coefficient values */
  18.626 +      if (nbits > MAX_COEF_BITS)
  18.627 +	ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  18.628 +      
  18.629 +      /* Count Huffman symbol for run length / number of bits */
  18.630 +      ac_counts[(r << 4) + nbits]++;
  18.631 +      
  18.632 +      r = 0;
  18.633 +    }
  18.634 +  }
  18.635 +
  18.636 +  /* If the last coef(s) were zero, emit an end-of-block code */
  18.637 +  if (r > 0)
  18.638 +    ac_counts[0]++;
  18.639 +}
  18.640 +
  18.641 +
  18.642 +/*
  18.643 + * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  18.644 + * No data is actually output, so no suspension return is possible.
  18.645 + */
  18.646 +
  18.647 +METHODDEF(boolean)
  18.648 +encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  18.649 +{
  18.650 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  18.651 +  int blkn, ci;
  18.652 +  jpeg_component_info * compptr;
  18.653 +
  18.654 +  /* Take care of restart intervals if needed */
  18.655 +  if (cinfo->restart_interval) {
  18.656 +    if (entropy->restarts_to_go == 0) {
  18.657 +      /* Re-initialize DC predictions to 0 */
  18.658 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  18.659 +	entropy->saved.last_dc_val[ci] = 0;
  18.660 +      /* Update restart state */
  18.661 +      entropy->restarts_to_go = cinfo->restart_interval;
  18.662 +    }
  18.663 +    entropy->restarts_to_go--;
  18.664 +  }
  18.665 +
  18.666 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  18.667 +    ci = cinfo->MCU_membership[blkn];
  18.668 +    compptr = cinfo->cur_comp_info[ci];
  18.669 +    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  18.670 +		    entropy->dc_count_ptrs[compptr->dc_tbl_no],
  18.671 +		    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  18.672 +    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  18.673 +  }
  18.674 +
  18.675 +  return TRUE;
  18.676 +}
  18.677 +
  18.678 +
  18.679 +/*
  18.680 + * Generate the best Huffman code table for the given counts, fill htbl.
  18.681 + * Note this is also used by jcphuff.c.
  18.682 + *
  18.683 + * The JPEG standard requires that no symbol be assigned a codeword of all
  18.684 + * one bits (so that padding bits added at the end of a compressed segment
  18.685 + * can't look like a valid code).  Because of the canonical ordering of
  18.686 + * codewords, this just means that there must be an unused slot in the
  18.687 + * longest codeword length category.  Section K.2 of the JPEG spec suggests
  18.688 + * reserving such a slot by pretending that symbol 256 is a valid symbol
  18.689 + * with count 1.  In theory that's not optimal; giving it count zero but
  18.690 + * including it in the symbol set anyway should give a better Huffman code.
  18.691 + * But the theoretically better code actually seems to come out worse in
  18.692 + * practice, because it produces more all-ones bytes (which incur stuffed
  18.693 + * zero bytes in the final file).  In any case the difference is tiny.
  18.694 + *
  18.695 + * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  18.696 + * If some symbols have a very small but nonzero probability, the Huffman tree
  18.697 + * must be adjusted to meet the code length restriction.  We currently use
  18.698 + * the adjustment method suggested in JPEG section K.2.  This method is *not*
  18.699 + * optimal; it may not choose the best possible limited-length code.  But
  18.700 + * typically only very-low-frequency symbols will be given less-than-optimal
  18.701 + * lengths, so the code is almost optimal.  Experimental comparisons against
  18.702 + * an optimal limited-length-code algorithm indicate that the difference is
  18.703 + * microscopic --- usually less than a hundredth of a percent of total size.
  18.704 + * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
  18.705 + */
  18.706 +
  18.707 +GLOBAL(void)
  18.708 +jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
  18.709 +{
  18.710 +#define MAX_CLEN 32		/* assumed maximum initial code length */
  18.711 +  UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
  18.712 +  int codesize[257];		/* codesize[k] = code length of symbol k */
  18.713 +  int others[257];		/* next symbol in current branch of tree */
  18.714 +  int c1, c2;
  18.715 +  int p, i, j;
  18.716 +  long v;
  18.717 +
  18.718 +  /* This algorithm is explained in section K.2 of the JPEG standard */
  18.719 +
  18.720 +  MEMZERO(bits, SIZEOF(bits));
  18.721 +  MEMZERO(codesize, SIZEOF(codesize));
  18.722 +  for (i = 0; i < 257; i++)
  18.723 +    others[i] = -1;		/* init links to empty */
  18.724 +  
  18.725 +  freq[256] = 1;		/* make sure 256 has a nonzero count */
  18.726 +  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  18.727 +   * that no real symbol is given code-value of all ones, because 256
  18.728 +   * will be placed last in the largest codeword category.
  18.729 +   */
  18.730 +
  18.731 +  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  18.732 +
  18.733 +  for (;;) {
  18.734 +    /* Find the smallest nonzero frequency, set c1 = its symbol */
  18.735 +    /* In case of ties, take the larger symbol number */
  18.736 +    c1 = -1;
  18.737 +    v = 1000000000L;
  18.738 +    for (i = 0; i <= 256; i++) {
  18.739 +      if (freq[i] && freq[i] <= v) {
  18.740 +	v = freq[i];
  18.741 +	c1 = i;
  18.742 +      }
  18.743 +    }
  18.744 +
  18.745 +    /* Find the next smallest nonzero frequency, set c2 = its symbol */
  18.746 +    /* In case of ties, take the larger symbol number */
  18.747 +    c2 = -1;
  18.748 +    v = 1000000000L;
  18.749 +    for (i = 0; i <= 256; i++) {
  18.750 +      if (freq[i] && freq[i] <= v && i != c1) {
  18.751 +	v = freq[i];
  18.752 +	c2 = i;
  18.753 +      }
  18.754 +    }
  18.755 +
  18.756 +    /* Done if we've merged everything into one frequency */
  18.757 +    if (c2 < 0)
  18.758 +      break;
  18.759 +    
  18.760 +    /* Else merge the two counts/trees */
  18.761 +    freq[c1] += freq[c2];
  18.762 +    freq[c2] = 0;
  18.763 +
  18.764 +    /* Increment the codesize of everything in c1's tree branch */
  18.765 +    codesize[c1]++;
  18.766 +    while (others[c1] >= 0) {
  18.767 +      c1 = others[c1];
  18.768 +      codesize[c1]++;
  18.769 +    }
  18.770 +    
  18.771 +    others[c1] = c2;		/* chain c2 onto c1's tree branch */
  18.772 +    
  18.773 +    /* Increment the codesize of everything in c2's tree branch */
  18.774 +    codesize[c2]++;
  18.775 +    while (others[c2] >= 0) {
  18.776 +      c2 = others[c2];
  18.777 +      codesize[c2]++;
  18.778 +    }
  18.779 +  }
  18.780 +
  18.781 +  /* Now count the number of symbols of each code length */
  18.782 +  for (i = 0; i <= 256; i++) {
  18.783 +    if (codesize[i]) {
  18.784 +      /* The JPEG standard seems to think that this can't happen, */
  18.785 +      /* but I'm paranoid... */
  18.786 +      if (codesize[i] > MAX_CLEN)
  18.787 +	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  18.788 +
  18.789 +      bits[codesize[i]]++;
  18.790 +    }
  18.791 +  }
  18.792 +
  18.793 +  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  18.794 +   * Huffman procedure assigned any such lengths, we must adjust the coding.
  18.795 +   * Here is what the JPEG spec says about how this next bit works:
  18.796 +   * Since symbols are paired for the longest Huffman code, the symbols are
  18.797 +   * removed from this length category two at a time.  The prefix for the pair
  18.798 +   * (which is one bit shorter) is allocated to one of the pair; then,
  18.799 +   * skipping the BITS entry for that prefix length, a code word from the next
  18.800 +   * shortest nonzero BITS entry is converted into a prefix for two code words
  18.801 +   * one bit longer.
  18.802 +   */
  18.803 +  
  18.804 +  for (i = MAX_CLEN; i > 16; i--) {
  18.805 +    while (bits[i] > 0) {
  18.806 +      j = i - 2;		/* find length of new prefix to be used */
  18.807 +      while (bits[j] == 0)
  18.808 +	j--;
  18.809 +      
  18.810 +      bits[i] -= 2;		/* remove two symbols */
  18.811 +      bits[i-1]++;		/* one goes in this length */
  18.812 +      bits[j+1] += 2;		/* two new symbols in this length */
  18.813 +      bits[j]--;		/* symbol of this length is now a prefix */
  18.814 +    }
  18.815 +  }
  18.816 +
  18.817 +  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  18.818 +  while (bits[i] == 0)		/* find largest codelength still in use */
  18.819 +    i--;
  18.820 +  bits[i]--;
  18.821 +  
  18.822 +  /* Return final symbol counts (only for lengths 0..16) */
  18.823 +  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  18.824 +  
  18.825 +  /* Return a list of the symbols sorted by code length */
  18.826 +  /* It's not real clear to me why we don't need to consider the codelength
  18.827 +   * changes made above, but the JPEG spec seems to think this works.
  18.828 +   */
  18.829 +  p = 0;
  18.830 +  for (i = 1; i <= MAX_CLEN; i++) {
  18.831 +    for (j = 0; j <= 255; j++) {
  18.832 +      if (codesize[j] == i) {
  18.833 +	htbl->huffval[p] = (UINT8) j;
  18.834 +	p++;
  18.835 +      }
  18.836 +    }
  18.837 +  }
  18.838 +
  18.839 +  /* Set sent_table FALSE so updated table will be written to JPEG file. */
  18.840 +  htbl->sent_table = FALSE;
  18.841 +}
  18.842 +
  18.843 +
  18.844 +/*
  18.845 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  18.846 + */
  18.847 +
  18.848 +METHODDEF(void)
  18.849 +finish_pass_gather (j_compress_ptr cinfo)
  18.850 +{
  18.851 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  18.852 +  int ci, dctbl, actbl;
  18.853 +  jpeg_component_info * compptr;
  18.854 +  JHUFF_TBL **htblptr;
  18.855 +  boolean did_dc[NUM_HUFF_TBLS];
  18.856 +  boolean did_ac[NUM_HUFF_TBLS];
  18.857 +
  18.858 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  18.859 +   * per table, because it clobbers the input frequency counts!
  18.860 +   */
  18.861 +  MEMZERO(did_dc, SIZEOF(did_dc));
  18.862 +  MEMZERO(did_ac, SIZEOF(did_ac));
  18.863 +
  18.864 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  18.865 +    compptr = cinfo->cur_comp_info[ci];
  18.866 +    dctbl = compptr->dc_tbl_no;
  18.867 +    actbl = compptr->ac_tbl_no;
  18.868 +    if (! did_dc[dctbl]) {
  18.869 +      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
  18.870 +      if (*htblptr == NULL)
  18.871 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  18.872 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  18.873 +      did_dc[dctbl] = TRUE;
  18.874 +    }
  18.875 +    if (! did_ac[actbl]) {
  18.876 +      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
  18.877 +      if (*htblptr == NULL)
  18.878 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  18.879 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  18.880 +      did_ac[actbl] = TRUE;
  18.881 +    }
  18.882 +  }
  18.883 +}
  18.884 +
  18.885 +
  18.886 +#endif /* ENTROPY_OPT_SUPPORTED */
  18.887 +
  18.888 +
  18.889 +/*
  18.890 + * Module initialization routine for Huffman entropy encoding.
  18.891 + */
  18.892 +
  18.893 +GLOBAL(void)
  18.894 +jinit_huff_encoder (j_compress_ptr cinfo)
  18.895 +{
  18.896 +  huff_entropy_ptr entropy;
  18.897 +  int i;
  18.898 +
  18.899 +  entropy = (huff_entropy_ptr)
  18.900 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.901 +				SIZEOF(huff_entropy_encoder));
  18.902 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  18.903 +  entropy->pub.start_pass = start_pass_huff;
  18.904 +
  18.905 +  /* Mark tables unallocated */
  18.906 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  18.907 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  18.908 +#ifdef ENTROPY_OPT_SUPPORTED
  18.909 +    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  18.910 +#endif
  18.911 +  }
  18.912 +}
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/libs/libjpeg/jchuff.h	Thu Sep 08 06:28:38 2011 +0300
    19.3 @@ -0,0 +1,47 @@
    19.4 +/*
    19.5 + * jchuff.h
    19.6 + *
    19.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    19.8 + * This file is part of the Independent JPEG Group's software.
    19.9 + * For conditions of distribution and use, see the accompanying README file.
   19.10 + *
   19.11 + * This file contains declarations for Huffman entropy encoding routines
   19.12 + * that are shared between the sequential encoder (jchuff.c) and the
   19.13 + * progressive encoder (jcphuff.c).  No other modules need to see these.
   19.14 + */
   19.15 +
   19.16 +/* The legal range of a DCT coefficient is
   19.17 + *  -1024 .. +1023  for 8-bit data;
   19.18 + * -16384 .. +16383 for 12-bit data.
   19.19 + * Hence the magnitude should always fit in 10 or 14 bits respectively.
   19.20 + */
   19.21 +
   19.22 +#if BITS_IN_JSAMPLE == 8
   19.23 +#define MAX_COEF_BITS 10
   19.24 +#else
   19.25 +#define MAX_COEF_BITS 14
   19.26 +#endif
   19.27 +
   19.28 +/* Derived data constructed for each Huffman table */
   19.29 +
   19.30 +typedef struct {
   19.31 +  unsigned int ehufco[256];	/* code for each symbol */
   19.32 +  char ehufsi[256];		/* length of code for each symbol */
   19.33 +  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
   19.34 +} c_derived_tbl;
   19.35 +
   19.36 +/* Short forms of external names for systems with brain-damaged linkers. */
   19.37 +
   19.38 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   19.39 +#define jpeg_make_c_derived_tbl	jMkCDerived
   19.40 +#define jpeg_gen_optimal_table	jGenOptTbl
   19.41 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   19.42 +
   19.43 +/* Expand a Huffman table definition into the derived format */
   19.44 +EXTERN(void) jpeg_make_c_derived_tbl
   19.45 +	JPP((j_compress_ptr cinfo, boolean isDC, int tblno,
   19.46 +	     c_derived_tbl ** pdtbl));
   19.47 +
   19.48 +/* Generate an optimal table definition given the specified counts */
   19.49 +EXTERN(void) jpeg_gen_optimal_table
   19.50 +	JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]));
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/libs/libjpeg/jcinit.c	Thu Sep 08 06:28:38 2011 +0300
    20.3 @@ -0,0 +1,72 @@
    20.4 +/*
    20.5 + * jcinit.c
    20.6 + *
    20.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    20.8 + * This file is part of the Independent JPEG Group's software.
    20.9 + * For conditions of distribution and use, see the accompanying README file.
   20.10 + *
   20.11 + * This file contains initialization logic for the JPEG compressor.
   20.12 + * This routine is in charge of selecting the modules to be executed and
   20.13 + * making an initialization call to each one.
   20.14 + *
   20.15 + * Logically, this code belongs in jcmaster.c.  It's split out because
   20.16 + * linking this routine implies linking the entire compression library.
   20.17 + * For a transcoding-only application, we want to be able to use jcmaster.c
   20.18 + * without linking in the whole library.
   20.19 + */
   20.20 +
   20.21 +#define JPEG_INTERNALS
   20.22 +#include "jinclude.h"
   20.23 +#include "jpeglib.h"
   20.24 +
   20.25 +
   20.26 +/*
   20.27 + * Master selection of compression modules.
   20.28 + * This is done once at the start of processing an image.  We determine
   20.29 + * which modules will be used and give them appropriate initialization calls.
   20.30 + */
   20.31 +
   20.32 +GLOBAL(void)
   20.33 +jinit_compress_master (j_compress_ptr cinfo)
   20.34 +{
   20.35 +  /* Initialize master control (includes parameter checking/processing) */
   20.36 +  jinit_c_master_control(cinfo, FALSE /* full compression */);
   20.37 +
   20.38 +  /* Preprocessing */
   20.39 +  if (! cinfo->raw_data_in) {
   20.40 +    jinit_color_converter(cinfo);
   20.41 +    jinit_downsampler(cinfo);
   20.42 +    jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
   20.43 +  }
   20.44 +  /* Forward DCT */
   20.45 +  jinit_forward_dct(cinfo);
   20.46 +  /* Entropy encoding: either Huffman or arithmetic coding. */
   20.47 +  if (cinfo->arith_code) {
   20.48 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   20.49 +  } else {
   20.50 +    if (cinfo->progressive_mode) {
   20.51 +#ifdef C_PROGRESSIVE_SUPPORTED
   20.52 +      jinit_phuff_encoder(cinfo);
   20.53 +#else
   20.54 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   20.55 +#endif
   20.56 +    } else
   20.57 +      jinit_huff_encoder(cinfo);
   20.58 +  }
   20.59 +
   20.60 +  /* Need a full-image coefficient buffer in any multi-pass mode. */
   20.61 +  jinit_c_coef_controller(cinfo,
   20.62 +		(boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
   20.63 +  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
   20.64 +
   20.65 +  jinit_marker_writer(cinfo);
   20.66 +
   20.67 +  /* We can now tell the memory manager to allocate virtual arrays. */
   20.68 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
   20.69 +
   20.70 +  /* Write the datastream header (SOI) immediately.
   20.71 +   * Frame and scan headers are postponed till later.
   20.72 +   * This lets application insert special markers after the SOI.
   20.73 +   */
   20.74 +  (*cinfo->marker->write_file_header) (cinfo);
   20.75 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/libs/libjpeg/jcmainct.c	Thu Sep 08 06:28:38 2011 +0300
    21.3 @@ -0,0 +1,293 @@
    21.4 +/*
    21.5 + * jcmainct.c
    21.6 + *
    21.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    21.8 + * This file is part of the Independent JPEG Group's software.
    21.9 + * For conditions of distribution and use, see the accompanying README file.
   21.10 + *
   21.11 + * This file contains the main buffer controller for compression.
   21.12 + * The main buffer lies between the pre-processor and the JPEG
   21.13 + * compressor proper; it holds downsampled data in the JPEG colorspace.
   21.14 + */
   21.15 +
   21.16 +#define JPEG_INTERNALS
   21.17 +#include "jinclude.h"
   21.18 +#include "jpeglib.h"
   21.19 +
   21.20 +
   21.21 +/* Note: currently, there is no operating mode in which a full-image buffer
   21.22 + * is needed at this step.  If there were, that mode could not be used with
   21.23 + * "raw data" input, since this module is bypassed in that case.  However,
   21.24 + * we've left the code here for possible use in special applications.
   21.25 + */
   21.26 +#undef FULL_MAIN_BUFFER_SUPPORTED
   21.27 +
   21.28 +
   21.29 +/* Private buffer controller object */
   21.30 +
   21.31 +typedef struct {
   21.32 +  struct jpeg_c_main_controller pub; /* public fields */
   21.33 +
   21.34 +  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
   21.35 +  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
   21.36 +  boolean suspended;		/* remember if we suspended output */
   21.37 +  J_BUF_MODE pass_mode;		/* current operating mode */
   21.38 +
   21.39 +  /* If using just a strip buffer, this points to the entire set of buffers
   21.40 +   * (we allocate one for each component).  In the full-image case, this
   21.41 +   * points to the currently accessible strips of the virtual arrays.
   21.42 +   */
   21.43 +  JSAMPARRAY buffer[MAX_COMPONENTS];
   21.44 +
   21.45 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   21.46 +  /* If using full-image storage, this array holds pointers to virtual-array
   21.47 +   * control blocks for each component.  Unused if not full-image storage.
   21.48 +   */
   21.49 +  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
   21.50 +#endif
   21.51 +} my_main_controller;
   21.52 +
   21.53 +typedef my_main_controller * my_main_ptr;
   21.54 +
   21.55 +
   21.56 +/* Forward declarations */
   21.57 +METHODDEF(void) process_data_simple_main
   21.58 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   21.59 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   21.60 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   21.61 +METHODDEF(void) process_data_buffer_main
   21.62 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   21.63 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   21.64 +#endif
   21.65 +
   21.66 +
   21.67 +/*
   21.68 + * Initialize for a processing pass.
   21.69 + */
   21.70 +
   21.71 +METHODDEF(void)
   21.72 +start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   21.73 +{
   21.74 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   21.75 +
   21.76 +  /* Do nothing in raw-data mode. */
   21.77 +  if (cinfo->raw_data_in)
   21.78 +    return;
   21.79 +
   21.80 +  main->cur_iMCU_row = 0;	/* initialize counters */
   21.81 +  main->rowgroup_ctr = 0;
   21.82 +  main->suspended = FALSE;
   21.83 +  main->pass_mode = pass_mode;	/* save mode for use by process_data */
   21.84 +
   21.85 +  switch (pass_mode) {
   21.86 +  case JBUF_PASS_THRU:
   21.87 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   21.88 +    if (main->whole_image[0] != NULL)
   21.89 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   21.90 +#endif
   21.91 +    main->pub.process_data = process_data_simple_main;
   21.92 +    break;
   21.93 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   21.94 +  case JBUF_SAVE_SOURCE:
   21.95 +  case JBUF_CRANK_DEST:
   21.96 +  case JBUF_SAVE_AND_PASS:
   21.97 +    if (main->whole_image[0] == NULL)
   21.98 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   21.99 +    main->pub.process_data = process_data_buffer_main;
  21.100 +    break;
  21.101 +#endif
  21.102 +  default:
  21.103 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.104 +    break;
  21.105 +  }
  21.106 +}
  21.107 +
  21.108 +
  21.109 +/*
  21.110 + * Process some data.
  21.111 + * This routine handles the simple pass-through mode,
  21.112 + * where we have only a strip buffer.
  21.113 + */
  21.114 +
  21.115 +METHODDEF(void)
  21.116 +process_data_simple_main (j_compress_ptr cinfo,
  21.117 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  21.118 +			  JDIMENSION in_rows_avail)
  21.119 +{
  21.120 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  21.121 +
  21.122 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  21.123 +    /* Read input data if we haven't filled the main buffer yet */
  21.124 +    if (main->rowgroup_ctr < DCTSIZE)
  21.125 +      (*cinfo->prep->pre_process_data) (cinfo,
  21.126 +					input_buf, in_row_ctr, in_rows_avail,
  21.127 +					main->buffer, &main->rowgroup_ctr,
  21.128 +					(JDIMENSION) DCTSIZE);
  21.129 +
  21.130 +    /* If we don't have a full iMCU row buffered, return to application for
  21.131 +     * more data.  Note that preprocessor will always pad to fill the iMCU row
  21.132 +     * at the bottom of the image.
  21.133 +     */
  21.134 +    if (main->rowgroup_ctr != DCTSIZE)
  21.135 +      return;
  21.136 +
  21.137 +    /* Send the completed row to the compressor */
  21.138 +    if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  21.139 +      /* If compressor did not consume the whole row, then we must need to
  21.140 +       * suspend processing and return to the application.  In this situation
  21.141 +       * we pretend we didn't yet consume the last input row; otherwise, if
  21.142 +       * it happened to be the last row of the image, the application would
  21.143 +       * think we were done.
  21.144 +       */
  21.145 +      if (! main->suspended) {
  21.146 +	(*in_row_ctr)--;
  21.147 +	main->suspended = TRUE;
  21.148 +      }
  21.149 +      return;
  21.150 +    }
  21.151 +    /* We did finish the row.  Undo our little suspension hack if a previous
  21.152 +     * call suspended; then mark the main buffer empty.
  21.153 +     */
  21.154 +    if (main->suspended) {
  21.155 +      (*in_row_ctr)++;
  21.156 +      main->suspended = FALSE;
  21.157 +    }
  21.158 +    main->rowgroup_ctr = 0;
  21.159 +    main->cur_iMCU_row++;
  21.160 +  }
  21.161 +}
  21.162 +
  21.163 +
  21.164 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  21.165 +
  21.166 +/*
  21.167 + * Process some data.
  21.168 + * This routine handles all of the modes that use a full-size buffer.
  21.169 + */
  21.170 +
  21.171 +METHODDEF(void)
  21.172 +process_data_buffer_main (j_compress_ptr cinfo,
  21.173 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  21.174 +			  JDIMENSION in_rows_avail)
  21.175 +{
  21.176 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  21.177 +  int ci;
  21.178 +  jpeg_component_info *compptr;
  21.179 +  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
  21.180 +
  21.181 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  21.182 +    /* Realign the virtual buffers if at the start of an iMCU row. */
  21.183 +    if (main->rowgroup_ctr == 0) {
  21.184 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  21.185 +	   ci++, compptr++) {
  21.186 +	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  21.187 +	  ((j_common_ptr) cinfo, main->whole_image[ci],
  21.188 +	   main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
  21.189 +	   (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
  21.190 +      }
  21.191 +      /* In a read pass, pretend we just read some source data. */
  21.192 +      if (! writing) {
  21.193 +	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
  21.194 +	main->rowgroup_ctr = DCTSIZE;
  21.195 +      }
  21.196 +    }
  21.197 +
  21.198 +    /* If a write pass, read input data until the current iMCU row is full. */
  21.199 +    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  21.200 +    if (writing) {
  21.201 +      (*cinfo->prep->pre_process_data) (cinfo,
  21.202 +					input_buf, in_row_ctr, in_rows_avail,
  21.203 +					main->buffer, &main->rowgroup_ctr,
  21.204 +					(JDIMENSION) DCTSIZE);
  21.205 +      /* Return to application if we need more data to fill the iMCU row. */
  21.206 +      if (main->rowgroup_ctr < DCTSIZE)
  21.207 +	return;
  21.208 +    }
  21.209 +
  21.210 +    /* Emit data, unless this is a sink-only pass. */
  21.211 +    if (main->pass_mode != JBUF_SAVE_SOURCE) {
  21.212 +      if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  21.213 +	/* If compressor did not consume the whole row, then we must need to
  21.214 +	 * suspend processing and return to the application.  In this situation
  21.215 +	 * we pretend we didn't yet consume the last input row; otherwise, if
  21.216 +	 * it happened to be the last row of the image, the application would
  21.217 +	 * think we were done.
  21.218 +	 */
  21.219 +	if (! main->suspended) {
  21.220 +	  (*in_row_ctr)--;
  21.221 +	  main->suspended = TRUE;
  21.222 +	}
  21.223 +	return;
  21.224 +      }
  21.225 +      /* We did finish the row.  Undo our little suspension hack if a previous
  21.226 +       * call suspended; then mark the main buffer empty.
  21.227 +       */
  21.228 +      if (main->suspended) {
  21.229 +	(*in_row_ctr)++;
  21.230 +	main->suspended = FALSE;
  21.231 +      }
  21.232 +    }
  21.233 +
  21.234 +    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
  21.235 +    main->rowgroup_ctr = 0;
  21.236 +    main->cur_iMCU_row++;
  21.237 +  }
  21.238 +}
  21.239 +
  21.240 +#endif /* FULL_MAIN_BUFFER_SUPPORTED */
  21.241 +
  21.242 +
  21.243 +/*
  21.244 + * Initialize main buffer controller.
  21.245 + */
  21.246 +
  21.247 +GLOBAL(void)
  21.248 +jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  21.249 +{
  21.250 +  my_main_ptr main;
  21.251 +  int ci;
  21.252 +  jpeg_component_info *compptr;
  21.253 +
  21.254 +  main = (my_main_ptr)
  21.255 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.256 +				SIZEOF(my_main_controller));
  21.257 +  cinfo->main = (struct jpeg_c_main_controller *) main;
  21.258 +  main->pub.start_pass = start_pass_main;
  21.259 +
  21.260 +  /* We don't need to create a buffer in raw-data mode. */
  21.261 +  if (cinfo->raw_data_in)
  21.262 +    return;
  21.263 +
  21.264 +  /* Create the buffer.  It holds downsampled data, so each component
  21.265 +   * may be of a different size.
  21.266 +   */
  21.267 +  if (need_full_buffer) {
  21.268 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  21.269 +    /* Allocate a full-image virtual array for each component */
  21.270 +    /* Note we pad the bottom to a multiple of the iMCU height */
  21.271 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  21.272 +	 ci++, compptr++) {
  21.273 +      main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  21.274 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  21.275 +	 compptr->width_in_blocks * DCTSIZE,
  21.276 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  21.277 +				(long) compptr->v_samp_factor) * DCTSIZE,
  21.278 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  21.279 +    }
  21.280 +#else
  21.281 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.282 +#endif
  21.283 +  } else {
  21.284 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  21.285 +    main->whole_image[0] = NULL; /* flag for no virtual arrays */
  21.286 +#endif
  21.287 +    /* Allocate a strip buffer for each component */
  21.288 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  21.289 +	 ci++, compptr++) {
  21.290 +      main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  21.291 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.292 +	 compptr->width_in_blocks * DCTSIZE,
  21.293 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  21.294 +    }
  21.295 +  }
  21.296 +}
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/libs/libjpeg/jcmarker.c	Thu Sep 08 06:28:38 2011 +0300
    22.3 @@ -0,0 +1,664 @@
    22.4 +/*
    22.5 + * jcmarker.c
    22.6 + *
    22.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    22.8 + * This file is part of the Independent JPEG Group's software.
    22.9 + * For conditions of distribution and use, see the accompanying README file.
   22.10 + *
   22.11 + * This file contains routines to write JPEG datastream markers.
   22.12 + */
   22.13 +
   22.14 +#define JPEG_INTERNALS
   22.15 +#include "jinclude.h"
   22.16 +#include "jpeglib.h"
   22.17 +
   22.18 +
   22.19 +typedef enum {			/* JPEG marker codes */
   22.20 +  M_SOF0  = 0xc0,
   22.21 +  M_SOF1  = 0xc1,
   22.22 +  M_SOF2  = 0xc2,
   22.23 +  M_SOF3  = 0xc3,
   22.24 +  
   22.25 +  M_SOF5  = 0xc5,
   22.26 +  M_SOF6  = 0xc6,
   22.27 +  M_SOF7  = 0xc7,
   22.28 +  
   22.29 +  M_JPG   = 0xc8,
   22.30 +  M_SOF9  = 0xc9,
   22.31 +  M_SOF10 = 0xca,
   22.32 +  M_SOF11 = 0xcb,
   22.33 +  
   22.34 +  M_SOF13 = 0xcd,
   22.35 +  M_SOF14 = 0xce,
   22.36 +  M_SOF15 = 0xcf,
   22.37 +  
   22.38 +  M_DHT   = 0xc4,
   22.39 +  
   22.40 +  M_DAC   = 0xcc,
   22.41 +  
   22.42 +  M_RST0  = 0xd0,
   22.43 +  M_RST1  = 0xd1,
   22.44 +  M_RST2  = 0xd2,
   22.45 +  M_RST3  = 0xd3,
   22.46 +  M_RST4  = 0xd4,
   22.47 +  M_RST5  = 0xd5,
   22.48 +  M_RST6  = 0xd6,
   22.49 +  M_RST7  = 0xd7,
   22.50 +  
   22.51 +  M_SOI   = 0xd8,
   22.52 +  M_EOI   = 0xd9,
   22.53 +  M_SOS   = 0xda,
   22.54 +  M_DQT   = 0xdb,
   22.55 +  M_DNL   = 0xdc,
   22.56 +  M_DRI   = 0xdd,
   22.57 +  M_DHP   = 0xde,
   22.58 +  M_EXP   = 0xdf,
   22.59 +  
   22.60 +  M_APP0  = 0xe0,
   22.61 +  M_APP1  = 0xe1,
   22.62 +  M_APP2  = 0xe2,
   22.63 +  M_APP3  = 0xe3,
   22.64 +  M_APP4  = 0xe4,
   22.65 +  M_APP5  = 0xe5,
   22.66 +  M_APP6  = 0xe6,
   22.67 +  M_APP7  = 0xe7,
   22.68 +  M_APP8  = 0xe8,
   22.69 +  M_APP9  = 0xe9,
   22.70 +  M_APP10 = 0xea,
   22.71 +  M_APP11 = 0xeb,
   22.72 +  M_APP12 = 0xec,
   22.73 +  M_APP13 = 0xed,
   22.74 +  M_APP14 = 0xee,
   22.75 +  M_APP15 = 0xef,
   22.76 +  
   22.77 +  M_JPG0  = 0xf0,
   22.78 +  M_JPG13 = 0xfd,
   22.79 +  M_COM   = 0xfe,
   22.80 +  
   22.81 +  M_TEM   = 0x01,
   22.82 +  
   22.83 +  M_ERROR = 0x100
   22.84 +} JPEG_MARKER;
   22.85 +
   22.86 +
   22.87 +/* Private state */
   22.88 +
   22.89 +typedef struct {
   22.90 +  struct jpeg_marker_writer pub; /* public fields */
   22.91 +
   22.92 +  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
   22.93 +} my_marker_writer;
   22.94 +
   22.95 +typedef my_marker_writer * my_marker_ptr;
   22.96 +
   22.97 +
   22.98 +/*
   22.99 + * Basic output routines.
  22.100 + *
  22.101 + * Note that we do not support suspension while writing a marker.
  22.102 + * Therefore, an application using suspension must ensure that there is
  22.103 + * enough buffer space for the initial markers (typ. 600-700 bytes) before
  22.104 + * calling jpeg_start_compress, and enough space to write the trailing EOI
  22.105 + * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
  22.106 + * modes are not supported at all with suspension, so those two are the only
  22.107 + * points where markers will be written.
  22.108 + */
  22.109 +
  22.110 +LOCAL(void)
  22.111 +emit_byte (j_compress_ptr cinfo, int val)
  22.112 +/* Emit a byte */
  22.113 +{
  22.114 +  struct jpeg_destination_mgr * dest = cinfo->dest;
  22.115 +
  22.116 +  *(dest->next_output_byte)++ = (JOCTET) val;
  22.117 +  if (--dest->free_in_buffer == 0) {
  22.118 +    if (! (*dest->empty_output_buffer) (cinfo))
  22.119 +      ERREXIT(cinfo, JERR_CANT_SUSPEND);
  22.120 +  }
  22.121 +}
  22.122 +
  22.123 +
  22.124 +LOCAL(void)
  22.125 +emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
  22.126 +/* Emit a marker code */
  22.127 +{
  22.128 +  emit_byte(cinfo, 0xFF);
  22.129 +  emit_byte(cinfo, (int) mark);
  22.130 +}
  22.131 +
  22.132 +
  22.133 +LOCAL(void)
  22.134 +emit_2bytes (j_compress_ptr cinfo, int value)
  22.135 +/* Emit a 2-byte integer; these are always MSB first in JPEG files */
  22.136 +{
  22.137 +  emit_byte(cinfo, (value >> 8) & 0xFF);
  22.138 +  emit_byte(cinfo, value & 0xFF);
  22.139 +}
  22.140 +
  22.141 +
  22.142 +/*
  22.143 + * Routines to write specific marker types.
  22.144 + */
  22.145 +
  22.146 +LOCAL(int)
  22.147 +emit_dqt (j_compress_ptr cinfo, int index)
  22.148 +/* Emit a DQT marker */
  22.149 +/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
  22.150 +{
  22.151 +  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
  22.152 +  int prec;
  22.153 +  int i;
  22.154 +
  22.155 +  if (qtbl == NULL)
  22.156 +    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
  22.157 +
  22.158 +  prec = 0;
  22.159 +  for (i = 0; i < DCTSIZE2; i++) {
  22.160 +    if (qtbl->quantval[i] > 255)
  22.161 +      prec = 1;
  22.162 +  }
  22.163 +
  22.164 +  if (! qtbl->sent_table) {
  22.165 +    emit_marker(cinfo, M_DQT);
  22.166 +
  22.167 +    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
  22.168 +
  22.169 +    emit_byte(cinfo, index + (prec<<4));
  22.170 +
  22.171 +    for (i = 0; i < DCTSIZE2; i++) {
  22.172 +      /* The table entries must be emitted in zigzag order. */
  22.173 +      unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
  22.174 +      if (prec)
  22.175 +	emit_byte(cinfo, (int) (qval >> 8));
  22.176 +      emit_byte(cinfo, (int) (qval & 0xFF));
  22.177 +    }
  22.178 +
  22.179 +    qtbl->sent_table = TRUE;
  22.180 +  }
  22.181 +
  22.182 +  return prec;
  22.183 +}
  22.184 +
  22.185 +
  22.186 +LOCAL(void)
  22.187 +emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
  22.188 +/* Emit a DHT marker */
  22.189 +{
  22.190 +  JHUFF_TBL * htbl;
  22.191 +  int length, i;
  22.192 +  
  22.193 +  if (is_ac) {
  22.194 +    htbl = cinfo->ac_huff_tbl_ptrs[index];
  22.195 +    index += 0x10;		/* output index has AC bit set */
  22.196 +  } else {
  22.197 +    htbl = cinfo->dc_huff_tbl_ptrs[index];
  22.198 +  }
  22.199 +
  22.200 +  if (htbl == NULL)
  22.201 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
  22.202 +  
  22.203 +  if (! htbl->sent_table) {
  22.204 +    emit_marker(cinfo, M_DHT);
  22.205 +    
  22.206 +    length = 0;
  22.207 +    for (i = 1; i <= 16; i++)
  22.208 +      length += htbl->bits[i];
  22.209 +    
  22.210 +    emit_2bytes(cinfo, length + 2 + 1 + 16);
  22.211 +    emit_byte(cinfo, index);
  22.212 +    
  22.213 +    for (i = 1; i <= 16; i++)
  22.214 +      emit_byte(cinfo, htbl->bits[i]);
  22.215 +    
  22.216 +    for (i = 0; i < length; i++)
  22.217 +      emit_byte(cinfo, htbl->huffval[i]);
  22.218 +    
  22.219 +    htbl->sent_table = TRUE;
  22.220 +  }
  22.221 +}
  22.222 +
  22.223 +
  22.224 +LOCAL(void)
  22.225 +emit_dac (j_compress_ptr cinfo)
  22.226 +/* Emit a DAC marker */
  22.227 +/* Since the useful info is so small, we want to emit all the tables in */
  22.228 +/* one DAC marker.  Therefore this routine does its own scan of the table. */
  22.229 +{
  22.230 +#ifdef C_ARITH_CODING_SUPPORTED
  22.231 +  char dc_in_use[NUM_ARITH_TBLS];
  22.232 +  char ac_in_use[NUM_ARITH_TBLS];
  22.233 +  int length, i;
  22.234 +  jpeg_component_info *compptr;
  22.235 +  
  22.236 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  22.237 +    dc_in_use[i] = ac_in_use[i] = 0;
  22.238 +  
  22.239 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  22.240 +    compptr = cinfo->cur_comp_info[i];
  22.241 +    dc_in_use[compptr->dc_tbl_no] = 1;
  22.242 +    ac_in_use[compptr->ac_tbl_no] = 1;
  22.243 +  }
  22.244 +  
  22.245 +  length = 0;
  22.246 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  22.247 +    length += dc_in_use[i] + ac_in_use[i];
  22.248 +  
  22.249 +  emit_marker(cinfo, M_DAC);
  22.250 +  
  22.251 +  emit_2bytes(cinfo, length*2 + 2);
  22.252 +  
  22.253 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  22.254 +    if (dc_in_use[i]) {
  22.255 +      emit_byte(cinfo, i);
  22.256 +      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
  22.257 +    }
  22.258 +    if (ac_in_use[i]) {
  22.259 +      emit_byte(cinfo, i + 0x10);
  22.260 +      emit_byte(cinfo, cinfo->arith_ac_K[i]);
  22.261 +    }
  22.262 +  }
  22.263 +#endif /* C_ARITH_CODING_SUPPORTED */
  22.264 +}
  22.265 +
  22.266 +
  22.267 +LOCAL(void)
  22.268 +emit_dri (j_compress_ptr cinfo)
  22.269 +/* Emit a DRI marker */
  22.270 +{
  22.271 +  emit_marker(cinfo, M_DRI);
  22.272 +  
  22.273 +  emit_2bytes(cinfo, 4);	/* fixed length */
  22.274 +
  22.275 +  emit_2bytes(cinfo, (int) cinfo->restart_interval);
  22.276 +}
  22.277 +
  22.278 +
  22.279 +LOCAL(void)
  22.280 +emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
  22.281 +/* Emit a SOF marker */
  22.282 +{
  22.283 +  int ci;
  22.284 +  jpeg_component_info *compptr;
  22.285 +  
  22.286 +  emit_marker(cinfo, code);
  22.287 +  
  22.288 +  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
  22.289 +
  22.290 +  /* Make sure image isn't bigger than SOF field can handle */
  22.291 +  if ((long) cinfo->image_height > 65535L ||
  22.292 +      (long) cinfo->image_width > 65535L)
  22.293 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
  22.294 +
  22.295 +  emit_byte(cinfo, cinfo->data_precision);
  22.296 +  emit_2bytes(cinfo, (int) cinfo->image_height);
  22.297 +  emit_2bytes(cinfo, (int) cinfo->image_width);
  22.298 +
  22.299 +  emit_byte(cinfo, cinfo->num_components);
  22.300 +
  22.301 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  22.302 +       ci++, compptr++) {
  22.303 +    emit_byte(cinfo, compptr->component_id);
  22.304 +    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
  22.305 +    emit_byte(cinfo, compptr->quant_tbl_no);
  22.306 +  }
  22.307 +}
  22.308 +
  22.309 +
  22.310 +LOCAL(void)
  22.311 +emit_sos (j_compress_ptr cinfo)
  22.312 +/* Emit a SOS marker */
  22.313 +{
  22.314 +  int i, td, ta;
  22.315 +  jpeg_component_info *compptr;
  22.316 +  
  22.317 +  emit_marker(cinfo, M_SOS);
  22.318 +  
  22.319 +  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
  22.320 +  
  22.321 +  emit_byte(cinfo, cinfo->comps_in_scan);
  22.322 +  
  22.323 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  22.324 +    compptr = cinfo->cur_comp_info[i];
  22.325 +    emit_byte(cinfo, compptr->component_id);
  22.326 +    td = compptr->dc_tbl_no;
  22.327 +    ta = compptr->ac_tbl_no;
  22.328 +    if (cinfo->progressive_mode) {
  22.329 +      /* Progressive mode: only DC or only AC tables are used in one scan;
  22.330 +       * furthermore, Huffman coding of DC refinement uses no table at all.
  22.331 +       * We emit 0 for unused field(s); this is recommended by the P&M text
  22.332 +       * but does not seem to be specified in the standard.
  22.333 +       */
  22.334 +      if (cinfo->Ss == 0) {
  22.335 +	ta = 0;			/* DC scan */
  22.336 +	if (cinfo->Ah != 0 && !cinfo->arith_code)
  22.337 +	  td = 0;		/* no DC table either */
  22.338 +      } else {
  22.339 +	td = 0;			/* AC scan */
  22.340 +      }
  22.341 +    }
  22.342 +    emit_byte(cinfo, (td << 4) + ta);
  22.343 +  }
  22.344 +
  22.345 +  emit_byte(cinfo, cinfo->Ss);
  22.346 +  emit_byte(cinfo, cinfo->Se);
  22.347 +  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
  22.348 +}
  22.349 +
  22.350 +
  22.351 +LOCAL(void)
  22.352 +emit_jfif_app0 (j_compress_ptr cinfo)
  22.353 +/* Emit a JFIF-compliant APP0 marker */
  22.354 +{
  22.355 +  /*
  22.356 +   * Length of APP0 block	(2 bytes)
  22.357 +   * Block ID			(4 bytes - ASCII "JFIF")
  22.358 +   * Zero byte			(1 byte to terminate the ID string)
  22.359 +   * Version Major, Minor	(2 bytes - major first)
  22.360 +   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
  22.361 +   * Xdpu			(2 bytes - dots per unit horizontal)
  22.362 +   * Ydpu			(2 bytes - dots per unit vertical)
  22.363 +   * Thumbnail X size		(1 byte)
  22.364 +   * Thumbnail Y size		(1 byte)
  22.365 +   */
  22.366 +  
  22.367 +  emit_marker(cinfo, M_APP0);
  22.368 +  
  22.369 +  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
  22.370 +
  22.371 +  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
  22.372 +  emit_byte(cinfo, 0x46);
  22.373 +  emit_byte(cinfo, 0x49);
  22.374 +  emit_byte(cinfo, 0x46);
  22.375 +  emit_byte(cinfo, 0);
  22.376 +  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
  22.377 +  emit_byte(cinfo, cinfo->JFIF_minor_version);
  22.378 +  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  22.379 +  emit_2bytes(cinfo, (int) cinfo->X_density);
  22.380 +  emit_2bytes(cinfo, (int) cinfo->Y_density);
  22.381 +  emit_byte(cinfo, 0);		/* No thumbnail image */
  22.382 +  emit_byte(cinfo, 0);
  22.383 +}
  22.384 +
  22.385 +
  22.386 +LOCAL(void)
  22.387 +emit_adobe_app14 (j_compress_ptr cinfo)
  22.388 +/* Emit an Adobe APP14 marker */
  22.389 +{
  22.390 +  /*
  22.391 +   * Length of APP14 block	(2 bytes)
  22.392 +   * Block ID			(5 bytes - ASCII "Adobe")
  22.393 +   * Version Number		(2 bytes - currently 100)
  22.394 +   * Flags0			(2 bytes - currently 0)
  22.395 +   * Flags1			(2 bytes - currently 0)
  22.396 +   * Color transform		(1 byte)
  22.397 +   *
  22.398 +   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
  22.399 +   * now in circulation seem to use Version = 100, so that's what we write.
  22.400 +   *
  22.401 +   * We write the color transform byte as 1 if the JPEG color space is
  22.402 +   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
  22.403 +   * whether the encoder performed a transformation, which is pretty useless.
  22.404 +   */
  22.405 +  
  22.406 +  emit_marker(cinfo, M_APP14);
  22.407 +  
  22.408 +  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
  22.409 +
  22.410 +  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
  22.411 +  emit_byte(cinfo, 0x64);
  22.412 +  emit_byte(cinfo, 0x6F);
  22.413 +  emit_byte(cinfo, 0x62);
  22.414 +  emit_byte(cinfo, 0x65);
  22.415 +  emit_2bytes(cinfo, 100);	/* Version */
  22.416 +  emit_2bytes(cinfo, 0);	/* Flags0 */
  22.417 +  emit_2bytes(cinfo, 0);	/* Flags1 */
  22.418 +  switch (cinfo->jpeg_color_space) {
  22.419 +  case JCS_YCbCr:
  22.420 +    emit_byte(cinfo, 1);	/* Color transform = 1 */
  22.421 +    break;
  22.422 +  case JCS_YCCK:
  22.423 +    emit_byte(cinfo, 2);	/* Color transform = 2 */
  22.424 +    break;
  22.425 +  default:
  22.426 +    emit_byte(cinfo, 0);	/* Color transform = 0 */
  22.427 +    break;
  22.428 +  }
  22.429 +}
  22.430 +
  22.431 +
  22.432 +/*
  22.433 + * These routines allow writing an arbitrary marker with parameters.
  22.434 + * The only intended use is to emit COM or APPn markers after calling
  22.435 + * write_file_header and before calling write_frame_header.
  22.436 + * Other uses are not guaranteed to produce desirable results.
  22.437 + * Counting the parameter bytes properly is the caller's responsibility.
  22.438 + */
  22.439 +
  22.440 +METHODDEF(void)
  22.441 +write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  22.442 +/* Emit an arbitrary marker header */
  22.443 +{
  22.444 +  if (datalen > (unsigned int) 65533)		/* safety check */
  22.445 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  22.446 +
  22.447 +  emit_marker(cinfo, (JPEG_MARKER) marker);
  22.448 +
  22.449 +  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
  22.450 +}
  22.451 +
  22.452 +METHODDEF(void)
  22.453 +write_marker_byte (j_compress_ptr cinfo, int val)
  22.454 +/* Emit one byte of marker parameters following write_marker_header */
  22.455 +{
  22.456 +  emit_byte(cinfo, val);
  22.457 +}
  22.458 +
  22.459 +
  22.460 +/*
  22.461 + * Write datastream header.
  22.462 + * This consists of an SOI and optional APPn markers.
  22.463 + * We recommend use of the JFIF marker, but not the Adobe marker,
  22.464 + * when using YCbCr or grayscale data.  The JFIF marker should NOT
  22.465 + * be used for any other JPEG colorspace.  The Adobe marker is helpful
  22.466 + * to distinguish RGB, CMYK, and YCCK colorspaces.
  22.467 + * Note that an application can write additional header markers after
  22.468 + * jpeg_start_compress returns.
  22.469 + */
  22.470 +
  22.471 +METHODDEF(void)
  22.472 +write_file_header (j_compress_ptr cinfo)
  22.473 +{
  22.474 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  22.475 +
  22.476 +  emit_marker(cinfo, M_SOI);	/* first the SOI */
  22.477 +
  22.478 +  /* SOI is defined to reset restart interval to 0 */
  22.479 +  marker->last_restart_interval = 0;
  22.480 +
  22.481 +  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
  22.482 +    emit_jfif_app0(cinfo);
  22.483 +  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
  22.484 +    emit_adobe_app14(cinfo);
  22.485 +}
  22.486 +
  22.487 +
  22.488 +/*
  22.489 + * Write frame header.
  22.490 + * This consists of DQT and SOFn markers.
  22.491 + * Note that we do not emit the SOF until we have emitted the DQT(s).
  22.492 + * This avoids compatibility problems with incorrect implementations that
  22.493 + * try to error-check the quant table numbers as soon as they see the SOF.
  22.494 + */
  22.495 +
  22.496 +METHODDEF(void)
  22.497 +write_frame_header (j_compress_ptr cinfo)
  22.498 +{
  22.499 +  int ci, prec;
  22.500 +  boolean is_baseline;
  22.501 +  jpeg_component_info *compptr;
  22.502 +  
  22.503 +  /* Emit DQT for each quantization table.
  22.504 +   * Note that emit_dqt() suppresses any duplicate tables.
  22.505 +   */
  22.506 +  prec = 0;
  22.507 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  22.508 +       ci++, compptr++) {
  22.509 +    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
  22.510 +  }
  22.511 +  /* now prec is nonzero iff there are any 16-bit quant tables. */
  22.512 +
  22.513 +  /* Check for a non-baseline specification.
  22.514 +   * Note we assume that Huffman table numbers won't be changed later.
  22.515 +   */
  22.516 +  if (cinfo->arith_code || cinfo->progressive_mode ||
  22.517 +      cinfo->data_precision != 8) {
  22.518 +    is_baseline = FALSE;
  22.519 +  } else {
  22.520 +    is_baseline = TRUE;
  22.521 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  22.522 +	 ci++, compptr++) {
  22.523 +      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
  22.524 +	is_baseline = FALSE;
  22.525 +    }
  22.526 +    if (prec && is_baseline) {
  22.527 +      is_baseline = FALSE;
  22.528 +      /* If it's baseline except for quantizer size, warn the user */
  22.529 +      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
  22.530 +    }
  22.531 +  }
  22.532 +
  22.533 +  /* Emit the proper SOF marker */
  22.534 +  if (cinfo->arith_code) {
  22.535 +    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
  22.536 +  } else {
  22.537 +    if (cinfo->progressive_mode)
  22.538 +      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
  22.539 +    else if (is_baseline)
  22.540 +      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
  22.541 +    else
  22.542 +      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
  22.543 +  }
  22.544 +}
  22.545 +
  22.546 +
  22.547 +/*
  22.548 + * Write scan header.
  22.549 + * This consists of DHT or DAC markers, optional DRI, and SOS.
  22.550 + * Compressed data will be written following the SOS.
  22.551 + */
  22.552 +
  22.553 +METHODDEF(void)
  22.554 +write_scan_header (j_compress_ptr cinfo)
  22.555 +{
  22.556 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  22.557 +  int i;
  22.558 +  jpeg_component_info *compptr;
  22.559 +
  22.560 +  if (cinfo->arith_code) {
  22.561 +    /* Emit arith conditioning info.  We may have some duplication
  22.562 +     * if the file has multiple scans, but it's so small it's hardly
  22.563 +     * worth worrying about.
  22.564 +     */
  22.565 +    emit_dac(cinfo);
  22.566 +  } else {
  22.567 +    /* Emit Huffman tables.
  22.568 +     * Note that emit_dht() suppresses any duplicate tables.
  22.569 +     */
  22.570 +    for (i = 0; i < cinfo->comps_in_scan; i++) {
  22.571 +      compptr = cinfo->cur_comp_info[i];
  22.572 +      if (cinfo->progressive_mode) {
  22.573 +	/* Progressive mode: only DC or only AC tables are used in one scan */
  22.574 +	if (cinfo->Ss == 0) {
  22.575 +	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
  22.576 +	    emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  22.577 +	} else {
  22.578 +	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  22.579 +	}
  22.580 +      } else {
  22.581 +	/* Sequential mode: need both DC and AC tables */
  22.582 +	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  22.583 +	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  22.584 +      }
  22.585 +    }
  22.586 +  }
  22.587 +
  22.588 +  /* Emit DRI if required --- note that DRI value could change for each scan.
  22.589 +   * We avoid wasting space with unnecessary DRIs, however.
  22.590 +   */
  22.591 +  if (cinfo->restart_interval != marker->last_restart_interval) {
  22.592 +    emit_dri(cinfo);
  22.593 +    marker->last_restart_interval = cinfo->restart_interval;
  22.594 +  }
  22.595 +
  22.596 +  emit_sos(cinfo);
  22.597 +}
  22.598 +
  22.599 +
  22.600 +/*
  22.601 + * Write datastream trailer.
  22.602 + */
  22.603 +
  22.604 +METHODDEF(void)
  22.605 +write_file_trailer (j_compress_ptr cinfo)
  22.606 +{
  22.607 +  emit_marker(cinfo, M_EOI);
  22.608 +}
  22.609 +
  22.610 +
  22.611 +/*
  22.612 + * Write an abbreviated table-specification datastream.
  22.613 + * This consists of SOI, DQT and DHT tables, and EOI.
  22.614 + * Any table that is defined and not marked sent_table = TRUE will be
  22.615 + * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
  22.616 + */
  22.617 +
  22.618 +METHODDEF(void)
  22.619 +write_tables_only (j_compress_ptr cinfo)
  22.620 +{
  22.621 +  int i;
  22.622 +
  22.623 +  emit_marker(cinfo, M_SOI);
  22.624 +
  22.625 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  22.626 +    if (cinfo->quant_tbl_ptrs[i] != NULL)
  22.627 +      (void) emit_dqt(cinfo, i);
  22.628 +  }
  22.629 +
  22.630 +  if (! cinfo->arith_code) {
  22.631 +    for (i = 0; i < NUM_HUFF_TBLS; i++) {
  22.632 +      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
  22.633 +	emit_dht(cinfo, i, FALSE);
  22.634 +      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
  22.635 +	emit_dht(cinfo, i, TRUE);
  22.636 +    }
  22.637 +  }
  22.638 +
  22.639 +  emit_marker(cinfo, M_EOI);
  22.640 +}
  22.641 +
  22.642 +
  22.643 +/*
  22.644 + * Initialize the marker writer module.
  22.645 + */
  22.646 +
  22.647 +GLOBAL(void)
  22.648 +jinit_marker_writer (j_compress_ptr cinfo)
  22.649 +{
  22.650 +  my_marker_ptr marker;
  22.651 +
  22.652 +  /* Create the subobject */
  22.653 +  marker = (my_marker_ptr)
  22.654 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  22.655 +				SIZEOF(my_marker_writer));
  22.656 +  cinfo->marker = (struct jpeg_marker_writer *) marker;
  22.657 +  /* Initialize method pointers */
  22.658 +  marker->pub.write_file_header = write_file_header;
  22.659 +  marker->pub.write_frame_header = write_frame_header;
  22.660 +  marker->pub.write_scan_header = write_scan_header;
  22.661 +  marker->pub.write_file_trailer = write_file_trailer;
  22.662 +  marker->pub.write_tables_only = write_tables_only;
  22.663 +  marker->pub.write_marker_header = write_marker_header;
  22.664 +  marker->pub.write_marker_byte = write_marker_byte;
  22.665 +  /* Initialize private state */
  22.666 +  marker->last_restart_interval = 0;
  22.667 +}
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/libs/libjpeg/jcmaster.c	Thu Sep 08 06:28:38 2011 +0300
    23.3 @@ -0,0 +1,590 @@
    23.4 +/*
    23.5 + * jcmaster.c
    23.6 + *
    23.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    23.8 + * This file is part of the Independent JPEG Group's software.
    23.9 + * For conditions of distribution and use, see the accompanying README file.
   23.10 + *
   23.11 + * This file contains master control logic for the JPEG compressor.
   23.12 + * These routines are concerned with parameter validation, initial setup,
   23.13 + * and inter-pass control (determining the number of passes and the work 
   23.14 + * to be done in each pass).
   23.15 + */
   23.16 +
   23.17 +#define JPEG_INTERNALS
   23.18 +#include "jinclude.h"
   23.19 +#include "jpeglib.h"
   23.20 +
   23.21 +
   23.22 +/* Private state */
   23.23 +
   23.24 +typedef enum {
   23.25 +	main_pass,		/* input data, also do first output step */
   23.26 +	huff_opt_pass,		/* Huffman code optimization pass */
   23.27 +	output_pass		/* data output pass */
   23.28 +} c_pass_type;
   23.29 +
   23.30 +typedef struct {
   23.31 +  struct jpeg_comp_master pub;	/* public fields */
   23.32 +
   23.33 +  c_pass_type pass_type;	/* the type of the current pass */
   23.34 +
   23.35 +  int pass_number;		/* # of passes completed */
   23.36 +  int total_passes;		/* total # of passes needed */
   23.37 +
   23.38 +  int scan_number;		/* current index in scan_info[] */
   23.39 +} my_comp_master;
   23.40 +
   23.41 +typedef my_comp_master * my_master_ptr;
   23.42 +
   23.43 +
   23.44 +/*
   23.45 + * Support routines that do various essential calculations.
   23.46 + */
   23.47 +
   23.48 +LOCAL(void)
   23.49 +initial_setup (j_compress_ptr cinfo)
   23.50 +/* Do computations that are needed before master selection phase */
   23.51 +{
   23.52 +  int ci;
   23.53 +  jpeg_component_info *compptr;
   23.54 +  long samplesperrow;
   23.55 +  JDIMENSION jd_samplesperrow;
   23.56 +
   23.57 +  /* Sanity check on image dimensions */
   23.58 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
   23.59 +      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
   23.60 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
   23.61 +
   23.62 +  /* Make sure image isn't bigger than I can handle */
   23.63 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   23.64 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   23.65 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   23.66 +
   23.67 +  /* Width of an input scanline must be representable as JDIMENSION. */
   23.68 +  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
   23.69 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
   23.70 +  if ((long) jd_samplesperrow != samplesperrow)
   23.71 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   23.72 +
   23.73 +  /* For now, precision must match compiled-in value... */
   23.74 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
   23.75 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
   23.76 +
   23.77 +  /* Check that number of components won't exceed internal array sizes */
   23.78 +  if (cinfo->num_components > MAX_COMPONENTS)
   23.79 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   23.80 +	     MAX_COMPONENTS);
   23.81 +
   23.82 +  /* Compute maximum sampling factors; check factor validity */
   23.83 +  cinfo->max_h_samp_factor = 1;
   23.84 +  cinfo->max_v_samp_factor = 1;
   23.85 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   23.86 +       ci++, compptr++) {
   23.87 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
   23.88 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
   23.89 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
   23.90 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
   23.91 +				   compptr->h_samp_factor);
   23.92 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   23.93 +				   compptr->v_samp_factor);
   23.94 +  }
   23.95 +
   23.96 +  /* Compute dimensions of components */
   23.97 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   23.98 +       ci++, compptr++) {
   23.99 +    /* Fill in the correct component_index value; don't rely on application */
  23.100 +    compptr->component_index = ci;
  23.101 +    /* For compression, we never do DCT scaling. */
  23.102 +    compptr->DCT_scaled_size = DCTSIZE;
  23.103 +    /* Size in DCT blocks */
  23.104 +    compptr->width_in_blocks = (JDIMENSION)
  23.105 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  23.106 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
  23.107 +    compptr->height_in_blocks = (JDIMENSION)
  23.108 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  23.109 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  23.110 +    /* Size in samples */
  23.111 +    compptr->downsampled_width = (JDIMENSION)
  23.112 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  23.113 +		    (long) cinfo->max_h_samp_factor);
  23.114 +    compptr->downsampled_height = (JDIMENSION)
  23.115 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  23.116 +		    (long) cinfo->max_v_samp_factor);
  23.117 +    /* Mark component needed (this flag isn't actually used for compression) */
  23.118 +    compptr->component_needed = TRUE;
  23.119 +  }
  23.120 +
  23.121 +  /* Compute number of fully interleaved MCU rows (number of times that
  23.122 +   * main controller will call coefficient controller).
  23.123 +   */
  23.124 +  cinfo->total_iMCU_rows = (JDIMENSION)
  23.125 +    jdiv_round_up((long) cinfo->image_height,
  23.126 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
  23.127 +}
  23.128 +
  23.129 +
  23.130 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  23.131 +
  23.132 +LOCAL(void)
  23.133 +validate_script (j_compress_ptr cinfo)
  23.134 +/* Verify that the scan script in cinfo->scan_info[] is valid; also
  23.135 + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  23.136 + */
  23.137 +{
  23.138 +  const jpeg_scan_info * scanptr;
  23.139 +  int scanno, ncomps, ci, coefi, thisi;
  23.140 +  int Ss, Se, Ah, Al;
  23.141 +  boolean component_sent[MAX_COMPONENTS];
  23.142 +#ifdef C_PROGRESSIVE_SUPPORTED
  23.143 +  int * last_bitpos_ptr;
  23.144 +  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  23.145 +  /* -1 until that coefficient has been seen; then last Al for it */
  23.146 +#endif
  23.147 +
  23.148 +  if (cinfo->num_scans <= 0)
  23.149 +    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  23.150 +
  23.151 +  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  23.152 +   * for progressive JPEG, no scan can have this.
  23.153 +   */
  23.154 +  scanptr = cinfo->scan_info;
  23.155 +  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  23.156 +#ifdef C_PROGRESSIVE_SUPPORTED
  23.157 +    cinfo->progressive_mode = TRUE;
  23.158 +    last_bitpos_ptr = & last_bitpos[0][0];
  23.159 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  23.160 +      for (coefi = 0; coefi < DCTSIZE2; coefi++)
  23.161 +	*last_bitpos_ptr++ = -1;
  23.162 +#else
  23.163 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  23.164 +#endif
  23.165 +  } else {
  23.166 +    cinfo->progressive_mode = FALSE;
  23.167 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  23.168 +      component_sent[ci] = FALSE;
  23.169 +  }
  23.170 +
  23.171 +  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  23.172 +    /* Validate component indexes */
  23.173 +    ncomps = scanptr->comps_in_scan;
  23.174 +    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  23.175 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  23.176 +    for (ci = 0; ci < ncomps; ci++) {
  23.177 +      thisi = scanptr->component_index[ci];
  23.178 +      if (thisi < 0 || thisi >= cinfo->num_components)
  23.179 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  23.180 +      /* Components must appear in SOF order within each scan */
  23.181 +      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  23.182 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  23.183 +    }
  23.184 +    /* Validate progression parameters */
  23.185 +    Ss = scanptr->Ss;
  23.186 +    Se = scanptr->Se;
  23.187 +    Ah = scanptr->Ah;
  23.188 +    Al = scanptr->Al;
  23.189 +    if (cinfo->progressive_mode) {
  23.190 +#ifdef C_PROGRESSIVE_SUPPORTED
  23.191 +      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  23.192 +       * seems wrong: the upper bound ought to depend on data precision.
  23.193 +       * Perhaps they really meant 0..N+1 for N-bit precision.
  23.194 +       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  23.195 +       * out-of-range reconstructed DC values during the first DC scan,
  23.196 +       * which might cause problems for some decoders.
  23.197 +       */
  23.198 +#if BITS_IN_JSAMPLE == 8
  23.199 +#define MAX_AH_AL 10
  23.200 +#else
  23.201 +#define MAX_AH_AL 13
  23.202 +#endif
  23.203 +      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  23.204 +	  Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
  23.205 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.206 +      if (Ss == 0) {
  23.207 +	if (Se != 0)		/* DC and AC together not OK */
  23.208 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.209 +      } else {
  23.210 +	if (ncomps != 1)	/* AC scans must be for only one component */
  23.211 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.212 +      }
  23.213 +      for (ci = 0; ci < ncomps; ci++) {
  23.214 +	last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  23.215 +	if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  23.216 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.217 +	for (coefi = Ss; coefi <= Se; coefi++) {
  23.218 +	  if (last_bitpos_ptr[coefi] < 0) {
  23.219 +	    /* first scan of this coefficient */
  23.220 +	    if (Ah != 0)
  23.221 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.222 +	  } else {
  23.223 +	    /* not first scan */
  23.224 +	    if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  23.225 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.226 +	  }
  23.227 +	  last_bitpos_ptr[coefi] = Al;
  23.228 +	}
  23.229 +      }
  23.230 +#endif
  23.231 +    } else {
  23.232 +      /* For sequential JPEG, all progression parameters must be these: */
  23.233 +      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  23.234 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  23.235 +      /* Make sure components are not sent twice */
  23.236 +      for (ci = 0; ci < ncomps; ci++) {
  23.237 +	thisi = scanptr->component_index[ci];
  23.238 +	if (component_sent[thisi])
  23.239 +	  ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  23.240 +	component_sent[thisi] = TRUE;
  23.241 +      }
  23.242 +    }
  23.243 +  }
  23.244 +
  23.245 +  /* Now verify that everything got sent. */
  23.246 +  if (cinfo->progressive_mode) {
  23.247 +#ifdef C_PROGRESSIVE_SUPPORTED
  23.248 +    /* For progressive mode, we only check that at least some DC data
  23.249 +     * got sent for each component; the spec does not require that all bits
  23.250 +     * of all coefficients be transmitted.  Would it be wiser to enforce
  23.251 +     * transmission of all coefficient bits??
  23.252 +     */
  23.253 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  23.254 +      if (last_bitpos[ci][0] < 0)
  23.255 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  23.256 +    }
  23.257 +#endif
  23.258 +  } else {
  23.259 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  23.260 +      if (! component_sent[ci])
  23.261 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  23.262 +    }
  23.263 +  }
  23.264 +}
  23.265 +
  23.266 +#endif /* C_MULTISCAN_FILES_SUPPORTED */
  23.267 +
  23.268 +
  23.269 +LOCAL(void)
  23.270 +select_scan_parameters (j_compress_ptr cinfo)
  23.271 +/* Set up the scan parameters for the current scan */
  23.272 +{
  23.273 +  int ci;
  23.274 +
  23.275 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  23.276 +  if (cinfo->scan_info != NULL) {
  23.277 +    /* Prepare for current scan --- the script is already validated */
  23.278 +    my_master_ptr master = (my_master_ptr) cinfo->master;
  23.279 +    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  23.280 +
  23.281 +    cinfo->comps_in_scan = scanptr->comps_in_scan;
  23.282 +    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  23.283 +      cinfo->cur_comp_info[ci] =
  23.284 +	&cinfo->comp_info[scanptr->component_index[ci]];
  23.285 +    }
  23.286 +    cinfo->Ss = scanptr->Ss;
  23.287 +    cinfo->Se = scanptr->Se;
  23.288 +    cinfo->Ah = scanptr->Ah;
  23.289 +    cinfo->Al = scanptr->Al;
  23.290 +  }
  23.291 +  else
  23.292 +#endif
  23.293 +  {
  23.294 +    /* Prepare for single sequential-JPEG scan containing all components */
  23.295 +    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  23.296 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  23.297 +	       MAX_COMPS_IN_SCAN);
  23.298 +    cinfo->comps_in_scan = cinfo->num_components;
  23.299 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  23.300 +      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  23.301 +    }
  23.302 +    cinfo->Ss = 0;
  23.303 +    cinfo->Se = DCTSIZE2-1;
  23.304 +    cinfo->Ah = 0;
  23.305 +    cinfo->Al = 0;
  23.306 +  }
  23.307 +}
  23.308 +
  23.309 +
  23.310 +LOCAL(void)
  23.311 +per_scan_setup (j_compress_ptr cinfo)
  23.312 +/* Do computations that are needed before processing a JPEG scan */
  23.313 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  23.314 +{
  23.315 +  int ci, mcublks, tmp;
  23.316 +  jpeg_component_info *compptr;
  23.317 +  
  23.318 +  if (cinfo->comps_in_scan == 1) {
  23.319 +    
  23.320 +    /* Noninterleaved (single-component) scan */
  23.321 +    compptr = cinfo->cur_comp_info[0];
  23.322 +    
  23.323 +    /* Overall image size in MCUs */
  23.324 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
  23.325 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  23.326 +    
  23.327 +    /* For noninterleaved scan, always one block per MCU */
  23.328 +    compptr->MCU_width = 1;
  23.329 +    compptr->MCU_height = 1;
  23.330 +    compptr->MCU_blocks = 1;
  23.331 +    compptr->MCU_sample_width = DCTSIZE;
  23.332 +    compptr->last_col_width = 1;
  23.333 +    /* For noninterleaved scans, it is convenient to define last_row_height
  23.334 +     * as the number of block rows present in the last iMCU row.
  23.335 +     */
  23.336 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  23.337 +    if (tmp == 0) tmp = compptr->v_samp_factor;
  23.338 +    compptr->last_row_height = tmp;
  23.339 +    
  23.340 +    /* Prepare array describing MCU composition */
  23.341 +    cinfo->blocks_in_MCU = 1;
  23.342 +    cinfo->MCU_membership[0] = 0;
  23.343 +    
  23.344 +  } else {
  23.345 +    
  23.346 +    /* Interleaved (multi-component) scan */
  23.347 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  23.348 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  23.349 +	       MAX_COMPS_IN_SCAN);
  23.350 +    
  23.351 +    /* Overall image size in MCUs */
  23.352 +    cinfo->MCUs_per_row = (JDIMENSION)
  23.353 +      jdiv_round_up((long) cinfo->image_width,
  23.354 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
  23.355 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
  23.356 +      jdiv_round_up((long) cinfo->image_height,
  23.357 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
  23.358 +    
  23.359 +    cinfo->blocks_in_MCU = 0;
  23.360 +    
  23.361 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  23.362 +      compptr = cinfo->cur_comp_info[ci];
  23.363 +      /* Sampling factors give # of blocks of component in each MCU */
  23.364 +      compptr->MCU_width = compptr->h_samp_factor;
  23.365 +      compptr->MCU_height = compptr->v_samp_factor;
  23.366 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  23.367 +      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  23.368 +      /* Figure number of non-dummy blocks in last MCU column & row */
  23.369 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  23.370 +      if (tmp == 0) tmp = compptr->MCU_width;
  23.371 +      compptr->last_col_width = tmp;
  23.372 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  23.373 +      if (tmp == 0) tmp = compptr->MCU_height;
  23.374 +      compptr->last_row_height = tmp;
  23.375 +      /* Prepare array describing MCU composition */
  23.376 +      mcublks = compptr->MCU_blocks;
  23.377 +      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  23.378 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  23.379 +      while (mcublks-- > 0) {
  23.380 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  23.381 +      }
  23.382 +    }
  23.383 +    
  23.384 +  }
  23.385 +
  23.386 +  /* Convert restart specified in rows to actual MCU count. */
  23.387 +  /* Note that count must fit in 16 bits, so we provide limiting. */
  23.388 +  if (cinfo->restart_in_rows > 0) {
  23.389 +    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  23.390 +    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  23.391 +  }
  23.392 +}
  23.393 +
  23.394 +
  23.395 +/*
  23.396 + * Per-pass setup.
  23.397 + * This is called at the beginning of each pass.  We determine which modules
  23.398 + * will be active during this pass and give them appropriate start_pass calls.
  23.399 + * We also set is_last_pass to indicate whether any more passes will be
  23.400 + * required.
  23.401 + */
  23.402 +
  23.403 +METHODDEF(void)
  23.404 +prepare_for_pass (j_compress_ptr cinfo)
  23.405 +{
  23.406 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  23.407 +
  23.408 +  switch (master->pass_type) {
  23.409 +  case main_pass:
  23.410 +    /* Initial pass: will collect input data, and do either Huffman
  23.411 +     * optimization or data output for the first scan.
  23.412 +     */
  23.413 +    select_scan_parameters(cinfo);
  23.414 +    per_scan_setup(cinfo);
  23.415 +    if (! cinfo->raw_data_in) {
  23.416 +      (*cinfo->cconvert->start_pass) (cinfo);
  23.417 +      (*cinfo->downsample->start_pass) (cinfo);
  23.418 +      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  23.419 +    }
  23.420 +    (*cinfo->fdct->start_pass) (cinfo);
  23.421 +    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  23.422 +    (*cinfo->coef->start_pass) (cinfo,
  23.423 +				(master->total_passes > 1 ?
  23.424 +				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  23.425 +    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  23.426 +    if (cinfo->optimize_coding) {
  23.427 +      /* No immediate data output; postpone writing frame/scan headers */
  23.428 +      master->pub.call_pass_startup = FALSE;
  23.429 +    } else {
  23.430 +      /* Will write frame/scan headers at first jpeg_write_scanlines call */
  23.431 +      master->pub.call_pass_startup = TRUE;
  23.432 +    }
  23.433 +    break;
  23.434 +#ifdef ENTROPY_OPT_SUPPORTED
  23.435 +  case huff_opt_pass:
  23.436 +    /* Do Huffman optimization for a scan after the first one. */
  23.437 +    select_scan_parameters(cinfo);
  23.438 +    per_scan_setup(cinfo);
  23.439 +    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  23.440 +      (*cinfo->entropy->start_pass) (cinfo, TRUE);
  23.441 +      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  23.442 +      master->pub.call_pass_startup = FALSE;
  23.443 +      break;
  23.444 +    }
  23.445 +    /* Special case: Huffman DC refinement scans need no Huffman table
  23.446 +     * and therefore we can skip the optimization pass for them.
  23.447 +     */
  23.448 +    master->pass_type = output_pass;
  23.449 +    master->pass_number++;
  23.450 +    /*FALLTHROUGH*/
  23.451 +#endif
  23.452 +  case output_pass:
  23.453 +    /* Do a data-output pass. */
  23.454 +    /* We need not repeat per-scan setup if prior optimization pass did it. */
  23.455 +    if (! cinfo->optimize_coding) {
  23.456 +      select_scan_parameters(cinfo);
  23.457 +      per_scan_setup(cinfo);
  23.458 +    }
  23.459 +    (*cinfo->entropy->start_pass) (cinfo, FALSE);
  23.460 +    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  23.461 +    /* We emit frame/scan headers now */
  23.462 +    if (master->scan_number == 0)
  23.463 +      (*cinfo->marker->write_frame_header) (cinfo);
  23.464 +    (*cinfo->marker->write_scan_header) (cinfo);
  23.465 +    master->pub.call_pass_startup = FALSE;
  23.466 +    break;
  23.467 +  default:
  23.468 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  23.469 +  }
  23.470 +
  23.471 +  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  23.472 +
  23.473 +  /* Set up progress monitor's pass info if present */
  23.474 +  if (cinfo->progress != NULL) {
  23.475 +    cinfo->progress->completed_passes = master->pass_number;
  23.476 +    cinfo->progress->total_passes = master->total_passes;
  23.477 +  }
  23.478 +}
  23.479 +
  23.480 +
  23.481 +/*
  23.482 + * Special start-of-pass hook.
  23.483 + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  23.484 + * In single-pass processing, we need this hook because we don't want to
  23.485 + * write frame/scan headers during jpeg_start_compress; we want to let the
  23.486 + * application write COM markers etc. between jpeg_start_compress and the
  23.487 + * jpeg_write_scanlines loop.
  23.488 + * In multi-pass processing, this routine is not used.
  23.489 + */
  23.490 +
  23.491 +METHODDEF(void)
  23.492 +pass_startup (j_compress_ptr cinfo)
  23.493 +{
  23.494 +  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  23.495 +
  23.496 +  (*cinfo->marker->write_frame_header) (cinfo);
  23.497 +  (*cinfo->marker->write_scan_header) (cinfo);
  23.498 +}
  23.499 +
  23.500 +
  23.501 +/*
  23.502 + * Finish up at end of pass.
  23.503 + */
  23.504 +
  23.505 +METHODDEF(void)
  23.506 +finish_pass_master (j_compress_ptr cinfo)
  23.507 +{
  23.508 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  23.509 +
  23.510 +  /* The entropy coder always needs an end-of-pass call,
  23.511 +   * either to analyze statistics or to flush its output buffer.
  23.512 +   */
  23.513 +  (*cinfo->entropy->finish_pass) (cinfo);
  23.514 +
  23.515 +  /* Update state for next pass */
  23.516 +  switch (master->pass_type) {
  23.517 +  case main_pass:
  23.518 +    /* next pass is either output of scan 0 (after optimization)
  23.519 +     * or output of scan 1 (if no optimization).
  23.520 +     */
  23.521 +    master->pass_type = output_pass;
  23.522 +    if (! cinfo->optimize_coding)
  23.523 +      master->scan_number++;
  23.524 +    break;
  23.525 +  case huff_opt_pass:
  23.526 +    /* next pass is always output of current scan */
  23.527 +    master->pass_type = output_pass;
  23.528 +    break;
  23.529 +  case output_pass:
  23.530 +    /* next pass is either optimization or output of next scan */
  23.531 +    if (cinfo->optimize_coding)
  23.532 +      master->pass_type = huff_opt_pass;
  23.533 +    master->scan_number++;
  23.534 +    break;
  23.535 +  }
  23.536 +
  23.537 +  master->pass_number++;
  23.538 +}
  23.539 +
  23.540 +
  23.541 +/*
  23.542 + * Initialize master compression control.
  23.543 + */
  23.544 +
  23.545 +GLOBAL(void)
  23.546 +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  23.547 +{
  23.548 +  my_master_ptr master;
  23.549 +
  23.550 +  master = (my_master_ptr)
  23.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  23.552 +				  SIZEOF(my_comp_master));
  23.553 +  cinfo->master = (struct jpeg_comp_master *) master;
  23.554 +  master->pub.prepare_for_pass = prepare_for_pass;
  23.555 +  master->pub.pass_startup = pass_startup;
  23.556 +  master->pub.finish_pass = finish_pass_master;
  23.557 +  master->pub.is_last_pass = FALSE;
  23.558 +
  23.559 +  /* Validate parameters, determine derived values */
  23.560 +  initial_setup(cinfo);
  23.561 +
  23.562 +  if (cinfo->scan_info != NULL) {
  23.563 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  23.564 +    validate_script(cinfo);
  23.565 +#else
  23.566 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  23.567 +#endif
  23.568 +  } else {
  23.569 +    cinfo->progressive_mode = FALSE;
  23.570 +    cinfo->num_scans = 1;
  23.571 +  }
  23.572 +
  23.573 +  if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
  23.574 +    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  23.575 +
  23.576 +  /* Initialize my private state */
  23.577 +  if (transcode_only) {
  23.578 +    /* no main pass in transcoding */
  23.579 +    if (cinfo->optimize_coding)
  23.580 +      master->pass_type = huff_opt_pass;
  23.581 +    else
  23.582 +      master->pass_type = output_pass;
  23.583 +  } else {
  23.584 +    /* for normal compression, first pass is always this type: */
  23.585 +    master->pass_type = main_pass;
  23.586 +  }
  23.587 +  master->scan_number = 0;
  23.588 +  master->pass_number = 0;
  23.589 +  if (cinfo->optimize_coding)
  23.590 +    master->total_passes = cinfo->num_scans * 2;
  23.591 +  else
  23.592 +    master->total_passes = cinfo->num_scans;
  23.593 +}
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/libs/libjpeg/jcomapi.c	Thu Sep 08 06:28:38 2011 +0300
    24.3 @@ -0,0 +1,106 @@
    24.4 +/*
    24.5 + * jcomapi.c
    24.6 + *
    24.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    24.8 + * This file is part of the Independent JPEG Group's software.
    24.9 + * For conditions of distribution and use, see the accompanying README file.
   24.10 + *
   24.11 + * This file contains application interface routines that are used for both
   24.12 + * compression and decompression.
   24.13 + */
   24.14 +
   24.15 +#define JPEG_INTERNALS
   24.16 +#include "jinclude.h"
   24.17 +#include "jpeglib.h"
   24.18 +
   24.19 +
   24.20 +/*
   24.21 + * Abort processing of a JPEG compression or decompression operation,
   24.22 + * but don't destroy the object itself.
   24.23 + *
   24.24 + * For this, we merely clean up all the nonpermanent memory pools.
   24.25 + * Note that temp files (virtual arrays) are not allowed to belong to
   24.26 + * the permanent pool, so we will be able to close all temp files here.
   24.27 + * Closing a data source or destination, if necessary, is the application's
   24.28 + * responsibility.
   24.29 + */
   24.30 +
   24.31 +GLOBAL(void)
   24.32 +jpeg_abort (j_common_ptr cinfo)
   24.33 +{
   24.34 +  int pool;
   24.35 +
   24.36 +  /* Do nothing if called on a not-initialized or destroyed JPEG object. */
   24.37 +  if (cinfo->mem == NULL)
   24.38 +    return;
   24.39 +
   24.40 +  /* Releasing pools in reverse order might help avoid fragmentation
   24.41 +   * with some (brain-damaged) malloc libraries.
   24.42 +   */
   24.43 +  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
   24.44 +    (*cinfo->mem->free_pool) (cinfo, pool);
   24.45 +  }
   24.46 +
   24.47 +  /* Reset overall state for possible reuse of object */
   24.48 +  if (cinfo->is_decompressor) {
   24.49 +    cinfo->global_state = DSTATE_START;
   24.50 +    /* Try to keep application from accessing now-deleted marker list.
   24.51 +     * A bit kludgy to do it here, but this is the most central place.
   24.52 +     */
   24.53 +    ((j_decompress_ptr) cinfo)->marker_list = NULL;
   24.54 +  } else {
   24.55 +    cinfo->global_state = CSTATE_START;
   24.56 +  }
   24.57 +}
   24.58 +
   24.59 +
   24.60 +/*
   24.61 + * Destruction of a JPEG object.
   24.62 + *
   24.63 + * Everything gets deallocated except the master jpeg_compress_struct itself
   24.64 + * and the error manager struct.  Both of these are supplied by the application
   24.65 + * and must be freed, if necessary, by the application.  (Often they are on
   24.66 + * the stack and so don't need to be freed anyway.)
   24.67 + * Closing a data source or destination, if necessary, is the application's
   24.68 + * responsibility.
   24.69 + */
   24.70 +
   24.71 +GLOBAL(void)
   24.72 +jpeg_destroy (j_common_ptr cinfo)
   24.73 +{
   24.74 +  /* We need only tell the memory manager to release everything. */
   24.75 +  /* NB: mem pointer is NULL if memory mgr failed to initialize. */
   24.76 +  if (cinfo->mem != NULL)
   24.77 +    (*cinfo->mem->self_destruct) (cinfo);
   24.78 +  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
   24.79 +  cinfo->global_state = 0;	/* mark it destroyed */
   24.80 +}
   24.81 +
   24.82 +
   24.83 +/*
   24.84 + * Convenience routines for allocating quantization and Huffman tables.
   24.85 + * (Would jutils.c be a more reasonable place to put these?)
   24.86 + */
   24.87 +
   24.88 +GLOBAL(JQUANT_TBL *)
   24.89 +jpeg_alloc_quant_table (j_common_ptr cinfo)
   24.90 +{
   24.91 +  JQUANT_TBL *tbl;
   24.92 +
   24.93 +  tbl = (JQUANT_TBL *)
   24.94 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
   24.95 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
   24.96 +  return tbl;
   24.97 +}
   24.98 +
   24.99 +
  24.100 +GLOBAL(JHUFF_TBL *)
  24.101 +jpeg_alloc_huff_table (j_common_ptr cinfo)
  24.102 +{
  24.103 +  JHUFF_TBL *tbl;
  24.104 +
  24.105 +  tbl = (JHUFF_TBL *)
  24.106 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
  24.107 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
  24.108 +  return tbl;
  24.109 +}
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/libs/libjpeg/jconfig.h	Thu Sep 08 06:28:38 2011 +0300
    25.3 @@ -0,0 +1,45 @@
    25.4 +/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
    25.5 +/* see jconfig.doc for explanations */
    25.6 +
    25.7 +#define HAVE_PROTOTYPES
    25.8 +#define HAVE_UNSIGNED_CHAR
    25.9 +#define HAVE_UNSIGNED_SHORT
   25.10 +/* #define void char */
   25.11 +/* #define const */
   25.12 +#undef CHAR_IS_UNSIGNED
   25.13 +#define HAVE_STDDEF_H
   25.14 +#define HAVE_STDLIB_H
   25.15 +#undef NEED_BSD_STRINGS
   25.16 +#undef NEED_SYS_TYPES_H
   25.17 +#undef NEED_FAR_POINTERS	/* we presume a 32-bit flat memory model */
   25.18 +#undef NEED_SHORT_EXTERNAL_NAMES
   25.19 +#undef INCOMPLETE_TYPES_BROKEN
   25.20 +
   25.21 +/* Define "boolean" as unsigned char, not int, per Windows custom */
   25.22 +#ifndef __RPCNDR_H__		/* don't conflict if rpcndr.h already read */
   25.23 +typedef unsigned char boolean;
   25.24 +#endif
   25.25 +#define HAVE_BOOLEAN		/* prevent jmorecfg.h from redefining it */
   25.26 +
   25.27 +
   25.28 +#ifdef JPEG_INTERNALS
   25.29 +
   25.30 +#undef RIGHT_SHIFT_IS_UNSIGNED
   25.31 +
   25.32 +#endif /* JPEG_INTERNALS */
   25.33 +
   25.34 +#ifdef JPEG_CJPEG_DJPEG
   25.35 +
   25.36 +#define BMP_SUPPORTED		/* BMP image file format */
   25.37 +#define GIF_SUPPORTED		/* GIF image file format */
   25.38 +#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
   25.39 +#undef RLE_SUPPORTED		/* Utah RLE image file format */
   25.40 +#define TARGA_SUPPORTED		/* Targa image file format */
   25.41 +
   25.42 +#define TWO_FILE_COMMANDLINE	/* optional */
   25.43 +#define USE_SETMODE		/* Microsoft has setmode() */
   25.44 +#undef NEED_SIGNAL_CATCHER
   25.45 +#undef DONT_USE_B_MODE
   25.46 +#undef PROGRESS_REPORT		/* optional */
   25.47 +
   25.48 +#endif /* JPEG_CJPEG_DJPEG */
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/libs/libjpeg/jcparam.c	Thu Sep 08 06:28:38 2011 +0300
    26.3 @@ -0,0 +1,610 @@
    26.4 +/*
    26.5 + * jcparam.c
    26.6 + *
    26.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    26.8 + * This file is part of the Independent JPEG Group's software.
    26.9 + * For conditions of distribution and use, see the accompanying README file.
   26.10 + *
   26.11 + * This file contains optional default-setting code for the JPEG compressor.
   26.12 + * Applications do not have to use this file, but those that don't use it
   26.13 + * must know a lot more about the innards of the JPEG code.
   26.14 + */
   26.15 +
   26.16 +#define JPEG_INTERNALS
   26.17 +#include "jinclude.h"
   26.18 +#include "jpeglib.h"
   26.19 +
   26.20 +
   26.21 +/*
   26.22 + * Quantization table setup routines
   26.23 + */
   26.24 +
   26.25 +GLOBAL(void)
   26.26 +jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
   26.27 +		      const unsigned int *basic_table,
   26.28 +		      int scale_factor, boolean force_baseline)
   26.29 +/* Define a quantization table equal to the basic_table times
   26.30 + * a scale factor (given as a percentage).
   26.31 + * If force_baseline is TRUE, the computed quantization table entries
   26.32 + * are limited to 1..255 for JPEG baseline compatibility.
   26.33 + */
   26.34 +{
   26.35 +  JQUANT_TBL ** qtblptr;
   26.36 +  int i;
   26.37 +  long temp;
   26.38 +
   26.39 +  /* Safety check to ensure start_compress not called yet. */
   26.40 +  if (cinfo->global_state != CSTATE_START)
   26.41 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   26.42 +
   26.43 +  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
   26.44 +    ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
   26.45 +
   26.46 +  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
   26.47 +
   26.48 +  if (*qtblptr == NULL)
   26.49 +    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   26.50 +
   26.51 +  for (i = 0; i < DCTSIZE2; i++) {
   26.52 +    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
   26.53 +    /* limit the values to the valid range */
   26.54 +    if (temp <= 0L) temp = 1L;
   26.55 +    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
   26.56 +    if (force_baseline && temp > 255L)
   26.57 +      temp = 255L;		/* limit to baseline range if requested */
   26.58 +    (*qtblptr)->quantval[i] = (UINT16) temp;
   26.59 +  }
   26.60 +
   26.61 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
   26.62 +  (*qtblptr)->sent_table = FALSE;
   26.63 +}
   26.64 +
   26.65 +
   26.66 +GLOBAL(void)
   26.67 +jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
   26.68 +			 boolean force_baseline)
   26.69 +/* Set or change the 'quality' (quantization) setting, using default tables
   26.70 + * and a straight percentage-scaling quality scale.  In most cases it's better
   26.71 + * to use jpeg_set_quality (below); this entry point is provided for
   26.72 + * applications that insist on a linear percentage scaling.
   26.73 + */
   26.74 +{
   26.75 +  /* These are the sample quantization tables given in JPEG spec section K.1.
   26.76 +   * The spec says that the values given produce "good" quality, and
   26.77 +   * when divided by 2, "very good" quality.
   26.78 +   */
   26.79 +  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
   26.80 +    16,  11,  10,  16,  24,  40,  51,  61,
   26.81 +    12,  12,  14,  19,  26,  58,  60,  55,
   26.82 +    14,  13,  16,  24,  40,  57,  69,  56,
   26.83 +    14,  17,  22,  29,  51,  87,  80,  62,
   26.84 +    18,  22,  37,  56,  68, 109, 103,  77,
   26.85 +    24,  35,  55,  64,  81, 104, 113,  92,
   26.86 +    49,  64,  78,  87, 103, 121, 120, 101,
   26.87 +    72,  92,  95,  98, 112, 100, 103,  99
   26.88 +  };
   26.89 +  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
   26.90 +    17,  18,  24,  47,  99,  99,  99,  99,
   26.91 +    18,  21,  26,  66,  99,  99,  99,  99,
   26.92 +    24,  26,  56,  99,  99,  99,  99,  99,
   26.93 +    47,  66,  99,  99,  99,  99,  99,  99,
   26.94 +    99,  99,  99,  99,  99,  99,  99,  99,
   26.95 +    99,  99,  99,  99,  99,  99,  99,  99,
   26.96 +    99,  99,  99,  99,  99,  99,  99,  99,
   26.97 +    99,  99,  99,  99,  99,  99,  99,  99
   26.98 +  };
   26.99 +
  26.100 +  /* Set up two quantization tables using the specified scaling */
  26.101 +  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  26.102 +		       scale_factor, force_baseline);
  26.103 +  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  26.104 +		       scale_factor, force_baseline);
  26.105 +}
  26.106 +
  26.107 +
  26.108 +GLOBAL(int)
  26.109 +jpeg_quality_scaling (int quality)
  26.110 +/* Convert a user-specified quality rating to a percentage scaling factor
  26.111 + * for an underlying quantization table, using our recommended scaling curve.
  26.112 + * The input 'quality' factor should be 0 (terrible) to 100 (very good).
  26.113 + */
  26.114 +{
  26.115 +  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
  26.116 +  if (quality <= 0) quality = 1;
  26.117 +  if (quality > 100) quality = 100;
  26.118 +
  26.119 +  /* The basic table is used as-is (scaling 100) for a quality of 50.
  26.120 +   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  26.121 +   * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  26.122 +   * to make all the table entries 1 (hence, minimum quantization loss).
  26.123 +   * Qualities 1..50 are converted to scaling percentage 5000/Q.
  26.124 +   */
  26.125 +  if (quality < 50)
  26.126 +    quality = 5000 / quality;
  26.127 +  else
  26.128 +    quality = 200 - quality*2;
  26.129 +
  26.130 +  return quality;
  26.131 +}
  26.132 +
  26.133 +
  26.134 +GLOBAL(void)
  26.135 +jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
  26.136 +/* Set or change the 'quality' (quantization) setting, using default tables.
  26.137 + * This is the standard quality-adjusting entry point for typical user
  26.138 + * interfaces; only those who want detailed control over quantization tables
  26.139 + * would use the preceding three routines directly.
  26.140 + */
  26.141 +{
  26.142 +  /* Convert user 0-100 rating to percentage scaling */
  26.143 +  quality = jpeg_quality_scaling(quality);
  26.144 +
  26.145 +  /* Set up standard quality tables */
  26.146 +  jpeg_set_linear_quality(cinfo, quality, force_baseline);
  26.147 +}
  26.148 +
  26.149 +
  26.150 +/*
  26.151 + * Huffman table setup routines
  26.152 + */
  26.153 +
  26.154 +LOCAL(void)
  26.155 +add_huff_table (j_compress_ptr cinfo,
  26.156 +		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
  26.157 +/* Define a Huffman table */
  26.158 +{
  26.159 +  int nsymbols, len;
  26.160 +
  26.161 +  if (*htblptr == NULL)
  26.162 +    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  26.163 +
  26.164 +  /* Copy the number-of-symbols-of-each-code-length counts */
  26.165 +  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  26.166 +
  26.167 +  /* Validate the counts.  We do this here mainly so we can copy the right
  26.168 +   * number of symbols from the val[] array, without risking marching off
  26.169 +   * the end of memory.  jchuff.c will do a more thorough test later.
  26.170 +   */
  26.171 +  nsymbols = 0;
  26.172 +  for (len = 1; len <= 16; len++)
  26.173 +    nsymbols += bits[len];
  26.174 +  if (nsymbols < 1 || nsymbols > 256)
  26.175 +    ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  26.176 +
  26.177 +  MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
  26.178 +
  26.179 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
  26.180 +  (*htblptr)->sent_table = FALSE;
  26.181 +}
  26.182 +
  26.183 +
  26.184 +LOCAL(void)
  26.185 +std_huff_tables (j_compress_ptr cinfo)
  26.186 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  26.187 +/* IMPORTANT: these are only valid for 8-bit data precision! */
  26.188 +{
  26.189 +  static const UINT8 bits_dc_luminance[17] =
  26.190 +    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  26.191 +  static const UINT8 val_dc_luminance[] =
  26.192 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  26.193 +  
  26.194 +  static const UINT8 bits_dc_chrominance[17] =
  26.195 +    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  26.196 +  static const UINT8 val_dc_chrominance[] =
  26.197 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  26.198 +  
  26.199 +  static const UINT8 bits_ac_luminance[17] =
  26.200 +    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  26.201 +  static const UINT8 val_ac_luminance[] =
  26.202 +    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  26.203 +      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  26.204 +      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  26.205 +      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  26.206 +      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  26.207 +      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  26.208 +      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  26.209 +      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  26.210 +      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  26.211 +      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  26.212 +      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  26.213 +      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  26.214 +      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  26.215 +      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  26.216 +      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  26.217 +      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  26.218 +      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  26.219 +      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  26.220 +      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  26.221 +      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  26.222 +      0xf9, 0xfa };
  26.223 +  
  26.224 +  static const UINT8 bits_ac_chrominance[17] =
  26.225 +    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  26.226 +  static const UINT8 val_ac_chrominance[] =
  26.227 +    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  26.228 +      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  26.229 +      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  26.230 +      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  26.231 +      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  26.232 +      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  26.233 +      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  26.234 +      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  26.235 +      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  26.236 +      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  26.237 +      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  26.238 +      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  26.239 +      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  26.240 +      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  26.241 +      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  26.242 +      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  26.243 +      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  26.244 +      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  26.245 +      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  26.246 +      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  26.247 +      0xf9, 0xfa };
  26.248 +  
  26.249 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
  26.250 +		 bits_dc_luminance, val_dc_luminance);
  26.251 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
  26.252 +		 bits_ac_luminance, val_ac_luminance);
  26.253 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
  26.254 +		 bits_dc_chrominance, val_dc_chrominance);
  26.255 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
  26.256 +		 bits_ac_chrominance, val_ac_chrominance);
  26.257 +}
  26.258 +
  26.259 +
  26.260 +/*
  26.261 + * Default parameter setup for compression.
  26.262 + *
  26.263 + * Applications that don't choose to use this routine must do their
  26.264 + * own setup of all these parameters.  Alternately, you can call this
  26.265 + * to establish defaults and then alter parameters selectively.  This
  26.266 + * is the recommended approach since, if we add any new parameters,
  26.267 + * your code will still work (they'll be set to reasonable defaults).
  26.268 + */
  26.269 +
  26.270 +GLOBAL(void)
  26.271 +jpeg_set_defaults (j_compress_ptr cinfo)
  26.272 +{
  26.273 +  int i;
  26.274 +
  26.275 +  /* Safety check to ensure start_compress not called yet. */
  26.276 +  if (cinfo->global_state != CSTATE_START)
  26.277 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  26.278 +
  26.279 +  /* Allocate comp_info array large enough for maximum component count.
  26.280 +   * Array is made permanent in case application wants to compress
  26.281 +   * multiple images at same param settings.
  26.282 +   */
  26.283 +  if (cinfo->comp_info == NULL)
  26.284 +    cinfo->comp_info = (jpeg_component_info *)
  26.285 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  26.286 +				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));
  26.287 +
  26.288 +  /* Initialize everything not dependent on the color space */
  26.289 +
  26.290 +  cinfo->data_precision = BITS_IN_JSAMPLE;
  26.291 +  /* Set up two quantization tables using default quality of 75 */
  26.292 +  jpeg_set_quality(cinfo, 75, TRUE);
  26.293 +  /* Set up two Huffman tables */
  26.294 +  std_huff_tables(cinfo);
  26.295 +
  26.296 +  /* Initialize default arithmetic coding conditioning */
  26.297 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  26.298 +    cinfo->arith_dc_L[i] = 0;
  26.299 +    cinfo->arith_dc_U[i] = 1;
  26.300 +    cinfo->arith_ac_K[i] = 5;
  26.301 +  }
  26.302 +
  26.303 +  /* Default is no multiple-scan output */
  26.304 +  cinfo->scan_info = NULL;
  26.305 +  cinfo->num_scans = 0;
  26.306 +
  26.307 +  /* Expect normal source image, not raw downsampled data */
  26.308 +  cinfo->raw_data_in = FALSE;
  26.309 +
  26.310 +  /* Use Huffman coding, not arithmetic coding, by default */
  26.311 +  cinfo->arith_code = FALSE;
  26.312 +
  26.313 +  /* By default, don't do extra passes to optimize entropy coding */
  26.314 +  cinfo->optimize_coding = FALSE;
  26.315 +  /* The standard Huffman tables are only valid for 8-bit data precision.
  26.316 +   * If the precision is higher, force optimization on so that usable
  26.317 +   * tables will be computed.  This test can be removed if default tables
  26.318 +   * are supplied that are valid for the desired precision.
  26.319 +   */
  26.320 +  if (cinfo->data_precision > 8)
  26.321 +    cinfo->optimize_coding = TRUE;
  26.322 +
  26.323 +  /* By default, use the simpler non-cosited sampling alignment */
  26.324 +  cinfo->CCIR601_sampling = FALSE;
  26.325 +
  26.326 +  /* No input smoothing */
  26.327 +  cinfo->smoothing_factor = 0;
  26.328 +
  26.329 +  /* DCT algorithm preference */
  26.330 +  cinfo->dct_method = JDCT_DEFAULT;
  26.331 +
  26.332 +  /* No restart markers */
  26.333 +  cinfo->restart_interval = 0;
  26.334 +  cinfo->restart_in_rows = 0;
  26.335 +
  26.336 +  /* Fill in default JFIF marker parameters.  Note that whether the marker
  26.337 +   * will actually be written is determined by jpeg_set_colorspace.
  26.338 +   *
  26.339 +   * By default, the library emits JFIF version code 1.01.
  26.340 +   * An application that wants to emit JFIF 1.02 extension markers should set
  26.341 +   * JFIF_minor_version to 2.  We could probably get away with just defaulting
  26.342 +   * to 1.02, but there may still be some decoders in use that will complain
  26.343 +   * about that; saying 1.01 should minimize compatibility problems.
  26.344 +   */
  26.345 +  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  26.346 +  cinfo->JFIF_minor_version = 1;
  26.347 +  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  26.348 +  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  26.349 +  cinfo->Y_density = 1;
  26.350 +
  26.351 +  /* Choose JPEG colorspace based on input space, set defaults accordingly */
  26.352 +
  26.353 +  jpeg_default_colorspace(cinfo);
  26.354 +}
  26.355 +
  26.356 +
  26.357 +/*
  26.358 + * Select an appropriate JPEG colorspace for in_color_space.
  26.359 + */
  26.360 +
  26.361 +GLOBAL(void)
  26.362 +jpeg_default_colorspace (j_compress_ptr cinfo)
  26.363 +{
  26.364 +  switch (cinfo->in_color_space) {
  26.365 +  case JCS_GRAYSCALE:
  26.366 +    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  26.367 +    break;
  26.368 +  case JCS_RGB:
  26.369 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  26.370 +    break;
  26.371 +  case JCS_YCbCr:
  26.372 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  26.373 +    break;
  26.374 +  case JCS_CMYK:
  26.375 +    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
  26.376 +    break;
  26.377 +  case JCS_YCCK:
  26.378 +    jpeg_set_colorspace(cinfo, JCS_YCCK);
  26.379 +    break;
  26.380 +  case JCS_UNKNOWN:
  26.381 +    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  26.382 +    break;
  26.383 +  default:
  26.384 +    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  26.385 +  }
  26.386 +}
  26.387 +
  26.388 +
  26.389 +/*
  26.390 + * Set the JPEG colorspace, and choose colorspace-dependent default values.
  26.391 + */
  26.392 +
  26.393 +GLOBAL(void)
  26.394 +jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
  26.395 +{
  26.396 +  jpeg_component_info * compptr;
  26.397 +  int ci;
  26.398 +
  26.399 +#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
  26.400 +  (compptr = &cinfo->comp_info[index], \
  26.401 +   compptr->component_id = (id), \
  26.402 +   compptr->h_samp_factor = (hsamp), \
  26.403 +   compptr->v_samp_factor = (vsamp), \
  26.404 +   compptr->quant_tbl_no = (quant), \
  26.405 +   compptr->dc_tbl_no = (dctbl), \
  26.406 +   compptr->ac_tbl_no = (actbl) )
  26.407 +
  26.408 +  /* Safety check to ensure start_compress not called yet. */
  26.409 +  if (cinfo->global_state != CSTATE_START)
  26.410 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  26.411 +
  26.412 +  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
  26.413 +   * tables 1 for chrominance components.
  26.414 +   */
  26.415 +
  26.416 +  cinfo->jpeg_color_space = colorspace;
  26.417 +
  26.418 +  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  26.419 +  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
  26.420 +
  26.421 +  switch (colorspace) {
  26.422 +  case JCS_GRAYSCALE:
  26.423 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  26.424 +    cinfo->num_components = 1;
  26.425 +    /* JFIF specifies component ID 1 */
  26.426 +    SET_COMP(0, 1, 1,1, 0, 0,0);
  26.427 +    break;
  26.428 +  case JCS_RGB:
  26.429 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
  26.430 +    cinfo->num_components = 3;
  26.431 +    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
  26.432 +    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
  26.433 +    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
  26.434 +    break;
  26.435 +  case JCS_YCbCr:
  26.436 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  26.437 +    cinfo->num_components = 3;
  26.438 +    /* JFIF specifies component IDs 1,2,3 */
  26.439 +    /* We default to 2x2 subsamples of chrominance */
  26.440 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  26.441 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  26.442 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  26.443 +    break;
  26.444 +  case JCS_CMYK:
  26.445 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
  26.446 +    cinfo->num_components = 4;
  26.447 +    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
  26.448 +    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
  26.449 +    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
  26.450 +    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
  26.451 +    break;
  26.452 +  case JCS_YCCK:
  26.453 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
  26.454 +    cinfo->num_components = 4;
  26.455 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  26.456 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  26.457 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  26.458 +    SET_COMP(3, 4, 2,2, 0, 0,0);
  26.459 +    break;
  26.460 +  case JCS_UNKNOWN:
  26.461 +    cinfo->num_components = cinfo->input_components;
  26.462 +    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
  26.463 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  26.464 +	       MAX_COMPONENTS);
  26.465 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  26.466 +      SET_COMP(ci, ci, 1,1, 0, 0,0);
  26.467 +    }
  26.468 +    break;
  26.469 +  default:
  26.470 +    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  26.471 +  }
  26.472 +}
  26.473 +
  26.474 +
  26.475 +#ifdef C_PROGRESSIVE_SUPPORTED
  26.476 +
  26.477 +LOCAL(jpeg_scan_info *)
  26.478 +fill_a_scan (jpeg_scan_info * scanptr, int ci,
  26.479 +	     int Ss, int Se, int Ah, int Al)
  26.480 +/* Support routine: generate one scan for specified component */
  26.481 +{
  26.482 +  scanptr->comps_in_scan = 1;
  26.483 +  scanptr->component_index[0] = ci;
  26.484 +  scanptr->Ss = Ss;
  26.485 +  scanptr->Se = Se;
  26.486 +  scanptr->Ah = Ah;
  26.487 +  scanptr->Al = Al;
  26.488 +  scanptr++;
  26.489 +  return scanptr;
  26.490 +}
  26.491 +
  26.492 +LOCAL(jpeg_scan_info *)
  26.493 +fill_scans (jpeg_scan_info * scanptr, int ncomps,
  26.494 +	    int Ss, int Se, int Ah, int Al)
  26.495 +/* Support routine: generate one scan for each component */
  26.496 +{
  26.497 +  int ci;
  26.498 +
  26.499 +  for (ci = 0; ci < ncomps; ci++) {
  26.500 +    scanptr->comps_in_scan = 1;
  26.501 +    scanptr->component_index[0] = ci;
  26.502 +    scanptr->Ss = Ss;
  26.503 +    scanptr->Se = Se;
  26.504 +    scanptr->Ah = Ah;
  26.505 +    scanptr->Al = Al;
  26.506 +    scanptr++;
  26.507 +  }
  26.508 +  return scanptr;
  26.509 +}
  26.510 +
  26.511 +LOCAL(jpeg_scan_info *)
  26.512 +fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
  26.513 +/* Support routine: generate interleaved DC scan if possible, else N scans */
  26.514 +{
  26.515 +  int ci;
  26.516 +
  26.517 +  if (ncomps <= MAX_COMPS_IN_SCAN) {
  26.518 +    /* Single interleaved DC scan */
  26.519 +    scanptr->comps_in_scan = ncomps;
  26.520 +    for (ci = 0; ci < ncomps; ci++)
  26.521 +      scanptr->component_index[ci] = ci;
  26.522 +    scanptr->Ss = scanptr->Se = 0;
  26.523 +    scanptr->Ah = Ah;
  26.524 +    scanptr->Al = Al;
  26.525 +    scanptr++;
  26.526 +  } else {
  26.527 +    /* Noninterleaved DC scan for each component */
  26.528 +    scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  26.529 +  }
  26.530 +  return scanptr;
  26.531 +}
  26.532 +
  26.533 +
  26.534 +/*
  26.535 + * Create a recommended progressive-JPEG script.
  26.536 + * cinfo->num_components and cinfo->jpeg_color_space must be correct.
  26.537 + */
  26.538 +
  26.539 +GLOBAL(void)
  26.540 +jpeg_simple_progression (j_compress_ptr cinfo)
  26.541 +{
  26.542 +  int ncomps = cinfo->num_components;
  26.543 +  int nscans;
  26.544 +  jpeg_scan_info * scanptr;
  26.545 +
  26.546 +  /* Safety check to ensure start_compress not called yet. */
  26.547 +  if (cinfo->global_state != CSTATE_START)
  26.548 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  26.549 +
  26.550 +  /* Figure space needed for script.  Calculation must match code below! */
  26.551 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  26.552 +    /* Custom script for YCbCr color images. */
  26.553 +    nscans = 10;
  26.554 +  } else {
  26.555 +    /* All-purpose script for other color spaces. */
  26.556 +    if (ncomps > MAX_COMPS_IN_SCAN)
  26.557 +      nscans = 6 * ncomps;	/* 2 DC + 4 AC scans per component */
  26.558 +    else
  26.559 +      nscans = 2 + 4 * ncomps;	/* 2 DC scans; 4 AC scans per component */
  26.560 +  }
  26.561 +
  26.562 +  /* Allocate space for script.
  26.563 +   * We need to put it in the permanent pool in case the application performs
  26.564 +   * multiple compressions without changing the settings.  To avoid a memory
  26.565 +   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
  26.566 +   * object, we try to re-use previously allocated space, and we allocate
  26.567 +   * enough space to handle YCbCr even if initially asked for grayscale.
  26.568 +   */
  26.569 +  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
  26.570 +    cinfo->script_space_size = MAX(nscans, 10);
  26.571 +    cinfo->script_space = (jpeg_scan_info *)
  26.572 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  26.573 +			cinfo->script_space_size * SIZEOF(jpeg_scan_info));
  26.574 +  }
  26.575 +  scanptr = cinfo->script_space;
  26.576 +  cinfo->scan_info = scanptr;
  26.577 +  cinfo->num_scans = nscans;
  26.578 +
  26.579 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  26.580 +    /* Custom script for YCbCr color images. */
  26.581 +    /* Initial DC scan */
  26.582 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  26.583 +    /* Initial AC scan: get some luma data out in a hurry */
  26.584 +    scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  26.585 +    /* Chroma data is too small to be worth expending many scans on */
  26.586 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  26.587 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  26.588 +    /* Complete spectral selection for luma AC */
  26.589 +    scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  26.590 +    /* Refine next bit of luma AC */
  26.591 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  26.592 +    /* Finish DC successive approximation */
  26.593 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  26.594 +    /* Finish AC successive approximation */
  26.595 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  26.596 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  26.597 +    /* Luma bottom bit comes last since it's usually largest scan */
  26.598 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  26.599 +  } else {
  26.600 +    /* All-purpose script for other color spaces. */
  26.601 +    /* Successive approximation first pass */
  26.602 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  26.603 +    scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  26.604 +    scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  26.605 +    /* Successive approximation second pass */
  26.606 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  26.607 +    /* Successive approximation final pass */
  26.608 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  26.609 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  26.610 +  }
  26.611 +}
  26.612 +
  26.613 +#endif /* C_PROGRESSIVE_SUPPORTED */
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/libs/libjpeg/jcphuff.c	Thu Sep 08 06:28:38 2011 +0300
    27.3 @@ -0,0 +1,833 @@
    27.4 +/*
    27.5 + * jcphuff.c
    27.6 + *
    27.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    27.8 + * This file is part of the Independent JPEG Group's software.
    27.9 + * For conditions of distribution and use, see the accompanying README file.
   27.10 + *
   27.11 + * This file contains Huffman entropy encoding routines for progressive JPEG.
   27.12 + *
   27.13 + * We do not support output suspension in this module, since the library
   27.14 + * currently does not allow multiple-scan files to be written with output
   27.15 + * suspension.
   27.16 + */
   27.17 +
   27.18 +#define JPEG_INTERNALS
   27.19 +#include "jinclude.h"
   27.20 +#include "jpeglib.h"
   27.21 +#include "jchuff.h"		/* Declarations shared with jchuff.c */
   27.22 +
   27.23 +#ifdef C_PROGRESSIVE_SUPPORTED
   27.24 +
   27.25 +/* Expanded entropy encoder object for progressive Huffman encoding. */
   27.26 +
   27.27 +typedef struct {
   27.28 +  struct jpeg_entropy_encoder pub; /* public fields */
   27.29 +
   27.30 +  /* Mode flag: TRUE for optimization, FALSE for actual data output */
   27.31 +  boolean gather_statistics;
   27.32 +
   27.33 +  /* Bit-level coding status.
   27.34 +   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
   27.35 +   */
   27.36 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   27.37 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   27.38 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   27.39 +  int put_bits;			/* # of bits now in it */
   27.40 +  j_compress_ptr cinfo;		/* link to cinfo (needed for dump_buffer) */
   27.41 +
   27.42 +  /* Coding status for DC components */
   27.43 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   27.44 +
   27.45 +  /* Coding status for AC components */
   27.46 +  int ac_tbl_no;		/* the table number of the single component */
   27.47 +  unsigned int EOBRUN;		/* run length of EOBs */
   27.48 +  unsigned int BE;		/* # of buffered correction bits before MCU */
   27.49 +  char * bit_buffer;		/* buffer for correction bits (1 per char) */
   27.50 +  /* packing correction bits tightly would save some space but cost time... */
   27.51 +
   27.52 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   27.53 +  int next_restart_num;		/* next restart number to write (0-7) */
   27.54 +
   27.55 +  /* Pointers to derived tables (these workspaces have image lifespan).
   27.56 +   * Since any one scan codes only DC or only AC, we only need one set
   27.57 +   * of tables, not one for DC and one for AC.
   27.58 +   */
   27.59 +  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
   27.60 +
   27.61 +  /* Statistics tables for optimization; again, one set is enough */
   27.62 +  long * count_ptrs[NUM_HUFF_TBLS];
   27.63 +} phuff_entropy_encoder;
   27.64 +
   27.65 +typedef phuff_entropy_encoder * phuff_entropy_ptr;
   27.66 +
   27.67 +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
   27.68 + * buffer can hold.  Larger sizes may slightly improve compression, but
   27.69 + * 1000 is already well into the realm of overkill.
   27.70 + * The minimum safe size is 64 bits.
   27.71 + */
   27.72 +
   27.73 +#define MAX_CORR_BITS  1000	/* Max # of correction bits I can buffer */
   27.74 +
   27.75 +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
   27.76 + * We assume that int right shift is unsigned if INT32 right shift is,
   27.77 + * which should be safe.
   27.78 + */
   27.79 +
   27.80 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
   27.81 +#define ISHIFT_TEMPS	int ishift_temp;
   27.82 +#define IRIGHT_SHIFT(x,shft)  \
   27.83 +	((ishift_temp = (x)) < 0 ? \
   27.84 +	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
   27.85 +	 (ishift_temp >> (shft)))
   27.86 +#else
   27.87 +#define ISHIFT_TEMPS
   27.88 +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
   27.89 +#endif
   27.90 +
   27.91 +/* Forward declarations */
   27.92 +METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
   27.93 +					    JBLOCKROW *MCU_data));
   27.94 +METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
   27.95 +					    JBLOCKROW *MCU_data));
   27.96 +METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
   27.97 +					     JBLOCKROW *MCU_data));
   27.98 +METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
   27.99 +					     JBLOCKROW *MCU_data));
  27.100 +METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
  27.101 +METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
  27.102 +
  27.103 +
  27.104 +/*
  27.105 + * Initialize for a Huffman-compressed scan using progressive JPEG.
  27.106 + */
  27.107 +
  27.108 +METHODDEF(void)
  27.109 +start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
  27.110 +{  
  27.111 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.112 +  boolean is_DC_band;
  27.113 +  int ci, tbl;
  27.114 +  jpeg_component_info * compptr;
  27.115 +
  27.116 +  entropy->cinfo = cinfo;
  27.117 +  entropy->gather_statistics = gather_statistics;
  27.118 +
  27.119 +  is_DC_band = (cinfo->Ss == 0);
  27.120 +
  27.121 +  /* We assume jcmaster.c already validated the scan parameters. */
  27.122 +
  27.123 +  /* Select execution routines */
  27.124 +  if (cinfo->Ah == 0) {
  27.125 +    if (is_DC_band)
  27.126 +      entropy->pub.encode_mcu = encode_mcu_DC_first;
  27.127 +    else
  27.128 +      entropy->pub.encode_mcu = encode_mcu_AC_first;
  27.129 +  } else {
  27.130 +    if (is_DC_band)
  27.131 +      entropy->pub.encode_mcu = encode_mcu_DC_refine;
  27.132 +    else {
  27.133 +      entropy->pub.encode_mcu = encode_mcu_AC_refine;
  27.134 +      /* AC refinement needs a correction bit buffer */
  27.135 +      if (entropy->bit_buffer == NULL)
  27.136 +	entropy->bit_buffer = (char *)
  27.137 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  27.138 +				      MAX_CORR_BITS * SIZEOF(char));
  27.139 +    }
  27.140 +  }
  27.141 +  if (gather_statistics)
  27.142 +    entropy->pub.finish_pass = finish_pass_gather_phuff;
  27.143 +  else
  27.144 +    entropy->pub.finish_pass = finish_pass_phuff;
  27.145 +
  27.146 +  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
  27.147 +   * for AC coefficients.
  27.148 +   */
  27.149 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  27.150 +    compptr = cinfo->cur_comp_info[ci];
  27.151 +    /* Initialize DC predictions to 0 */
  27.152 +    entropy->last_dc_val[ci] = 0;
  27.153 +    /* Get table index */
  27.154 +    if (is_DC_band) {
  27.155 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  27.156 +	continue;
  27.157 +      tbl = compptr->dc_tbl_no;
  27.158 +    } else {
  27.159 +      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  27.160 +    }
  27.161 +    if (gather_statistics) {
  27.162 +      /* Check for invalid table index */
  27.163 +      /* (make_c_derived_tbl does this in the other path) */
  27.164 +      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
  27.165 +        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
  27.166 +      /* Allocate and zero the statistics tables */
  27.167 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  27.168 +      if (entropy->count_ptrs[tbl] == NULL)
  27.169 +	entropy->count_ptrs[tbl] = (long *)
  27.170 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  27.171 +				      257 * SIZEOF(long));
  27.172 +      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
  27.173 +    } else {
  27.174 +      /* Compute derived values for Huffman table */
  27.175 +      /* We may do this more than once for a table, but it's not expensive */
  27.176 +      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  27.177 +			      & entropy->derived_tbls[tbl]);
  27.178 +    }
  27.179 +  }
  27.180 +
  27.181 +  /* Initialize AC stuff */
  27.182 +  entropy->EOBRUN = 0;
  27.183 +  entropy->BE = 0;
  27.184 +
  27.185 +  /* Initialize bit buffer to empty */
  27.186 +  entropy->put_buffer = 0;
  27.187 +  entropy->put_bits = 0;
  27.188 +
  27.189 +  /* Initialize restart stuff */
  27.190 +  entropy->restarts_to_go = cinfo->restart_interval;
  27.191 +  entropy->next_restart_num = 0;
  27.192 +}
  27.193 +
  27.194 +
  27.195 +/* Outputting bytes to the file.
  27.196 + * NB: these must be called only when actually outputting,
  27.197 + * that is, entropy->gather_statistics == FALSE.
  27.198 + */
  27.199 +
  27.200 +/* Emit a byte */
  27.201 +#define emit_byte(entropy,val)  \
  27.202 +	{ *(entropy)->next_output_byte++ = (JOCTET) (val);  \
  27.203 +	  if (--(entropy)->free_in_buffer == 0)  \
  27.204 +	    dump_buffer(entropy); }
  27.205 +
  27.206 +
  27.207 +LOCAL(void)
  27.208 +dump_buffer (phuff_entropy_ptr entropy)
  27.209 +/* Empty the output buffer; we do not support suspension in this module. */
  27.210 +{
  27.211 +  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
  27.212 +
  27.213 +  if (! (*dest->empty_output_buffer) (entropy->cinfo))
  27.214 +    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  27.215 +  /* After a successful buffer dump, must reset buffer pointers */
  27.216 +  entropy->next_output_byte = dest->next_output_byte;
  27.217 +  entropy->free_in_buffer = dest->free_in_buffer;
  27.218 +}
  27.219 +
  27.220 +
  27.221 +/* Outputting bits to the file */
  27.222 +
  27.223 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  27.224 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  27.225 + * in one call, and we never retain more than 7 bits in put_buffer
  27.226 + * between calls, so 24 bits are sufficient.
  27.227 + */
  27.228 +
  27.229 +INLINE
  27.230 +LOCAL(void)
  27.231 +emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  27.232 +/* Emit some bits, unless we are in gather mode */
  27.233 +{
  27.234 +  /* This routine is heavily used, so it's worth coding tightly. */
  27.235 +  register INT32 put_buffer = (INT32) code;
  27.236 +  register int put_bits = entropy->put_bits;
  27.237 +
  27.238 +  /* if size is 0, caller used an invalid Huffman table entry */
  27.239 +  if (size == 0)
  27.240 +    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  27.241 +
  27.242 +  if (entropy->gather_statistics)
  27.243 +    return;			/* do nothing if we're only getting stats */
  27.244 +
  27.245 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  27.246 +  
  27.247 +  put_bits += size;		/* new number of bits in buffer */
  27.248 +  
  27.249 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  27.250 +
  27.251 +  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  27.252 +
  27.253 +  while (put_bits >= 8) {
  27.254 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  27.255 +    
  27.256 +    emit_byte(entropy, c);
  27.257 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  27.258 +      emit_byte(entropy, 0);
  27.259 +    }
  27.260 +    put_buffer <<= 8;
  27.261 +    put_bits -= 8;
  27.262 +  }
  27.263 +
  27.264 +  entropy->put_buffer = put_buffer; /* update variables */
  27.265 +  entropy->put_bits = put_bits;
  27.266 +}
  27.267 +
  27.268 +
  27.269 +LOCAL(void)
  27.270 +flush_bits (phuff_entropy_ptr entropy)
  27.271 +{
  27.272 +  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  27.273 +  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
  27.274 +  entropy->put_bits = 0;
  27.275 +}
  27.276 +
  27.277 +
  27.278 +/*
  27.279 + * Emit (or just count) a Huffman symbol.
  27.280 + */
  27.281 +
  27.282 +INLINE
  27.283 +LOCAL(void)
  27.284 +emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  27.285 +{
  27.286 +  if (entropy->gather_statistics)
  27.287 +    entropy->count_ptrs[tbl_no][symbol]++;
  27.288 +  else {
  27.289 +    c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  27.290 +    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  27.291 +  }
  27.292 +}
  27.293 +
  27.294 +
  27.295 +/*
  27.296 + * Emit bits from a correction bit buffer.
  27.297 + */
  27.298 +
  27.299 +LOCAL(void)
  27.300 +emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  27.301 +		    unsigned int nbits)
  27.302 +{
  27.303 +  if (entropy->gather_statistics)
  27.304 +    return;			/* no real work */
  27.305 +
  27.306 +  while (nbits > 0) {
  27.307 +    emit_bits(entropy, (unsigned int) (*bufstart), 1);
  27.308 +    bufstart++;
  27.309 +    nbits--;
  27.310 +  }
  27.311 +}
  27.312 +
  27.313 +
  27.314 +/*
  27.315 + * Emit any pending EOBRUN symbol.
  27.316 + */
  27.317 +
  27.318 +LOCAL(void)
  27.319 +emit_eobrun (phuff_entropy_ptr entropy)
  27.320 +{
  27.321 +  register int temp, nbits;
  27.322 +
  27.323 +  if (entropy->EOBRUN > 0) {	/* if there is any pending EOBRUN */
  27.324 +    temp = entropy->EOBRUN;
  27.325 +    nbits = 0;
  27.326 +    while ((temp >>= 1))
  27.327 +      nbits++;
  27.328 +    /* safety check: shouldn't happen given limited correction-bit buffer */
  27.329 +    if (nbits > 14)
  27.330 +      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  27.331 +
  27.332 +    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  27.333 +    if (nbits)
  27.334 +      emit_bits(entropy, entropy->EOBRUN, nbits);
  27.335 +
  27.336 +    entropy->EOBRUN = 0;
  27.337 +
  27.338 +    /* Emit any buffered correction bits */
  27.339 +    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  27.340 +    entropy->BE = 0;
  27.341 +  }
  27.342 +}
  27.343 +
  27.344 +
  27.345 +/*
  27.346 + * Emit a restart marker & resynchronize predictions.
  27.347 + */
  27.348 +
  27.349 +LOCAL(void)
  27.350 +emit_restart (phuff_entropy_ptr entropy, int restart_num)
  27.351 +{
  27.352 +  int ci;
  27.353 +
  27.354 +  emit_eobrun(entropy);
  27.355 +
  27.356 +  if (! entropy->gather_statistics) {
  27.357 +    flush_bits(entropy);
  27.358 +    emit_byte(entropy, 0xFF);
  27.359 +    emit_byte(entropy, JPEG_RST0 + restart_num);
  27.360 +  }
  27.361 +
  27.362 +  if (entropy->cinfo->Ss == 0) {
  27.363 +    /* Re-initialize DC predictions to 0 */
  27.364 +    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  27.365 +      entropy->last_dc_val[ci] = 0;
  27.366 +  } else {
  27.367 +    /* Re-initialize all AC-related fields to 0 */
  27.368 +    entropy->EOBRUN = 0;
  27.369 +    entropy->BE = 0;
  27.370 +  }
  27.371 +}
  27.372 +
  27.373 +
  27.374 +/*
  27.375 + * MCU encoding for DC initial scan (either spectral selection,
  27.376 + * or first pass of successive approximation).
  27.377 + */
  27.378 +
  27.379 +METHODDEF(boolean)
  27.380 +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  27.381 +{
  27.382 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.383 +  register int temp, temp2;
  27.384 +  register int nbits;
  27.385 +  int blkn, ci;
  27.386 +  int Al = cinfo->Al;
  27.387 +  JBLOCKROW block;
  27.388 +  jpeg_component_info * compptr;
  27.389 +  ISHIFT_TEMPS
  27.390 +
  27.391 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  27.392 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  27.393 +
  27.394 +  /* Emit restart marker if needed */
  27.395 +  if (cinfo->restart_interval)
  27.396 +    if (entropy->restarts_to_go == 0)
  27.397 +      emit_restart(entropy, entropy->next_restart_num);
  27.398 +
  27.399 +  /* Encode the MCU data blocks */
  27.400 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  27.401 +    block = MCU_data[blkn];
  27.402 +    ci = cinfo->MCU_membership[blkn];
  27.403 +    compptr = cinfo->cur_comp_info[ci];
  27.404 +
  27.405 +    /* Compute the DC value after the required point transform by Al.
  27.406 +     * This is simply an arithmetic right shift.
  27.407 +     */
  27.408 +    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  27.409 +
  27.410 +    /* DC differences are figured on the point-transformed values. */
  27.411 +    temp = temp2 - entropy->last_dc_val[ci];
  27.412 +    entropy->last_dc_val[ci] = temp2;
  27.413 +
  27.414 +    /* Encode the DC coefficient difference per section G.1.2.1 */
  27.415 +    temp2 = temp;
  27.416 +    if (temp < 0) {
  27.417 +      temp = -temp;		/* temp is abs value of input */
  27.418 +      /* For a negative input, want temp2 = bitwise complement of abs(input) */
  27.419 +      /* This code assumes we are on a two's complement machine */
  27.420 +      temp2--;
  27.421 +    }
  27.422 +    
  27.423 +    /* Find the number of bits needed for the magnitude of the coefficient */
  27.424 +    nbits = 0;
  27.425 +    while (temp) {
  27.426 +      nbits++;
  27.427 +      temp >>= 1;
  27.428 +    }
  27.429 +    /* Check for out-of-range coefficient values.
  27.430 +     * Since we're encoding a difference, the range limit is twice as much.
  27.431 +     */
  27.432 +    if (nbits > MAX_COEF_BITS+1)
  27.433 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  27.434 +    
  27.435 +    /* Count/emit the Huffman-coded symbol for the number of bits */
  27.436 +    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  27.437 +    
  27.438 +    /* Emit that number of bits of the value, if positive, */
  27.439 +    /* or the complement of its magnitude, if negative. */
  27.440 +    if (nbits)			/* emit_bits rejects calls with size 0 */
  27.441 +      emit_bits(entropy, (unsigned int) temp2, nbits);
  27.442 +  }
  27.443 +
  27.444 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  27.445 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  27.446 +
  27.447 +  /* Update restart-interval state too */
  27.448 +  if (cinfo->restart_interval) {
  27.449 +    if (entropy->restarts_to_go == 0) {
  27.450 +      entropy->restarts_to_go = cinfo->restart_interval;
  27.451 +      entropy->next_restart_num++;
  27.452 +      entropy->next_restart_num &= 7;
  27.453 +    }
  27.454 +    entropy->restarts_to_go--;
  27.455 +  }
  27.456 +
  27.457 +  return TRUE;
  27.458 +}
  27.459 +
  27.460 +
  27.461 +/*
  27.462 + * MCU encoding for AC initial scan (either spectral selection,
  27.463 + * or first pass of successive approximation).
  27.464 + */
  27.465 +
  27.466 +METHODDEF(boolean)
  27.467 +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  27.468 +{
  27.469 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.470 +  register int temp, temp2;
  27.471 +  register int nbits;
  27.472 +  register int r, k;
  27.473 +  int Se = cinfo->Se;
  27.474 +  int Al = cinfo->Al;
  27.475 +  JBLOCKROW block;
  27.476 +
  27.477 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  27.478 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  27.479 +
  27.480 +  /* Emit restart marker if needed */
  27.481 +  if (cinfo->restart_interval)
  27.482 +    if (entropy->restarts_to_go == 0)
  27.483 +      emit_restart(entropy, entropy->next_restart_num);
  27.484 +
  27.485 +  /* Encode the MCU data block */
  27.486 +  block = MCU_data[0];
  27.487 +
  27.488 +  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  27.489 +  
  27.490 +  r = 0;			/* r = run length of zeros */
  27.491 +   
  27.492 +  for (k = cinfo->Ss; k <= Se; k++) {
  27.493 +    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  27.494 +      r++;
  27.495 +      continue;
  27.496 +    }
  27.497 +    /* We must apply the point transform by Al.  For AC coefficients this
  27.498 +     * is an integer division with rounding towards 0.  To do this portably
  27.499 +     * in C, we shift after obtaining the absolute value; so the code is
  27.500 +     * interwoven with finding the abs value (temp) and output bits (temp2).
  27.501 +     */
  27.502 +    if (temp < 0) {
  27.503 +      temp = -temp;		/* temp is abs value of input */
  27.504 +      temp >>= Al;		/* apply the point transform */
  27.505 +      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  27.506 +      temp2 = ~temp;
  27.507 +    } else {
  27.508 +      temp >>= Al;		/* apply the point transform */
  27.509 +      temp2 = temp;
  27.510 +    }
  27.511 +    /* Watch out for case that nonzero coef is zero after point transform */
  27.512 +    if (temp == 0) {
  27.513 +      r++;
  27.514 +      continue;
  27.515 +    }
  27.516 +
  27.517 +    /* Emit any pending EOBRUN */
  27.518 +    if (entropy->EOBRUN > 0)
  27.519 +      emit_eobrun(entropy);
  27.520 +    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  27.521 +    while (r > 15) {
  27.522 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  27.523 +      r -= 16;
  27.524 +    }
  27.525 +
  27.526 +    /* Find the number of bits needed for the magnitude of the coefficient */
  27.527 +    nbits = 1;			/* there must be at least one 1 bit */
  27.528 +    while ((temp >>= 1))
  27.529 +      nbits++;
  27.530 +    /* Check for out-of-range coefficient values */
  27.531 +    if (nbits > MAX_COEF_BITS)
  27.532 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  27.533 +
  27.534 +    /* Count/emit Huffman symbol for run length / number of bits */
  27.535 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
  27.536 +
  27.537 +    /* Emit that number of bits of the value, if positive, */
  27.538 +    /* or the complement of its magnitude, if negative. */
  27.539 +    emit_bits(entropy, (unsigned int) temp2, nbits);
  27.540 +
  27.541 +    r = 0;			/* reset zero run length */
  27.542 +  }
  27.543 +
  27.544 +  if (r > 0) {			/* If there are trailing zeroes, */
  27.545 +    entropy->EOBRUN++;		/* count an EOB */
  27.546 +    if (entropy->EOBRUN == 0x7FFF)
  27.547 +      emit_eobrun(entropy);	/* force it out to avoid overflow */
  27.548 +  }
  27.549 +
  27.550 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  27.551 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  27.552 +
  27.553 +  /* Update restart-interval state too */
  27.554 +  if (cinfo->restart_interval) {
  27.555 +    if (entropy->restarts_to_go == 0) {
  27.556 +      entropy->restarts_to_go = cinfo->restart_interval;
  27.557 +      entropy->next_restart_num++;
  27.558 +      entropy->next_restart_num &= 7;
  27.559 +    }
  27.560 +    entropy->restarts_to_go--;
  27.561 +  }
  27.562 +
  27.563 +  return TRUE;
  27.564 +}
  27.565 +
  27.566 +
  27.567 +/*
  27.568 + * MCU encoding for DC successive approximation refinement scan.
  27.569 + * Note: we assume such scans can be multi-component, although the spec
  27.570 + * is not very clear on the point.
  27.571 + */
  27.572 +
  27.573 +METHODDEF(boolean)
  27.574 +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  27.575 +{
  27.576 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.577 +  register int temp;
  27.578 +  int blkn;
  27.579 +  int Al = cinfo->Al;
  27.580 +  JBLOCKROW block;
  27.581 +
  27.582 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  27.583 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  27.584 +
  27.585 +  /* Emit restart marker if needed */
  27.586 +  if (cinfo->restart_interval)
  27.587 +    if (entropy->restarts_to_go == 0)
  27.588 +      emit_restart(entropy, entropy->next_restart_num);
  27.589 +
  27.590 +  /* Encode the MCU data blocks */
  27.591 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  27.592 +    block = MCU_data[blkn];
  27.593 +
  27.594 +    /* We simply emit the Al'th bit of the DC coefficient value. */
  27.595 +    temp = (*block)[0];
  27.596 +    emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  27.597 +  }
  27.598 +
  27.599 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  27.600 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  27.601 +
  27.602 +  /* Update restart-interval state too */
  27.603 +  if (cinfo->restart_interval) {
  27.604 +    if (entropy->restarts_to_go == 0) {
  27.605 +      entropy->restarts_to_go = cinfo->restart_interval;
  27.606 +      entropy->next_restart_num++;
  27.607 +      entropy->next_restart_num &= 7;
  27.608 +    }
  27.609 +    entropy->restarts_to_go--;
  27.610 +  }
  27.611 +
  27.612 +  return TRUE;
  27.613 +}
  27.614 +
  27.615 +
  27.616 +/*
  27.617 + * MCU encoding for AC successive approximation refinement scan.
  27.618 + */
  27.619 +
  27.620 +METHODDEF(boolean)
  27.621 +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  27.622 +{
  27.623 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.624 +  register int temp;
  27.625 +  register int r, k;
  27.626 +  int EOB;
  27.627 +  char *BR_buffer;
  27.628 +  unsigned int BR;
  27.629 +  int Se = cinfo->Se;
  27.630 +  int Al = cinfo->Al;
  27.631 +  JBLOCKROW block;
  27.632 +  int absvalues[DCTSIZE2];
  27.633 +
  27.634 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  27.635 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  27.636 +
  27.637 +  /* Emit restart marker if needed */
  27.638 +  if (cinfo->restart_interval)
  27.639 +    if (entropy->restarts_to_go == 0)
  27.640 +      emit_restart(entropy, entropy->next_restart_num);
  27.641 +
  27.642 +  /* Encode the MCU data block */
  27.643 +  block = MCU_data[0];
  27.644 +
  27.645 +  /* It is convenient to make a pre-pass to determine the transformed
  27.646 +   * coefficients' absolute values and the EOB position.
  27.647 +   */
  27.648 +  EOB = 0;
  27.649 +  for (k = cinfo->Ss; k <= Se; k++) {
  27.650 +    temp = (*block)[jpeg_natural_order[k]];
  27.651 +    /* We must apply the point transform by Al.  For AC coefficients this
  27.652 +     * is an integer division with rounding towards 0.  To do this portably
  27.653 +     * in C, we shift after obtaining the absolute value.
  27.654 +     */
  27.655 +    if (temp < 0)
  27.656 +      temp = -temp;		/* temp is abs value of input */
  27.657 +    temp >>= Al;		/* apply the point transform */
  27.658 +    absvalues[k] = temp;	/* save abs value for main pass */
  27.659 +    if (temp == 1)
  27.660 +      EOB = k;			/* EOB = index of last newly-nonzero coef */
  27.661 +  }
  27.662 +
  27.663 +  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  27.664 +  
  27.665 +  r = 0;			/* r = run length of zeros */
  27.666 +  BR = 0;			/* BR = count of buffered bits added now */
  27.667 +  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  27.668 +
  27.669 +  for (k = cinfo->Ss; k <= Se; k++) {
  27.670 +    if ((temp = absvalues[k]) == 0) {
  27.671 +      r++;
  27.672 +      continue;
  27.673 +    }
  27.674 +
  27.675 +    /* Emit any required ZRLs, but not if they can be folded into EOB */
  27.676 +    while (r > 15 && k <= EOB) {
  27.677 +      /* emit any pending EOBRUN and the BE correction bits */
  27.678 +      emit_eobrun(entropy);
  27.679 +      /* Emit ZRL */
  27.680 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  27.681 +      r -= 16;
  27.682 +      /* Emit buffered correction bits that must be associated with ZRL */
  27.683 +      emit_buffered_bits(entropy, BR_buffer, BR);
  27.684 +      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  27.685 +      BR = 0;
  27.686 +    }
  27.687 +
  27.688 +    /* If the coef was previously nonzero, it only needs a correction bit.
  27.689 +     * NOTE: a straight translation of the spec's figure G.7 would suggest
  27.690 +     * that we also need to test r > 15.  But if r > 15, we can only get here
  27.691 +     * if k > EOB, which implies that this coefficient is not 1.
  27.692 +     */
  27.693 +    if (temp > 1) {
  27.694 +      /* The correction bit is the next bit of the absolute value. */
  27.695 +      BR_buffer[BR++] = (char) (temp & 1);
  27.696 +      continue;
  27.697 +    }
  27.698 +
  27.699 +    /* Emit any pending EOBRUN and the BE correction bits */
  27.700 +    emit_eobrun(entropy);
  27.701 +
  27.702 +    /* Count/emit Huffman symbol for run length / number of bits */
  27.703 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
  27.704 +
  27.705 +    /* Emit output bit for newly-nonzero coef */
  27.706 +    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
  27.707 +    emit_bits(entropy, (unsigned int) temp, 1);
  27.708 +
  27.709 +    /* Emit buffered correction bits that must be associated with this code */
  27.710 +    emit_buffered_bits(entropy, BR_buffer, BR);
  27.711 +    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  27.712 +    BR = 0;
  27.713 +    r = 0;			/* reset zero run length */
  27.714 +  }
  27.715 +
  27.716 +  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */
  27.717 +    entropy->EOBRUN++;		/* count an EOB */
  27.718 +    entropy->BE += BR;		/* concat my correction bits to older ones */
  27.719 +    /* We force out the EOB if we risk either:
  27.720 +     * 1. overflow of the EOB counter;
  27.721 +     * 2. overflow of the correction bit buffer during the next MCU.
  27.722 +     */
  27.723 +    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
  27.724 +      emit_eobrun(entropy);
  27.725 +  }
  27.726 +
  27.727 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  27.728 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  27.729 +
  27.730 +  /* Update restart-interval state too */
  27.731 +  if (cinfo->restart_interval) {
  27.732 +    if (entropy->restarts_to_go == 0) {
  27.733 +      entropy->restarts_to_go = cinfo->restart_interval;
  27.734 +      entropy->next_restart_num++;
  27.735 +      entropy->next_restart_num &= 7;
  27.736 +    }
  27.737 +    entropy->restarts_to_go--;
  27.738 +  }
  27.739 +
  27.740 +  return TRUE;
  27.741 +}
  27.742 +
  27.743 +
  27.744 +/*
  27.745 + * Finish up at the end of a Huffman-compressed progressive scan.
  27.746 + */
  27.747 +
  27.748 +METHODDEF(void)
  27.749 +finish_pass_phuff (j_compress_ptr cinfo)
  27.750 +{   
  27.751 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.752 +
  27.753 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  27.754 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  27.755 +
  27.756 +  /* Flush out any buffered data */
  27.757 +  emit_eobrun(entropy);
  27.758 +  flush_bits(entropy);
  27.759 +
  27.760 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  27.761 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  27.762 +}
  27.763 +
  27.764 +
  27.765 +/*
  27.766 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  27.767 + */
  27.768 +
  27.769 +METHODDEF(void)
  27.770 +finish_pass_gather_phuff (j_compress_ptr cinfo)
  27.771 +{
  27.772 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  27.773 +  boolean is_DC_band;
  27.774 +  int ci, tbl;
  27.775 +  jpeg_component_info * compptr;
  27.776 +  JHUFF_TBL **htblptr;
  27.777 +  boolean did[NUM_HUFF_TBLS];
  27.778 +
  27.779 +  /* Flush out buffered data (all we care about is counting the EOB symbol) */
  27.780 +  emit_eobrun(entropy);
  27.781 +
  27.782 +  is_DC_band = (cinfo->Ss == 0);
  27.783 +
  27.784 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  27.785 +   * per table, because it clobbers the input frequency counts!
  27.786 +   */
  27.787 +  MEMZERO(did, SIZEOF(did));
  27.788 +
  27.789 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  27.790 +    compptr = cinfo->cur_comp_info[ci];
  27.791 +    if (is_DC_band) {
  27.792 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  27.793 +	continue;
  27.794 +      tbl = compptr->dc_tbl_no;
  27.795 +    } else {
  27.796 +      tbl = compptr->ac_tbl_no;
  27.797 +    }
  27.798 +    if (! did[tbl]) {
  27.799 +      if (is_DC_band)
  27.800 +        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  27.801 +      else
  27.802 +        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  27.803 +      if (*htblptr == NULL)
  27.804 +        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  27.805 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  27.806 +      did[tbl] = TRUE;
  27.807 +    }
  27.808 +  }
  27.809 +}
  27.810 +
  27.811 +
  27.812 +/*
  27.813 + * Module initialization routine for progressive Huffman entropy encoding.
  27.814 + */
  27.815 +
  27.816 +GLOBAL(void)
  27.817 +jinit_phuff_encoder (j_compress_ptr cinfo)
  27.818 +{
  27.819 +  phuff_entropy_ptr entropy;
  27.820 +  int i;
  27.821 +
  27.822 +  entropy = (phuff_entropy_ptr)
  27.823 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  27.824 +				SIZEOF(phuff_entropy_encoder));
  27.825 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  27.826 +  entropy->pub.start_pass = start_pass_phuff;
  27.827 +
  27.828 +  /* Mark tables unallocated */
  27.829 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  27.830 +    entropy->derived_tbls[i] = NULL;
  27.831 +    entropy->count_ptrs[i] = NULL;
  27.832 +  }
  27.833 +  entropy->bit_buffer = NULL;	/* needed only in AC refinement scan */
  27.834 +}
  27.835 +
  27.836 +#endif /* C_PROGRESSIVE_SUPPORTED */
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/libs/libjpeg/jcprepct.c	Thu Sep 08 06:28:38 2011 +0300
    28.3 @@ -0,0 +1,354 @@
    28.4 +/*
    28.5 + * jcprepct.c
    28.6 + *
    28.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    28.8 + * This file is part of the Independent JPEG Group's software.
    28.9 + * For conditions of distribution and use, see the accompanying README file.
   28.10 + *
   28.11 + * This file contains the compression preprocessing controller.
   28.12 + * This controller manages the color conversion, downsampling,
   28.13 + * and edge expansion steps.
   28.14 + *
   28.15 + * Most of the complexity here is associated with buffering input rows
   28.16 + * as required by the downsampler.  See the comments at the head of
   28.17 + * jcsample.c for the downsampler's needs.
   28.18 + */
   28.19 +
   28.20 +#define JPEG_INTERNALS
   28.21 +#include "jinclude.h"
   28.22 +#include "jpeglib.h"
   28.23 +
   28.24 +
   28.25 +/* At present, jcsample.c can request context rows only for smoothing.
   28.26 + * In the future, we might also need context rows for CCIR601 sampling
   28.27 + * or other more-complex downsampling procedures.  The code to support
   28.28 + * context rows should be compiled only if needed.
   28.29 + */
   28.30 +#ifdef INPUT_SMOOTHING_SUPPORTED
   28.31 +#define CONTEXT_ROWS_SUPPORTED
   28.32 +#endif
   28.33 +
   28.34 +
   28.35 +/*
   28.36 + * For the simple (no-context-row) case, we just need to buffer one
   28.37 + * row group's worth of pixels for the downsampling step.  At the bottom of
   28.38 + * the image, we pad to a full row group by replicating the last pixel row.
   28.39 + * The downsampler's last output row is then replicated if needed to pad
   28.40 + * out to a full iMCU row.
   28.41 + *
   28.42 + * When providing context rows, we must buffer three row groups' worth of
   28.43 + * pixels.  Three row groups are physically allocated, but the row pointer
   28.44 + * arrays are made five row groups high, with the extra pointers above and
   28.45 + * below "wrapping around" to point to the last and first real row groups.
   28.46 + * This allows the downsampler to access the proper context rows.
   28.47 + * At the top and bottom of the image, we create dummy context rows by
   28.48 + * copying the first or last real pixel row.  This copying could be avoided
   28.49 + * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
   28.50 + * trouble on the compression side.
   28.51 + */
   28.52 +
   28.53 +
   28.54 +/* Private buffer controller object */
   28.55 +
   28.56 +typedef struct {
   28.57 +  struct jpeg_c_prep_controller pub; /* public fields */
   28.58 +
   28.59 +  /* Downsampling input buffer.  This buffer holds color-converted data
   28.60 +   * until we have enough to do a downsample step.
   28.61 +   */
   28.62 +  JSAMPARRAY color_buf[MAX_COMPONENTS];
   28.63 +
   28.64 +  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
   28.65 +  int next_buf_row;		/* index of next row to store in color_buf */
   28.66 +
   28.67 +#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
   28.68 +  int this_row_group;		/* starting row index of group to process */
   28.69 +  int next_buf_stop;		/* downsample when we reach this index */
   28.70 +#endif
   28.71 +} my_prep_controller;
   28.72 +
   28.73 +typedef my_prep_controller * my_prep_ptr;
   28.74 +
   28.75 +
   28.76 +/*
   28.77 + * Initialize for a processing pass.
   28.78 + */
   28.79 +
   28.80 +METHODDEF(void)
   28.81 +start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   28.82 +{
   28.83 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
   28.84 +
   28.85 +  if (pass_mode != JBUF_PASS_THRU)
   28.86 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   28.87 +
   28.88 +  /* Initialize total-height counter for detecting bottom of image */
   28.89 +  prep->rows_to_go = cinfo->image_height;
   28.90 +  /* Mark the conversion buffer empty */
   28.91 +  prep->next_buf_row = 0;
   28.92 +#ifdef CONTEXT_ROWS_SUPPORTED
   28.93 +  /* Preset additional state variables for context mode.
   28.94 +   * These aren't used in non-context mode, so we needn't test which mode.
   28.95 +   */
   28.96 +  prep->this_row_group = 0;
   28.97 +  /* Set next_buf_stop to stop after two row groups have been read in. */
   28.98 +  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
   28.99 +#endif
  28.100 +}
  28.101 +
  28.102 +
  28.103 +/*
  28.104 + * Expand an image vertically from height input_rows to height output_rows,
  28.105 + * by duplicating the bottom row.
  28.106 + */
  28.107 +
  28.108 +LOCAL(void)
  28.109 +expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  28.110 +		    int input_rows, int output_rows)
  28.111 +{
  28.112 +  register int row;
  28.113 +
  28.114 +  for (row = input_rows; row < output_rows; row++) {
  28.115 +    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  28.116 +		      1, num_cols);
  28.117 +  }
  28.118 +}
  28.119 +
  28.120 +
  28.121 +/*
  28.122 + * Process some data in the simple no-context case.
  28.123 + *
  28.124 + * Preprocessor output data is counted in "row groups".  A row group
  28.125 + * is defined to be v_samp_factor sample rows of each component.
  28.126 + * Downsampling will produce this much data from each max_v_samp_factor
  28.127 + * input rows.
  28.128 + */
  28.129 +
  28.130 +METHODDEF(void)
  28.131 +pre_process_data (j_compress_ptr cinfo,
  28.132 +		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  28.133 +		  JDIMENSION in_rows_avail,
  28.134 +		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  28.135 +		  JDIMENSION out_row_groups_avail)
  28.136 +{
  28.137 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  28.138 +  int numrows, ci;
  28.139 +  JDIMENSION inrows;
  28.140 +  jpeg_component_info * compptr;
  28.141 +
  28.142 +  while (*in_row_ctr < in_rows_avail &&
  28.143 +	 *out_row_group_ctr < out_row_groups_avail) {
  28.144 +    /* Do color conversion to fill the conversion buffer. */
  28.145 +    inrows = in_rows_avail - *in_row_ctr;
  28.146 +    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  28.147 +    numrows = (int) MIN((JDIMENSION) numrows, inrows);
  28.148 +    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  28.149 +				       prep->color_buf,
  28.150 +				       (JDIMENSION) prep->next_buf_row,
  28.151 +				       numrows);
  28.152 +    *in_row_ctr += numrows;
  28.153 +    prep->next_buf_row += numrows;
  28.154 +    prep->rows_to_go -= numrows;
  28.155 +    /* If at bottom of image, pad to fill the conversion buffer. */
  28.156 +    if (prep->rows_to_go == 0 &&
  28.157 +	prep->next_buf_row < cinfo->max_v_samp_factor) {
  28.158 +      for (ci = 0; ci < cinfo->num_components; ci++) {
  28.159 +	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  28.160 +			   prep->next_buf_row, cinfo->max_v_samp_factor);
  28.161 +      }
  28.162 +      prep->next_buf_row = cinfo->max_v_samp_factor;
  28.163 +    }
  28.164 +    /* If we've filled the conversion buffer, empty it. */
  28.165 +    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  28.166 +      (*cinfo->downsample->downsample) (cinfo,
  28.167 +					prep->color_buf, (JDIMENSION) 0,
  28.168 +					output_buf, *out_row_group_ctr);
  28.169 +      prep->next_buf_row = 0;
  28.170 +      (*out_row_group_ctr)++;
  28.171 +    }
  28.172 +    /* If at bottom of image, pad the output to a full iMCU height.
  28.173 +     * Note we assume the caller is providing a one-iMCU-height output buffer!
  28.174 +     */
  28.175 +    if (prep->rows_to_go == 0 &&
  28.176 +	*out_row_group_ctr < out_row_groups_avail) {
  28.177 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.178 +	   ci++, compptr++) {
  28.179 +	expand_bottom_edge(output_buf[ci],
  28.180 +			   compptr->width_in_blocks * DCTSIZE,
  28.181 +			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
  28.182 +			   (int) (out_row_groups_avail * compptr->v_samp_factor));
  28.183 +      }
  28.184 +      *out_row_group_ctr = out_row_groups_avail;
  28.185 +      break;			/* can exit outer loop without test */
  28.186 +    }
  28.187 +  }
  28.188 +}
  28.189 +
  28.190 +
  28.191 +#ifdef CONTEXT_ROWS_SUPPORTED
  28.192 +
  28.193 +/*
  28.194 + * Process some data in the context case.
  28.195 + */
  28.196 +
  28.197 +METHODDEF(void)
  28.198 +pre_process_context (j_compress_ptr cinfo,
  28.199 +		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  28.200 +		     JDIMENSION in_rows_avail,
  28.201 +		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  28.202 +		     JDIMENSION out_row_groups_avail)
  28.203 +{
  28.204 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  28.205 +  int numrows, ci;
  28.206 +  int buf_height = cinfo->max_v_samp_factor * 3;
  28.207 +  JDIMENSION inrows;
  28.208 +
  28.209 +  while (*out_row_group_ctr < out_row_groups_avail) {
  28.210 +    if (*in_row_ctr < in_rows_avail) {
  28.211 +      /* Do color conversion to fill the conversion buffer. */
  28.212 +      inrows = in_rows_avail - *in_row_ctr;
  28.213 +      numrows = prep->next_buf_stop - prep->next_buf_row;
  28.214 +      numrows = (int) MIN((JDIMENSION) numrows, inrows);
  28.215 +      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  28.216 +					 prep->color_buf,
  28.217 +					 (JDIMENSION) prep->next_buf_row,
  28.218 +					 numrows);
  28.219 +      /* Pad at top of image, if first time through */
  28.220 +      if (prep->rows_to_go == cinfo->image_height) {
  28.221 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  28.222 +	  int row;
  28.223 +	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  28.224 +	    jcopy_sample_rows(prep->color_buf[ci], 0,
  28.225 +			      prep->color_buf[ci], -row,
  28.226 +			      1, cinfo->image_width);
  28.227 +	  }
  28.228 +	}
  28.229 +      }
  28.230 +      *in_row_ctr += numrows;
  28.231 +      prep->next_buf_row += numrows;
  28.232 +      prep->rows_to_go -= numrows;
  28.233 +    } else {
  28.234 +      /* Return for more data, unless we are at the bottom of the image. */
  28.235 +      if (prep->rows_to_go != 0)
  28.236 +	break;
  28.237 +      /* When at bottom of image, pad to fill the conversion buffer. */
  28.238 +      if (prep->next_buf_row < prep->next_buf_stop) {
  28.239 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  28.240 +	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  28.241 +			     prep->next_buf_row, prep->next_buf_stop);
  28.242 +	}
  28.243 +	prep->next_buf_row = prep->next_buf_stop;
  28.244 +      }
  28.245 +    }
  28.246 +    /* If we've gotten enough data, downsample a row group. */
  28.247 +    if (prep->next_buf_row == prep->next_buf_stop) {
  28.248 +      (*cinfo->downsample->downsample) (cinfo,
  28.249 +					prep->color_buf,
  28.250 +					(JDIMENSION) prep->this_row_group,
  28.251 +					output_buf, *out_row_group_ctr);
  28.252 +      (*out_row_group_ctr)++;
  28.253 +      /* Advance pointers with wraparound as necessary. */
  28.254 +      prep->this_row_group += cinfo->max_v_samp_factor;
  28.255 +      if (prep->this_row_group >= buf_height)
  28.256 +	prep->this_row_group = 0;
  28.257 +      if (prep->next_buf_row >= buf_height)
  28.258 +	prep->next_buf_row = 0;
  28.259 +      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  28.260 +    }
  28.261 +  }
  28.262 +}
  28.263 +
  28.264 +
  28.265 +/*
  28.266 + * Create the wrapped-around downsampling input buffer needed for context mode.
  28.267 + */
  28.268 +
  28.269 +LOCAL(void)
  28.270 +create_context_buffer (j_compress_ptr cinfo)
  28.271 +{
  28.272 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  28.273 +  int rgroup_height = cinfo->max_v_samp_factor;
  28.274 +  int ci, i;
  28.275 +  jpeg_component_info * compptr;
  28.276 +  JSAMPARRAY true_buffer, fake_buffer;
  28.277 +
  28.278 +  /* Grab enough space for fake row pointers for all the components;
  28.279 +   * we need five row groups' worth of pointers for each component.
  28.280 +   */
  28.281 +  fake_buffer = (JSAMPARRAY)
  28.282 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  28.283 +				(cinfo->num_components * 5 * rgroup_height) *
  28.284 +				SIZEOF(JSAMPROW));
  28.285 +
  28.286 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.287 +       ci++, compptr++) {
  28.288 +    /* Allocate the actual buffer space (3 row groups) for this component.
  28.289 +     * We make the buffer wide enough to allow the downsampler to edge-expand
  28.290 +     * horizontally within the buffer, if it so chooses.
  28.291 +     */
  28.292 +    true_buffer = (*cinfo->mem->alloc_sarray)
  28.293 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
  28.294 +       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  28.295 +		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  28.296 +       (JDIMENSION) (3 * rgroup_height));
  28.297 +    /* Copy true buffer row pointers into the middle of the fake row array */
  28.298 +    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  28.299 +	    3 * rgroup_height * SIZEOF(JSAMPROW));
  28.300 +    /* Fill in the above and below wraparound pointers */
  28.301 +    for (i = 0; i < rgroup_height; i++) {
  28.302 +      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  28.303 +      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  28.304 +    }
  28.305 +    prep->color_buf[ci] = fake_buffer + rgroup_height;
  28.306 +    fake_buffer += 5 * rgroup_height; /* point to space for next component */
  28.307 +  }
  28.308 +}
  28.309 +
  28.310 +#endif /* CONTEXT_ROWS_SUPPORTED */
  28.311 +
  28.312 +
  28.313 +/*
  28.314 + * Initialize preprocessing controller.
  28.315 + */
  28.316 +
  28.317 +GLOBAL(void)
  28.318 +jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  28.319 +{
  28.320 +  my_prep_ptr prep;
  28.321 +  int ci;
  28.322 +  jpeg_component_info * compptr;
  28.323 +
  28.324 +  if (need_full_buffer)		/* safety check */
  28.325 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  28.326 +
  28.327 +  prep = (my_prep_ptr)
  28.328 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  28.329 +				SIZEOF(my_prep_controller));
  28.330 +  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  28.331 +  prep->pub.start_pass = start_pass_prep;
  28.332 +
  28.333 +  /* Allocate the color conversion buffer.
  28.334 +   * We make the buffer wide enough to allow the downsampler to edge-expand
  28.335 +   * horizontally within the buffer, if it so chooses.
  28.336 +   */
  28.337 +  if (cinfo->downsample->need_context_rows) {
  28.338 +    /* Set up to provide context rows */
  28.339 +#ifdef CONTEXT_ROWS_SUPPORTED
  28.340 +    prep->pub.pre_process_data = pre_process_context;
  28.341 +    create_context_buffer(cinfo);
  28.342 +#else
  28.343 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  28.344 +#endif
  28.345 +  } else {
  28.346 +    /* No context, just make it tall enough for one row group */
  28.347 +    prep->pub.pre_process_data = pre_process_data;
  28.348 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.349 +	 ci++, compptr++) {
  28.350 +      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  28.351 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  28.352 +	 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  28.353 +			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  28.354 +	 (JDIMENSION) cinfo->max_v_samp_factor);
  28.355 +    }
  28.356 +  }
  28.357 +}
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/libs/libjpeg/jcsample.c	Thu Sep 08 06:28:38 2011 +0300
    29.3 @@ -0,0 +1,519 @@
    29.4 +/*
    29.5 + * jcsample.c
    29.6 + *
    29.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    29.8 + * This file is part of the Independent JPEG Group's software.
    29.9 + * For conditions of distribution and use, see the accompanying README file.
   29.10 + *
   29.11 + * This file contains downsampling routines.
   29.12 + *
   29.13 + * Downsampling input data is counted in "row groups".  A row group
   29.14 + * is defined to be max_v_samp_factor pixel rows of each component,
   29.15 + * from which the downsampler produces v_samp_factor sample rows.
   29.16 + * A single row group is processed in each call to the downsampler module.
   29.17 + *
   29.18 + * The downsampler is responsible for edge-expansion of its output data
   29.19 + * to fill an integral number of DCT blocks horizontally.  The source buffer
   29.20 + * may be modified if it is helpful for this purpose (the source buffer is
   29.21 + * allocated wide enough to correspond to the desired output width).
   29.22 + * The caller (the prep controller) is responsible for vertical padding.
   29.23 + *
   29.24 + * The downsampler may request "context rows" by setting need_context_rows
   29.25 + * during startup.  In this case, the input arrays will contain at least
   29.26 + * one row group's worth of pixels above and below the passed-in data;
   29.27 + * the caller will create dummy rows at image top and bottom by replicating
   29.28 + * the first or last real pixel row.
   29.29 + *
   29.30 + * An excellent reference for image resampling is
   29.31 + *   Digital Image Warping, George Wolberg, 1990.
   29.32 + *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
   29.33 + *
   29.34 + * The downsampling algorithm used here is a simple average of the source
   29.35 + * pixels covered by the output pixel.  The hi-falutin sampling literature
   29.36 + * refers to this as a "box filter".  In general the characteristics of a box
   29.37 + * filter are not very good, but for the specific cases we normally use (1:1
   29.38 + * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
   29.39 + * nearly so bad.  If you intend to use other sampling ratios, you'd be well
   29.40 + * advised to improve this code.
   29.41 + *
   29.42 + * A simple input-smoothing capability is provided.  This is mainly intended
   29.43 + * for cleaning up color-dithered GIF input files (if you find it inadequate,
   29.44 + * we suggest using an external filtering program such as pnmconvol).  When
   29.45 + * enabled, each input pixel P is replaced by a weighted sum of itself and its
   29.46 + * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
   29.47 + * where SF = (smoothing_factor / 1024).
   29.48 + * Currently, smoothing is only supported for 2h2v sampling factors.
   29.49 + */
   29.50 +
   29.51 +#define JPEG_INTERNALS
   29.52 +#include "jinclude.h"
   29.53 +#include "jpeglib.h"
   29.54 +
   29.55 +
   29.56 +/* Pointer to routine to downsample a single component */
   29.57 +typedef JMETHOD(void, downsample1_ptr,
   29.58 +		(j_compress_ptr cinfo, jpeg_component_info * compptr,
   29.59 +		 JSAMPARRAY input_data, JSAMPARRAY output_data));
   29.60 +
   29.61 +/* Private subobject */
   29.62 +
   29.63 +typedef struct {
   29.64 +  struct jpeg_downsampler pub;	/* public fields */
   29.65 +
   29.66 +  /* Downsampling method pointers, one per component */
   29.67 +  downsample1_ptr methods[MAX_COMPONENTS];
   29.68 +} my_downsampler;
   29.69 +
   29.70 +typedef my_downsampler * my_downsample_ptr;
   29.71 +
   29.72 +
   29.73 +/*
   29.74 + * Initialize for a downsampling pass.
   29.75 + */
   29.76 +
   29.77 +METHODDEF(void)
   29.78 +start_pass_downsample (j_compress_ptr cinfo)
   29.79 +{
   29.80 +  /* no work for now */
   29.81 +}
   29.82 +
   29.83 +
   29.84 +/*
   29.85 + * Expand a component horizontally from width input_cols to width output_cols,
   29.86 + * by duplicating the rightmost samples.
   29.87 + */
   29.88 +
   29.89 +LOCAL(void)
   29.90 +expand_right_edge (JSAMPARRAY image_data, int num_rows,
   29.91 +		   JDIMENSION input_cols, JDIMENSION output_cols)
   29.92 +{
   29.93 +  register JSAMPROW ptr;
   29.94 +  register JSAMPLE pixval;
   29.95 +  register int count;
   29.96 +  int row;
   29.97 +  int numcols = (int) (output_cols - input_cols);
   29.98 +
   29.99 +  if (numcols > 0) {
  29.100 +    for (row = 0; row < num_rows; row++) {
  29.101 +      ptr = image_data[row] + input_cols;
  29.102 +      pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
  29.103 +      for (count = numcols; count > 0; count--)
  29.104 +	*ptr++ = pixval;
  29.105 +    }
  29.106 +  }
  29.107 +}
  29.108 +
  29.109 +
  29.110 +/*
  29.111 + * Do downsampling for a whole row group (all components).
  29.112 + *
  29.113 + * In this version we simply downsample each component independently.
  29.114 + */
  29.115 +
  29.116 +METHODDEF(void)
  29.117 +sep_downsample (j_compress_ptr cinfo,
  29.118 +		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
  29.119 +		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
  29.120 +{
  29.121 +  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
  29.122 +  int ci;
  29.123 +  jpeg_component_info * compptr;
  29.124 +  JSAMPARRAY in_ptr, out_ptr;
  29.125 +
  29.126 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  29.127 +       ci++, compptr++) {
  29.128 +    in_ptr = input_buf[ci] + in_row_index;
  29.129 +    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
  29.130 +    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
  29.131 +  }
  29.132 +}
  29.133 +
  29.134 +
  29.135 +/*
  29.136 + * Downsample pixel values of a single component.
  29.137 + * One row group is processed per call.
  29.138 + * This version handles arbitrary integral sampling ratios, without smoothing.
  29.139 + * Note that this version is not actually used for customary sampling ratios.
  29.140 + */
  29.141 +
  29.142 +METHODDEF(void)
  29.143 +int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  29.144 +		JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.145 +{
  29.146 +  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  29.147 +  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
  29.148 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  29.149 +  JSAMPROW inptr, outptr;
  29.150 +  INT32 outvalue;
  29.151 +
  29.152 +  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  29.153 +  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  29.154 +  numpix = h_expand * v_expand;
  29.155 +  numpix2 = numpix/2;
  29.156 +
  29.157 +  /* Expand input data enough to let all the output samples be generated
  29.158 +   * by the standard loop.  Special-casing padded output would be more
  29.159 +   * efficient.
  29.160 +   */
  29.161 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  29.162 +		    cinfo->image_width, output_cols * h_expand);
  29.163 +
  29.164 +  inrow = 0;
  29.165 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  29.166 +    outptr = output_data[outrow];
  29.167 +    for (outcol = 0, outcol_h = 0; outcol < output_cols;
  29.168 +	 outcol++, outcol_h += h_expand) {
  29.169 +      outvalue = 0;
  29.170 +      for (v = 0; v < v_expand; v++) {
  29.171 +	inptr = input_data[inrow+v] + outcol_h;
  29.172 +	for (h = 0; h < h_expand; h++) {
  29.173 +	  outvalue += (INT32) GETJSAMPLE(*inptr++);
  29.174 +	}
  29.175 +      }
  29.176 +      *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
  29.177 +    }
  29.178 +    inrow += v_expand;
  29.179 +  }
  29.180 +}
  29.181 +
  29.182 +
  29.183 +/*
  29.184 + * Downsample pixel values of a single component.
  29.185 + * This version handles the special case of a full-size component,
  29.186 + * without smoothing.
  29.187 + */
  29.188 +
  29.189 +METHODDEF(void)
  29.190 +fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  29.191 +		     JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.192 +{
  29.193 +  /* Copy the data */
  29.194 +  jcopy_sample_rows(input_data, 0, output_data, 0,
  29.195 +		    cinfo->max_v_samp_factor, cinfo->image_width);
  29.196 +  /* Edge-expand */
  29.197 +  expand_right_edge(output_data, cinfo->max_v_samp_factor,
  29.198 +		    cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
  29.199 +}
  29.200 +
  29.201 +
  29.202 +/*
  29.203 + * Downsample pixel values of a single component.
  29.204 + * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  29.205 + * without smoothing.
  29.206 + *
  29.207 + * A note about the "bias" calculations: when rounding fractional values to
  29.208 + * integer, we do not want to always round 0.5 up to the next integer.
  29.209 + * If we did that, we'd introduce a noticeable bias towards larger values.
  29.210 + * Instead, this code is arranged so that 0.5 will be rounded up or down at
  29.211 + * alternate pixel locations (a simple ordered dither pattern).
  29.212 + */
  29.213 +
  29.214 +METHODDEF(void)
  29.215 +h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  29.216 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.217 +{
  29.218 +  int outrow;
  29.219 +  JDIMENSION outcol;
  29.220 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  29.221 +  register JSAMPROW inptr, outptr;
  29.222 +  register int bias;
  29.223 +
  29.224 +  /* Expand input data enough to let all the output samples be generated
  29.225 +   * by the standard loop.  Special-casing padded output would be more
  29.226 +   * efficient.
  29.227 +   */
  29.228 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  29.229 +		    cinfo->image_width, output_cols * 2);
  29.230 +
  29.231 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  29.232 +    outptr = output_data[outrow];
  29.233 +    inptr = input_data[outrow];
  29.234 +    bias = 0;			/* bias = 0,1,0,1,... for successive samples */
  29.235 +    for (outcol = 0; outcol < output_cols; outcol++) {
  29.236 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
  29.237 +			      + bias) >> 1);
  29.238 +      bias ^= 1;		/* 0=>1, 1=>0 */
  29.239 +      inptr += 2;
  29.240 +    }
  29.241 +  }
  29.242 +}
  29.243 +
  29.244 +
  29.245 +/*
  29.246 + * Downsample pixel values of a single component.
  29.247 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  29.248 + * without smoothing.
  29.249 + */
  29.250 +
  29.251 +METHODDEF(void)
  29.252 +h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  29.253 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.254 +{
  29.255 +  int inrow, outrow;
  29.256 +  JDIMENSION outcol;
  29.257 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  29.258 +  register JSAMPROW inptr0, inptr1, outptr;
  29.259 +  register int bias;
  29.260 +
  29.261 +  /* Expand input data enough to let all the output samples be generated
  29.262 +   * by the standard loop.  Special-casing padded output would be more
  29.263 +   * efficient.
  29.264 +   */
  29.265 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  29.266 +		    cinfo->image_width, output_cols * 2);
  29.267 +
  29.268 +  inrow = 0;
  29.269 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  29.270 +    outptr = output_data[outrow];
  29.271 +    inptr0 = input_data[inrow];
  29.272 +    inptr1 = input_data[inrow+1];
  29.273 +    bias = 1;			/* bias = 1,2,1,2,... for successive samples */
  29.274 +    for (outcol = 0; outcol < output_cols; outcol++) {
  29.275 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  29.276 +			      GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
  29.277 +			      + bias) >> 2);
  29.278 +      bias ^= 3;		/* 1=>2, 2=>1 */
  29.279 +      inptr0 += 2; inptr1 += 2;
  29.280 +    }
  29.281 +    inrow += 2;
  29.282 +  }
  29.283 +}
  29.284 +
  29.285 +
  29.286 +#ifdef INPUT_SMOOTHING_SUPPORTED
  29.287 +
  29.288 +/*
  29.289 + * Downsample pixel values of a single component.
  29.290 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  29.291 + * with smoothing.  One row of context is required.
  29.292 + */
  29.293 +
  29.294 +METHODDEF(void)
  29.295 +h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  29.296 +			JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.297 +{
  29.298 +  int inrow, outrow;
  29.299 +  JDIMENSION colctr;
  29.300 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  29.301 +  register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  29.302 +  INT32 membersum, neighsum, memberscale, neighscale;
  29.303 +
  29.304 +  /* Expand input data enough to let all the output samples be generated
  29.305 +   * by the standard loop.  Special-casing padded output would be more
  29.306 +   * efficient.
  29.307 +   */
  29.308 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  29.309 +		    cinfo->image_width, output_cols * 2);
  29.310 +
  29.311 +  /* We don't bother to form the individual "smoothed" input pixel values;
  29.312 +   * we can directly compute the output which is the average of the four
  29.313 +   * smoothed values.  Each of the four member pixels contributes a fraction
  29.314 +   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
  29.315 +   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
  29.316 +   * output.  The four corner-adjacent neighbor pixels contribute a fraction
  29.317 +   * SF to just one smoothed pixel, or SF/4 to the final output; while the
  29.318 +   * eight edge-adjacent neighbors contribute SF to each of two smoothed
  29.319 +   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
  29.320 +   * factors are scaled by 2^16 = 65536.
  29.321 +   * Also recall that SF = smoothing_factor / 1024.
  29.322 +   */
  29.323 +
  29.324 +  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
  29.325 +  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
  29.326 +
  29.327 +  inrow = 0;
  29.328 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  29.329 +    outptr = output_data[outrow];
  29.330 +    inptr0 = input_data[inrow];
  29.331 +    inptr1 = input_data[inrow+1];
  29.332 +    above_ptr = input_data[inrow-1];
  29.333 +    below_ptr = input_data[inrow+2];
  29.334 +
  29.335 +    /* Special case for first column: pretend column -1 is same as column 0 */
  29.336 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  29.337 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  29.338 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  29.339 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  29.340 +	       GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
  29.341 +	       GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
  29.342 +    neighsum += neighsum;
  29.343 +    neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
  29.344 +		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
  29.345 +    membersum = membersum * memberscale + neighsum * neighscale;
  29.346 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  29.347 +    inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  29.348 +
  29.349 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  29.350 +      /* sum of pixels directly mapped to this output element */
  29.351 +      membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  29.352 +		  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  29.353 +      /* sum of edge-neighbor pixels */
  29.354 +      neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  29.355 +		 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  29.356 +		 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
  29.357 +		 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
  29.358 +      /* The edge-neighbors count twice as much as corner-neighbors */
  29.359 +      neighsum += neighsum;
  29.360 +      /* Add in the corner-neighbors */
  29.361 +      neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
  29.362 +		  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
  29.363 +      /* form final output scaled up by 2^16 */
  29.364 +      membersum = membersum * memberscale + neighsum * neighscale;
  29.365 +      /* round, descale and output it */
  29.366 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  29.367 +      inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  29.368 +    }
  29.369 +
  29.370 +    /* Special case for last column */
  29.371 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  29.372 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  29.373 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  29.374 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  29.375 +	       GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
  29.376 +	       GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
  29.377 +    neighsum += neighsum;
  29.378 +    neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
  29.379 +		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
  29.380 +    membersum = membersum * memberscale + neighsum * neighscale;
  29.381 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  29.382 +
  29.383 +    inrow += 2;
  29.384 +  }
  29.385 +}
  29.386 +
  29.387 +
  29.388 +/*
  29.389 + * Downsample pixel values of a single component.
  29.390 + * This version handles the special case of a full-size component,
  29.391 + * with smoothing.  One row of context is required.
  29.392 + */
  29.393 +
  29.394 +METHODDEF(void)
  29.395 +fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
  29.396 +			    JSAMPARRAY input_data, JSAMPARRAY output_data)
  29.397 +{
  29.398 +  int outrow;
  29.399 +  JDIMENSION colctr;
  29.400 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  29.401 +  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  29.402 +  INT32 membersum, neighsum, memberscale, neighscale;
  29.403 +  int colsum, lastcolsum, nextcolsum;
  29.404 +
  29.405 +  /* Expand input data enough to let all the output samples be generated
  29.406 +   * by the standard loop.  Special-casing padded output would be more
  29.407 +   * efficient.
  29.408 +   */
  29.409 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  29.410 +		    cinfo->image_width, output_cols);
  29.411 +
  29.412 +  /* Each of the eight neighbor pixels contributes a fraction SF to the
  29.413 +   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
  29.414 +   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
  29.415 +   * Also recall that SF = smoothing_factor / 1024.
  29.416 +   */
  29.417 +
  29.418 +  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  29.419 +  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
  29.420 +
  29.421 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  29.422 +    outptr = output_data[outrow];
  29.423 +    inptr = input_data[outrow];
  29.424 +    above_ptr = input_data[outrow-1];
  29.425 +    below_ptr = input_data[outrow+1];
  29.426 +
  29.427 +    /* Special case for first column */
  29.428 +    colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
  29.429 +	     GETJSAMPLE(*inptr);
  29.430 +    membersum = GETJSAMPLE(*inptr++);
  29.431 +    nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  29.432 +		 GETJSAMPLE(*inptr);
  29.433 +    neighsum = colsum + (colsum - membersum) + nextcolsum;
  29.434 +    membersum = membersum * memberscale + neighsum * neighscale;
  29.435 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  29.436 +    lastcolsum = colsum; colsum = nextcolsum;
  29.437 +
  29.438 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  29.439 +      membersum = GETJSAMPLE(*inptr++);
  29.440 +      above_ptr++; below_ptr++;
  29.441 +      nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  29.442 +		   GETJSAMPLE(*inptr);
  29.443 +      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
  29.444 +      membersum = membersum * memberscale + neighsum * neighscale;
  29.445 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  29.446 +      lastcolsum = colsum; colsum = nextcolsum;
  29.447 +    }
  29.448 +
  29.449 +    /* Special case for last column */
  29.450 +    membersum = GETJSAMPLE(*inptr);
  29.451 +    neighsum = lastcolsum + (colsum - membersum) + colsum;
  29.452 +    membersum = membersum * memberscale + neighsum * neighscale;
  29.453 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  29.454 +
  29.455 +  }
  29.456 +}
  29.457 +
  29.458 +#endif /* INPUT_SMOOTHING_SUPPORTED */
  29.459 +
  29.460 +
  29.461 +/*
  29.462 + * Module initialization routine for downsampling.
  29.463 + * Note that we must select a routine for each component.
  29.464 + */
  29.465 +
  29.466 +GLOBAL(void)
  29.467 +jinit_downsampler (j_compress_ptr cinfo)
  29.468 +{
  29.469 +  my_downsample_ptr downsample;
  29.470 +  int ci;
  29.471 +  jpeg_component_info * compptr;
  29.472 +  boolean smoothok = TRUE;
  29.473 +
  29.474 +  downsample = (my_downsample_ptr)
  29.475 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  29.476 +				SIZEOF(my_downsampler));
  29.477 +  cinfo->downsample = (struct jpeg_downsampler *) downsample;
  29.478 +  downsample->pub.start_pass = start_pass_downsample;
  29.479 +  downsample->pub.downsample = sep_downsample;
  29.480 +  downsample->pub.need_context_rows = FALSE;
  29.481 +
  29.482 +  if (cinfo->CCIR601_sampling)
  29.483 +    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  29.484 +
  29.485 +  /* Verify we can handle the sampling factors, and set up method pointers */
  29.486 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  29.487 +       ci++, compptr++) {
  29.488 +    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  29.489 +	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  29.490 +#ifdef INPUT_SMOOTHING_SUPPORTED
  29.491 +      if (cinfo->smoothing_factor) {
  29.492 +	downsample->methods[ci] = fullsize_smooth_downsample;
  29.493 +	downsample->pub.need_context_rows = TRUE;
  29.494 +      } else
  29.495 +#endif
  29.496 +	downsample->methods[ci] = fullsize_downsample;
  29.497 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  29.498 +	       compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  29.499 +      smoothok = FALSE;
  29.500 +      downsample->methods[ci] = h2v1_downsample;
  29.501 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  29.502 +	       compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
  29.503 +#ifdef INPUT_SMOOTHING_SUPPORTED
  29.504 +      if (cinfo->smoothing_factor) {
  29.505 +	downsample->methods[ci] = h2v2_smooth_downsample;
  29.506 +	downsample->pub.need_context_rows = TRUE;
  29.507 +      } else
  29.508 +#endif
  29.509 +	downsample->methods[ci] = h2v2_downsample;
  29.510 +    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  29.511 +	       (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
  29.512 +      smoothok = FALSE;
  29.513 +      downsample->methods[ci] = int_downsample;
  29.514 +    } else
  29.515 +      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  29.516 +  }
  29.517 +
  29.518 +#ifdef INPUT_SMOOTHING_SUPPORTED
  29.519 +  if (cinfo->smoothing_factor && !smoothok)
  29.520 +    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
  29.521 +#endif
  29.522 +}
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/libs/libjpeg/jctrans.c	Thu Sep 08 06:28:38 2011 +0300
    30.3 @@ -0,0 +1,388 @@
    30.4 +/*
    30.5 + * jctrans.c
    30.6 + *
    30.7 + * Copyright (C) 1995-1998, Thomas G. Lane.
    30.8 + * This file is part of the Independent JPEG Group's software.
    30.9 + * For conditions of distribution and use, see the accompanying README file.
   30.10 + *
   30.11 + * This file contains library routines for transcoding compression,
   30.12 + * that is, writing raw DCT coefficient arrays to an output JPEG file.
   30.13 + * The routines in jcapimin.c will also be needed by a transcoder.
   30.14 + */
   30.15 +
   30.16 +#define JPEG_INTERNALS
   30.17 +#include "jinclude.h"
   30.18 +#include "jpeglib.h"
   30.19 +
   30.20 +
   30.21 +/* Forward declarations */
   30.22 +LOCAL(void) transencode_master_selection
   30.23 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   30.24 +LOCAL(void) transencode_coef_controller
   30.25 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   30.26 +
   30.27 +
   30.28 +/*
   30.29 + * Compression initialization for writing raw-coefficient data.
   30.30 + * Before calling this, all parameters and a data destination must be set up.
   30.31 + * Call jpeg_finish_compress() to actually write the data.
   30.32 + *
   30.33 + * The number of passed virtual arrays must match cinfo->num_components.
   30.34 + * Note that the virtual arrays need not be filled or even realized at
   30.35 + * the time write_coefficients is called; indeed, if the virtual arrays
   30.36 + * were requested from this compression object's memory manager, they
   30.37 + * typically will be realized during this routine and filled afterwards.
   30.38 + */
   30.39 +
   30.40 +GLOBAL(void)
   30.41 +jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
   30.42 +{
   30.43 +  if (cinfo->global_state != CSTATE_START)
   30.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   30.45 +  /* Mark all tables to be written */
   30.46 +  jpeg_suppress_tables(cinfo, FALSE);
   30.47 +  /* (Re)initialize error mgr and destination modules */
   30.48 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   30.49 +  (*cinfo->dest->init_destination) (cinfo);
   30.50 +  /* Perform master selection of active modules */
   30.51 +  transencode_master_selection(cinfo, coef_arrays);
   30.52 +  /* Wait for jpeg_finish_compress() call */
   30.53 +  cinfo->next_scanline = 0;	/* so jpeg_write_marker works */
   30.54 +  cinfo->global_state = CSTATE_WRCOEFS;
   30.55 +}
   30.56 +
   30.57 +
   30.58 +/*
   30.59 + * Initialize the compression object with default parameters,
   30.60 + * then copy from the source object all parameters needed for lossless
   30.61 + * transcoding.  Parameters that can be varied without loss (such as
   30.62 + * scan script and Huffman optimization) are left in their default states.
   30.63 + */
   30.64 +
   30.65 +GLOBAL(void)
   30.66 +jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
   30.67 +			       j_compress_ptr dstinfo)
   30.68 +{
   30.69 +  JQUANT_TBL ** qtblptr;
   30.70 +  jpeg_component_info *incomp, *outcomp;
   30.71 +  JQUANT_TBL *c_quant, *slot_quant;
   30.72 +  int tblno, ci, coefi;
   30.73 +
   30.74 +  /* Safety check to ensure start_compress not called yet. */
   30.75 +  if (dstinfo->global_state != CSTATE_START)
   30.76 +    ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
   30.77 +  /* Copy fundamental image dimensions */
   30.78 +  dstinfo->image_width = srcinfo->image_width;
   30.79 +  dstinfo->image_height = srcinfo->image_height;
   30.80 +  dstinfo->input_components = srcinfo->num_components;
   30.81 +  dstinfo->in_color_space = srcinfo->jpeg_color_space;
   30.82 +  /* Initialize all parameters to default values */
   30.83 +  jpeg_set_defaults(dstinfo);
   30.84 +  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
   30.85 +   * Fix it to get the right header markers for the image colorspace.
   30.86 +   */
   30.87 +  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
   30.88 +  dstinfo->data_precision = srcinfo->data_precision;
   30.89 +  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
   30.90 +  /* Copy the source's quantization tables. */
   30.91 +  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
   30.92 +    if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
   30.93 +      qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
   30.94 +      if (*qtblptr == NULL)
   30.95 +	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
   30.96 +      MEMCOPY((*qtblptr)->quantval,
   30.97 +	      srcinfo->quant_tbl_ptrs[tblno]->quantval,
   30.98 +	      SIZEOF((*qtblptr)->quantval));
   30.99 +      (*qtblptr)->sent_table = FALSE;
  30.100 +    }
  30.101 +  }
  30.102 +  /* Copy the source's per-component info.
  30.103 +   * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
  30.104 +   */
  30.105 +  dstinfo->num_components = srcinfo->num_components;
  30.106 +  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
  30.107 +    ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
  30.108 +	     MAX_COMPONENTS);
  30.109 +  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
  30.110 +       ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
  30.111 +    outcomp->component_id = incomp->component_id;
  30.112 +    outcomp->h_samp_factor = incomp->h_samp_factor;
  30.113 +    outcomp->v_samp_factor = incomp->v_samp_factor;
  30.114 +    outcomp->quant_tbl_no = incomp->quant_tbl_no;
  30.115 +    /* Make sure saved quantization table for component matches the qtable
  30.116 +     * slot.  If not, the input file re-used this qtable slot.
  30.117 +     * IJG encoder currently cannot duplicate this.
  30.118 +     */
  30.119 +    tblno = outcomp->quant_tbl_no;
  30.120 +    if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
  30.121 +	srcinfo->quant_tbl_ptrs[tblno] == NULL)
  30.122 +      ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
  30.123 +    slot_quant = srcinfo->quant_tbl_ptrs[tblno];
  30.124 +    c_quant = incomp->quant_table;
  30.125 +    if (c_quant != NULL) {
  30.126 +      for (coefi = 0; coefi < DCTSIZE2; coefi++) {
  30.127 +	if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
  30.128 +	  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
  30.129 +      }
  30.130 +    }
  30.131 +    /* Note: we do not copy the source's Huffman table assignments;
  30.132 +     * instead we rely on jpeg_set_colorspace to have made a suitable choice.
  30.133 +     */
  30.134 +  }
  30.135 +  /* Also copy JFIF version and resolution information, if available.
  30.136 +   * Strictly speaking this isn't "critical" info, but it's nearly
  30.137 +   * always appropriate to copy it if available.  In particular,
  30.138 +   * if the application chooses to copy JFIF 1.02 extension markers from
  30.139 +   * the source file, we need to copy the version to make sure we don't
  30.140 +   * emit a file that has 1.02 extensions but a claimed version of 1.01.
  30.141 +   * We will *not*, however, copy version info from mislabeled "2.01" files.
  30.142 +   */
  30.143 +  if (srcinfo->saw_JFIF_marker) {
  30.144 +    if (srcinfo->JFIF_major_version == 1) {
  30.145 +      dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
  30.146 +      dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
  30.147 +    }
  30.148 +    dstinfo->density_unit = srcinfo->density_unit;
  30.149 +    dstinfo->X_density = srcinfo->X_density;
  30.150 +    dstinfo->Y_density = srcinfo->Y_density;
  30.151 +  }
  30.152 +}
  30.153 +
  30.154 +
  30.155 +/*
  30.156 + * Master selection of compression modules for transcoding.
  30.157 + * This substitutes for jcinit.c's initialization of the full compressor.
  30.158 + */
  30.159 +
  30.160 +LOCAL(void)
  30.161 +transencode_master_selection (j_compress_ptr cinfo,
  30.162 +			      jvirt_barray_ptr * coef_arrays)
  30.163 +{
  30.164 +  /* Although we don't actually use input_components for transcoding,
  30.165 +   * jcmaster.c's initial_setup will complain if input_components is 0.
  30.166 +   */
  30.167 +  cinfo->input_components = 1;
  30.168 +  /* Initialize master control (includes parameter checking/processing) */
  30.169 +  jinit_c_master_control(cinfo, TRUE /* transcode only */);
  30.170 +
  30.171 +  /* Entropy encoding: either Huffman or arithmetic coding. */
  30.172 +  if (cinfo->arith_code) {
  30.173 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  30.174 +  } else {
  30.175 +    if (cinfo->progressive_mode) {
  30.176 +#ifdef C_PROGRESSIVE_SUPPORTED
  30.177 +      jinit_phuff_encoder(cinfo);
  30.178 +#else
  30.179 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  30.180 +#endif
  30.181 +    } else
  30.182 +      jinit_huff_encoder(cinfo);
  30.183 +  }
  30.184 +
  30.185 +  /* We need a special coefficient buffer controller. */
  30.186 +  transencode_coef_controller(cinfo, coef_arrays);
  30.187 +
  30.188 +  jinit_marker_writer(cinfo);
  30.189 +
  30.190 +  /* We can now tell the memory manager to allocate virtual arrays. */
  30.191 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  30.192 +
  30.193 +  /* Write the datastream header (SOI, JFIF) immediately.
  30.194 +   * Frame and scan headers are postponed till later.
  30.195 +   * This lets application insert special markers after the SOI.
  30.196 +   */
  30.197 +  (*cinfo->marker->write_file_header) (cinfo);
  30.198 +}
  30.199 +
  30.200 +
  30.201 +/*
  30.202 + * The rest of this file is a special implementation of the coefficient
  30.203 + * buffer controller.  This is similar to jccoefct.c, but it handles only
  30.204 + * output from presupplied virtual arrays.  Furthermore, we generate any
  30.205 + * dummy padding blocks on-the-fly rather than expecting them to be present
  30.206 + * in the arrays.
  30.207 + */
  30.208 +
  30.209 +/* Private buffer controller object */
  30.210 +
  30.211 +typedef struct {
  30.212 +  struct jpeg_c_coef_controller pub; /* public fields */
  30.213 +
  30.214 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
  30.215 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
  30.216 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  30.217 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
  30.218 +
  30.219 +  /* Virtual block array for each component. */
  30.220 +  jvirt_barray_ptr * whole_image;
  30.221 +
  30.222 +  /* Workspace for constructing dummy blocks at right/bottom edges. */
  30.223 +  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
  30.224 +} my_coef_controller;
  30.225 +
  30.226 +typedef my_coef_controller * my_coef_ptr;
  30.227 +
  30.228 +
  30.229 +LOCAL(void)
  30.230 +start_iMCU_row (j_compress_ptr cinfo)
  30.231 +/* Reset within-iMCU-row counters for a new row */
  30.232 +{
  30.233 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  30.234 +
  30.235 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
  30.236 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  30.237 +   * But at the bottom of the image, process only what's left.
  30.238 +   */
  30.239 +  if (cinfo->comps_in_scan > 1) {
  30.240 +    coef->MCU_rows_per_iMCU_row = 1;
  30.241 +  } else {
  30.242 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  30.243 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  30.244 +    else
  30.245 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  30.246 +  }
  30.247 +
  30.248 +  coef->mcu_ctr = 0;
  30.249 +  coef->MCU_vert_offset = 0;
  30.250 +}
  30.251 +
  30.252 +
  30.253 +/*
  30.254 + * Initialize for a processing pass.
  30.255 + */
  30.256 +
  30.257 +METHODDEF(void)
  30.258 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  30.259 +{
  30.260 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  30.261 +
  30.262 +  if (pass_mode != JBUF_CRANK_DEST)
  30.263 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  30.264 +
  30.265 +  coef->iMCU_row_num = 0;
  30.266 +  start_iMCU_row(cinfo);
  30.267 +}
  30.268 +
  30.269 +
  30.270 +/*
  30.271 + * Process some data.
  30.272 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  30.273 + * per call, ie, v_samp_factor block rows for each component in the scan.
  30.274 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  30.275 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  30.276 + *
  30.277 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  30.278 + */
  30.279 +
  30.280 +METHODDEF(boolean)
  30.281 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  30.282 +{
  30.283 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  30.284 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  30.285 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  30.286 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  30.287 +  int blkn, ci, xindex, yindex, yoffset, blockcnt;
  30.288 +  JDIMENSION start_col;
  30.289 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  30.290 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  30.291 +  JBLOCKROW buffer_ptr;
  30.292 +  jpeg_component_info *compptr;
  30.293 +
  30.294 +  /* Align the virtual buffers for the components used in this scan. */
  30.295 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  30.296 +    compptr = cinfo->cur_comp_info[ci];
  30.297 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  30.298 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  30.299 +       coef->iMCU_row_num * compptr->v_samp_factor,
  30.300 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  30.301 +  }
  30.302 +
  30.303 +  /* Loop to process one whole iMCU row */
  30.304 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  30.305 +       yoffset++) {
  30.306 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  30.307 +	 MCU_col_num++) {
  30.308 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  30.309 +      blkn = 0;			/* index of current DCT block within MCU */
  30.310 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  30.311 +	compptr = cinfo->cur_comp_info[ci];
  30.312 +	start_col = MCU_col_num * compptr->MCU_width;
  30.313 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  30.314 +						: compptr->last_col_width;
  30.315 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  30.316 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  30.317 +	      yindex+yoffset < compptr->last_row_height) {
  30.318 +	    /* Fill in pointers to real blocks in this row */
  30.319 +	    buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  30.320 +	    for (xindex = 0; xindex < blockcnt; xindex++)
  30.321 +	      MCU_buffer[blkn++] = buffer_ptr++;
  30.322 +	  } else {
  30.323 +	    /* At bottom of image, need a whole row of dummy blocks */
  30.324 +	    xindex = 0;
  30.325 +	  }
  30.326 +	  /* Fill in any dummy blocks needed in this row.
  30.327 +	   * Dummy blocks are filled in the same way as in jccoefct.c:
  30.328 +	   * all zeroes in the AC entries, DC entries equal to previous
  30.329 +	   * block's DC value.  The init routine has already zeroed the
  30.330 +	   * AC entries, so we need only set the DC entries correctly.
  30.331 +	   */
  30.332 +	  for (; xindex < compptr->MCU_width; xindex++) {
  30.333 +	    MCU_buffer[blkn] = coef->dummy_buffer[blkn];
  30.334 +	    MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
  30.335 +	    blkn++;
  30.336 +	  }
  30.337 +	}
  30.338 +      }
  30.339 +      /* Try to write the MCU. */
  30.340 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
  30.341 +	/* Suspension forced; update state counters and exit */
  30.342 +	coef->MCU_vert_offset = yoffset;
  30.343 +	coef->mcu_ctr = MCU_col_num;
  30.344 +	return FALSE;
  30.345 +      }
  30.346 +    }
  30.347 +    /* Completed an MCU row, but perhaps not an iMCU row */
  30.348 +    coef->mcu_ctr = 0;
  30.349 +  }
  30.350 +  /* Completed the iMCU row, advance counters for next one */
  30.351 +  coef->iMCU_row_num++;
  30.352 +  start_iMCU_row(cinfo);
  30.353 +  return TRUE;
  30.354 +}
  30.355 +
  30.356 +
  30.357 +/*
  30.358 + * Initialize coefficient buffer controller.
  30.359 + *
  30.360 + * Each passed coefficient array must be the right size for that
  30.361 + * coefficient: width_in_blocks wide and height_in_blocks high,
  30.362 + * with unitheight at least v_samp_factor.
  30.363 + */
  30.364 +
  30.365 +LOCAL(void)
  30.366 +transencode_coef_controller (j_compress_ptr cinfo,
  30.367 +			     jvirt_barray_ptr * coef_arrays)
  30.368 +{
  30.369 +  my_coef_ptr coef;
  30.370 +  JBLOCKROW buffer;
  30.371 +  int i;
  30.372 +
  30.373 +  coef = (my_coef_ptr)
  30.374 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  30.375 +				SIZEOF(my_coef_controller));
  30.376 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  30.377 +  coef->pub.start_pass = start_pass_coef;
  30.378 +  coef->pub.compress_data = compress_output;
  30.379 +
  30.380 +  /* Save pointer to virtual arrays */
  30.381 +  coef->whole_image = coef_arrays;
  30.382 +
  30.383 +  /* Allocate and pre-zero space for dummy DCT blocks. */
  30.384 +  buffer = (JBLOCKROW)
  30.385 +    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  30.386 +				C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  30.387 +  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  30.388 +  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  30.389 +    coef->dummy_buffer[i] = buffer + i;
  30.390 +  }
  30.391 +}
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/libs/libjpeg/jdapimin.c	Thu Sep 08 06:28:38 2011 +0300
    31.3 @@ -0,0 +1,395 @@
    31.4 +/*
    31.5 + * jdapimin.c
    31.6 + *
    31.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    31.8 + * This file is part of the Independent JPEG Group's software.
    31.9 + * For conditions of distribution and use, see the accompanying README file.
   31.10 + *
   31.11 + * This file contains application interface code for the decompression half
   31.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   31.13 + * needed in either the normal full-decompression case or the
   31.14 + * transcoding-only case.
   31.15 + *
   31.16 + * Most of the routines intended to be called directly by an application
   31.17 + * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
   31.18 + * shared by compression and decompression, and jdtrans.c for the transcoding
   31.19 + * case.
   31.20 + */
   31.21 +
   31.22 +#define JPEG_INTERNALS
   31.23 +#include "jinclude.h"
   31.24 +#include "jpeglib.h"
   31.25 +
   31.26 +
   31.27 +/*
   31.28 + * Initialization of a JPEG decompression object.
   31.29 + * The error manager must already be set up (in case memory manager fails).
   31.30 + */
   31.31 +
   31.32 +GLOBAL(void)
   31.33 +jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
   31.34 +{
   31.35 +  int i;
   31.36 +
   31.37 +  /* Guard against version mismatches between library and caller. */
   31.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   31.39 +  if (version != JPEG_LIB_VERSION)
   31.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   31.41 +  if (structsize != SIZEOF(struct jpeg_decompress_struct))
   31.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   31.43 +	     (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
   31.44 +
   31.45 +  /* For debugging purposes, we zero the whole master structure.
   31.46 +   * But the application has already set the err pointer, and may have set
   31.47 +   * client_data, so we have to save and restore those fields.
   31.48 +   * Note: if application hasn't set client_data, tools like Purify may
   31.49 +   * complain here.
   31.50 +   */
   31.51 +  {
   31.52 +    struct jpeg_error_mgr * err = cinfo->err;
   31.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   31.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
   31.55 +    cinfo->err = err;
   31.56 +    cinfo->client_data = client_data;
   31.57 +  }
   31.58 +  cinfo->is_decompressor = TRUE;
   31.59 +
   31.60 +  /* Initialize a memory manager instance for this object */
   31.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   31.62 +
   31.63 +  /* Zero out pointers to permanent structures. */
   31.64 +  cinfo->progress = NULL;
   31.65 +  cinfo->src = NULL;
   31.66 +
   31.67 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   31.68 +    cinfo->quant_tbl_ptrs[i] = NULL;
   31.69 +
   31.70 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   31.71 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   31.72 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   31.73 +  }
   31.74 +
   31.75 +  /* Initialize marker processor so application can override methods
   31.76 +   * for COM, APPn markers before calling jpeg_read_header.
   31.77 +   */
   31.78 +  cinfo->marker_list = NULL;
   31.79 +  jinit_marker_reader(cinfo);
   31.80 +
   31.81 +  /* And initialize the overall input controller. */
   31.82 +  jinit_input_controller(cinfo);
   31.83 +
   31.84 +  /* OK, I'm ready */
   31.85 +  cinfo->global_state = DSTATE_START;
   31.86 +}
   31.87 +
   31.88 +
   31.89 +/*
   31.90 + * Destruction of a JPEG decompression object
   31.91 + */
   31.92 +
   31.93 +GLOBAL(void)
   31.94 +jpeg_destroy_decompress (j_decompress_ptr cinfo)
   31.95 +{
   31.96 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   31.97 +}
   31.98 +
   31.99 +
  31.100 +/*
  31.101 + * Abort processing of a JPEG decompression operation,
  31.102 + * but don't destroy the object itself.
  31.103 + */
  31.104 +
  31.105 +GLOBAL(void)
  31.106 +jpeg_abort_decompress (j_decompress_ptr cinfo)
  31.107 +{
  31.108 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  31.109 +}
  31.110 +
  31.111 +
  31.112 +/*
  31.113 + * Set default decompression parameters.
  31.114 + */
  31.115 +
  31.116 +LOCAL(void)
  31.117 +default_decompress_parms (j_decompress_ptr cinfo)
  31.118 +{
  31.119 +  /* Guess the input colorspace, and set output colorspace accordingly. */
  31.120 +  /* (Wish JPEG committee had provided a real way to specify this...) */
  31.121 +  /* Note application may override our guesses. */
  31.122 +  switch (cinfo->num_components) {
  31.123 +  case 1:
  31.124 +    cinfo->jpeg_color_space = JCS_GRAYSCALE;
  31.125 +    cinfo->out_color_space = JCS_GRAYSCALE;
  31.126 +    break;
  31.127 +    
  31.128 +  case 3:
  31.129 +    if (cinfo->saw_JFIF_marker) {
  31.130 +      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  31.131 +    } else if (cinfo->saw_Adobe_marker) {
  31.132 +      switch (cinfo->Adobe_transform) {
  31.133 +      case 0:
  31.134 +	cinfo->jpeg_color_space = JCS_RGB;
  31.135 +	break;
  31.136 +      case 1:
  31.137 +	cinfo->jpeg_color_space = JCS_YCbCr;
  31.138 +	break;
  31.139 +      default:
  31.140 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  31.141 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  31.142 +	break;
  31.143 +      }
  31.144 +    } else {
  31.145 +      /* Saw no special markers, try to guess from the component IDs */
  31.146 +      int cid0 = cinfo->comp_info[0].component_id;
  31.147 +      int cid1 = cinfo->comp_info[1].component_id;
  31.148 +      int cid2 = cinfo->comp_info[2].component_id;
  31.149 +
  31.150 +      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  31.151 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  31.152 +      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  31.153 +	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  31.154 +      else {
  31.155 +	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  31.156 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  31.157 +      }
  31.158 +    }
  31.159 +    /* Always guess RGB is proper output colorspace. */
  31.160 +    cinfo->out_color_space = JCS_RGB;
  31.161 +    break;
  31.162 +    
  31.163 +  case 4:
  31.164 +    if (cinfo->saw_Adobe_marker) {
  31.165 +      switch (cinfo->Adobe_transform) {
  31.166 +      case 0:
  31.167 +	cinfo->jpeg_color_space = JCS_CMYK;
  31.168 +	break;
  31.169 +      case 2:
  31.170 +	cinfo->jpeg_color_space = JCS_YCCK;
  31.171 +	break;
  31.172 +      default:
  31.173 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  31.174 +	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  31.175 +	break;
  31.176 +      }
  31.177 +    } else {
  31.178 +      /* No special markers, assume straight CMYK. */
  31.179 +      cinfo->jpeg_color_space = JCS_CMYK;
  31.180 +    }
  31.181 +    cinfo->out_color_space = JCS_CMYK;
  31.182 +    break;
  31.183 +    
  31.184 +  default:
  31.185 +    cinfo->jpeg_color_space = JCS_UNKNOWN;
  31.186 +    cinfo->out_color_space = JCS_UNKNOWN;
  31.187 +    break;
  31.188 +  }
  31.189 +
  31.190 +  /* Set defaults for other decompression parameters. */
  31.191 +  cinfo->scale_num = 1;		/* 1:1 scaling */
  31.192 +  cinfo->scale_denom = 1;
  31.193 +  cinfo->output_gamma = 1.0;
  31.194 +  cinfo->buffered_image = FALSE;
  31.195 +  cinfo->raw_data_out = FALSE;
  31.196 +  cinfo->dct_method = JDCT_DEFAULT;
  31.197 +  cinfo->do_fancy_upsampling = TRUE;
  31.198 +  cinfo->do_block_smoothing = TRUE;
  31.199 +  cinfo->quantize_colors = FALSE;
  31.200 +  /* We set these in case application only sets quantize_colors. */
  31.201 +  cinfo->dither_mode = JDITHER_FS;
  31.202 +#ifdef QUANT_2PASS_SUPPORTED
  31.203 +  cinfo->two_pass_quantize = TRUE;
  31.204 +#else
  31.205 +  cinfo->two_pass_quantize = FALSE;
  31.206 +#endif
  31.207 +  cinfo->desired_number_of_colors = 256;
  31.208 +  cinfo->colormap = NULL;
  31.209 +  /* Initialize for no mode change in buffered-image mode. */
  31.210 +  cinfo->enable_1pass_quant = FALSE;
  31.211 +  cinfo->enable_external_quant = FALSE;
  31.212 +  cinfo->enable_2pass_quant = FALSE;
  31.213 +}
  31.214 +
  31.215 +
  31.216 +/*
  31.217 + * Decompression startup: read start of JPEG datastream to see what's there.
  31.218 + * Need only initialize JPEG object and supply a data source before calling.
  31.219 + *
  31.220 + * This routine will read as far as the first SOS marker (ie, actual start of
  31.221 + * compressed data), and will save all tables and parameters in the JPEG
  31.222 + * object.  It will also initialize the decompression parameters to default
  31.223 + * values, and finally return JPEG_HEADER_OK.  On return, the application may
  31.224 + * adjust the decompression parameters and then call jpeg_start_decompress.
  31.225 + * (Or, if the application only wanted to determine the image parameters,