Merge lp:~linaro-graphics-wg/glmark2/image-readers into lp:glmark2/2011.11

Proposed by Alexandros Frantzis
Status: Merged
Merged at revision: 226
Proposed branch: lp:~linaro-graphics-wg/glmark2/image-readers
Merge into: lp:glmark2/2011.11
Diff against target: 33152 lines (+32505/-119)
80 files modified
android/jni/Android.mk (+14/-1)
android/jni/Android.ndk.mk (+14/-1)
src/image-reader.cpp (+386/-0)
src/image-reader.h (+77/-0)
src/libjpeg-turbo/README (+290/-0)
src/libjpeg-turbo/README-turbo.txt (+361/-0)
src/libjpeg-turbo/config.h (+137/-0)
src/libjpeg-turbo/jaricom.c (+153/-0)
src/libjpeg-turbo/jcapimin.c (+292/-0)
src/libjpeg-turbo/jcapistd.c (+161/-0)
src/libjpeg-turbo/jcarith.c (+925/-0)
src/libjpeg-turbo/jccoefct.c (+449/-0)
src/libjpeg-turbo/jccolext.c.inc (+114/-0)
src/libjpeg-turbo/jccolor.c (+599/-0)
src/libjpeg-turbo/jcdctmgr.c (+642/-0)
src/libjpeg-turbo/jchuff.c (+1026/-0)
src/libjpeg-turbo/jchuff.h (+47/-0)
src/libjpeg-turbo/jcinit.c (+76/-0)
src/libjpeg-turbo/jcmainct.c (+293/-0)
src/libjpeg-turbo/jcmarker.c (+666/-0)
src/libjpeg-turbo/jcmaster.c (+624/-0)
src/libjpeg-turbo/jcomapi.c (+106/-0)
src/libjpeg-turbo/jconfig.h (+58/-0)
src/libjpeg-turbo/jcparam.c (+649/-0)
src/libjpeg-turbo/jcphuff.c (+831/-0)
src/libjpeg-turbo/jcprepct.c (+354/-0)
src/libjpeg-turbo/jcsample.c (+527/-0)
src/libjpeg-turbo/jctrans.c (+399/-0)
src/libjpeg-turbo/jdapimin.c (+395/-0)
src/libjpeg-turbo/jdapistd.c (+277/-0)
src/libjpeg-turbo/jdarith.c (+761/-0)
src/libjpeg-turbo/jdatadst-tj.c (+188/-0)
src/libjpeg-turbo/jdatasrc-tj.c (+182/-0)
src/libjpeg-turbo/jdcoefct.c (+749/-0)
src/libjpeg-turbo/jdcolext.c.inc (+104/-0)
src/libjpeg-turbo/jdcolor.c (+529/-0)
src/libjpeg-turbo/jdct.h (+184/-0)
src/libjpeg-turbo/jddctmgr.c (+288/-0)
src/libjpeg-turbo/jdhuff.c (+808/-0)
src/libjpeg-turbo/jdhuff.h (+234/-0)
src/libjpeg-turbo/jdinput.c (+471/-0)
src/libjpeg-turbo/jdmainct.c (+514/-0)
src/libjpeg-turbo/jdmarker.c (+1364/-0)
src/libjpeg-turbo/jdmaster.c (+601/-0)
src/libjpeg-turbo/jdmerge.c (+455/-0)
src/libjpeg-turbo/jdmrgext.c.inc (+156/-0)
src/libjpeg-turbo/jdphuff.c (+668/-0)
src/libjpeg-turbo/jdpostct.c (+290/-0)
src/libjpeg-turbo/jdsample.c (+496/-0)
src/libjpeg-turbo/jdtrans.c (+152/-0)
src/libjpeg-turbo/jerror.c (+252/-0)
src/libjpeg-turbo/jerror.h (+314/-0)
src/libjpeg-turbo/jfdctflt.c (+168/-0)
src/libjpeg-turbo/jfdctfst.c (+224/-0)
src/libjpeg-turbo/jfdctint.c (+283/-0)
src/libjpeg-turbo/jidctflt.c (+242/-0)
src/libjpeg-turbo/jidctfst.c (+368/-0)
src/libjpeg-turbo/jidctint.c (+389/-0)
src/libjpeg-turbo/jidctred.c (+398/-0)
src/libjpeg-turbo/jinclude.h (+91/-0)
src/libjpeg-turbo/jmemmgr.c (+1151/-0)
src/libjpeg-turbo/jmemnobs.c (+109/-0)
src/libjpeg-turbo/jmemsys.h (+198/-0)
src/libjpeg-turbo/jmorecfg.h (+404/-0)
src/libjpeg-turbo/jpegcomp.h (+26/-0)
src/libjpeg-turbo/jpegint.h (+401/-0)
src/libjpeg-turbo/jpeglib.h (+1213/-0)
src/libjpeg-turbo/jquant1.c (+860/-0)
src/libjpeg-turbo/jquant2.c (+1293/-0)
src/libjpeg-turbo/jsimd.h (+98/-0)
src/libjpeg-turbo/jsimddct.h (+102/-0)
src/libjpeg-turbo/jutils.c (+179/-0)
src/libjpeg-turbo/jversion.h (+31/-0)
src/libjpeg-turbo/simd/jsimd.h (+666/-0)
src/libjpeg-turbo/simd/jsimd_arm.c (+670/-0)
src/libjpeg-turbo/simd/jsimd_arm_neon.S (+2159/-0)
src/texture.cpp (+58/-106)
src/texture.h (+17/-6)
src/wscript_build (+2/-2)
wscript (+3/-3)
To merge this branch: bzr merge lp:~linaro-graphics-wg/glmark2/image-readers
Reviewer Review Type Date Requested Status
Jesse Barker Approve
Review via email: mp+112294@code.launchpad.net

Description of the change

Add support for loading texture data from JPEG files, plus some refactoring of the image reading infrastructure.

To post a comment you must log in.
Revision history for this message
Jesse Barker (jesse-barker) wrote :

In the function that fills in the "struct jpeg_source_mgr" function pointer for skip_input_data, while I normally object to C casting in C++, it seems especially odd in this case as there's a mix of C and C++ casts in this function. Aside from that, the code looks great.

review: Approve
Revision history for this message
Alexandros Frantzis (afrantzis) wrote :

Thanks, I copied that from the jpeg source code and didn't pay attention to the casts. Fixed in 232.

232. By Alexandros Frantzis

ImageReader: Remove C casts.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'android/jni/Android.mk'
2--- android/jni/Android.mk 2012-06-21 12:57:43 +0000
3+++ android/jni/Android.mk 2012-06-27 16:20:24 +0000
4@@ -24,6 +24,18 @@
5
6 include $(CLEAR_VARS)
7
8+LOCAL_MODULE := libglmark2-jpeg
9+LOCAL_CFLAGS := -Werror -Wall -Wextra -Wno-error=attributes \
10+ -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable
11+LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/libjpeg-turbo/
12+LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.c)) \
13+ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.S)) \
14+ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/*.c))
15+
16+include $(BUILD_STATIC_LIBRARY)
17+
18+include $(CLEAR_VARS)
19+
20 LOCAL_CPP_EXTENSION := .cc
21 LOCAL_MODULE := libglmark2-ideas
22 LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DUSE_GLESv2 -Werror -Wall -Wextra\
23@@ -41,7 +53,7 @@
24
25 LOCAL_MODULE_TAGS := optional
26 LOCAL_MODULE := libglmark2-android
27-LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas
28+LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas libglmark2-jpeg
29 LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DGLMARK_VERSION="\"2012.06\"" \
30 -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \
31 -Wno-error=unused-parameter
32@@ -49,6 +61,7 @@
33 LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \
34 $(LOCAL_PATH)/src/libmatrix \
35 $(LOCAL_PATH)/src/scene-ideas \
36+ $(LOCAL_PATH)/src/libjpeg-turbo \
37 $(LOCAL_PATH)/src/libpng \
38 external/zlib
39 LOCAL_SRC_FILES := $(filter-out src/canvas% src/main.cpp, \
40
41=== modified file 'android/jni/Android.ndk.mk'
42--- android/jni/Android.ndk.mk 2012-06-21 12:57:43 +0000
43+++ android/jni/Android.ndk.mk 2012-06-27 16:20:24 +0000
44@@ -20,6 +20,18 @@
45
46 include $(CLEAR_VARS)
47
48+LOCAL_MODULE := libglmark2-jpeg
49+LOCAL_CFLAGS := -Werror -Wall -Wextra -Wno-error=attributes \
50+ -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable
51+LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/libjpeg-turbo/
52+LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.c)) \
53+ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.S)) \
54+ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/*.c))
55+
56+include $(BUILD_STATIC_LIBRARY)
57+
58+include $(CLEAR_VARS)
59+
60 LOCAL_CPP_EXTENSION := .cc
61 LOCAL_MODULE := libglmark2-ideas
62 LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DUSE_GLESv2 -Werror -Wall -Wextra\
63@@ -34,7 +46,7 @@
64
65 LOCAL_MODULE_TAGS := optional
66 LOCAL_MODULE := libglmark2-android
67-LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas
68+LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas libglmark2-jpeg
69 LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DGLMARK_VERSION="\"2012.06\"" \
70 -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \
71 -Wno-error=unused-parameter
72@@ -42,6 +54,7 @@
73 LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \
74 $(LOCAL_PATH)/src/libmatrix \
75 $(LOCAL_PATH)/src/scene-ideas \
76+ $(LOCAL_PATH)/src/libjpeg-turbo \
77 $(LOCAL_PATH)/src/libpng
78 LOCAL_SRC_FILES := $(filter-out src/canvas% src/main.cpp, \
79 $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/*.cpp))) \
80
81=== added file 'src/image-reader.cpp'
82--- src/image-reader.cpp 1970-01-01 00:00:00 +0000
83+++ src/image-reader.cpp 2012-06-27 16:20:24 +0000
84@@ -0,0 +1,386 @@
85+/*
86+ * Copyright © 2012 Linaro Limited
87+ *
88+ * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
89+ *
90+ * glmark2 is free software: you can redistribute it and/or modify it under the
91+ * terms of the GNU General Public License as published by the Free Software
92+ * Foundation, either version 3 of the License, or (at your option) any later
93+ * version.
94+ *
95+ * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
96+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
97+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
98+ * details.
99+ *
100+ * You should have received a copy of the GNU General Public License along with
101+ * glmark2. If not, see <http://www.gnu.org/licenses/>.
102+ *
103+ * Authors:
104+ * Alexandros Frantzis
105+ */
106+#include <cstdio>
107+#include <png.h>
108+#include <jpeglib.h>
109+#include <memory>
110+
111+#include "image-reader.h"
112+#include "log.h"
113+#include "util.h"
114+
115+/*******
116+ * PNG *
117+ *******/
118+
119+struct PNGReaderPrivate
120+{
121+ PNGReaderPrivate() :
122+ png(0), info(0), rows(0), png_error(0),
123+ current_row(0), row_stride(0) {}
124+
125+ static void png_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
126+ {
127+ std::istream *is = reinterpret_cast<std::istream*>(png_get_io_ptr(png_ptr));
128+ is->read(reinterpret_cast<char *>(data), length);
129+ }
130+
131+ png_structp png;
132+ png_infop info;
133+ png_bytepp rows;
134+ bool png_error;
135+ unsigned int current_row;
136+ unsigned int row_stride;
137+};
138+
139+PNGReader::PNGReader(const std::string& filename):
140+ priv_(new PNGReaderPrivate())
141+{
142+ priv_->png_error = !init(filename);
143+}
144+
145+PNGReader::~PNGReader()
146+{
147+ finish();
148+ delete priv_;
149+}
150+
151+bool
152+PNGReader::error()
153+{
154+ return priv_->png_error;
155+}
156+
157+bool
158+PNGReader::nextRow(unsigned char *dst)
159+{
160+ bool ret;
161+
162+ if (priv_->current_row < height()) {
163+ memcpy(dst, priv_->rows[priv_->current_row], priv_->row_stride);
164+ priv_->current_row++;
165+ ret = true;
166+ }
167+ else {
168+ ret = false;
169+ }
170+
171+ return ret;
172+}
173+
174+unsigned int
175+PNGReader::width() const
176+{
177+ return png_get_image_width(priv_->png, priv_->info);
178+}
179+
180+unsigned int
181+PNGReader::height() const
182+{
183+ return png_get_image_height(priv_->png, priv_->info);
184+}
185+
186+unsigned int
187+PNGReader::pixelBytes() const
188+{
189+ if (png_get_color_type(priv_->png, priv_->info) == PNG_COLOR_TYPE_RGB)
190+ {
191+ return 3;
192+ }
193+ return 4;
194+}
195+
196+
197+bool
198+PNGReader::init(const std::string& filename)
199+{
200+ static const int png_transforms = PNG_TRANSFORM_STRIP_16 |
201+ PNG_TRANSFORM_GRAY_TO_RGB |
202+ PNG_TRANSFORM_PACKING |
203+ PNG_TRANSFORM_EXPAND;
204+
205+ Log::debug("Reading PNG file %s\n", filename.c_str());
206+
207+ const std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename));
208+ if (!(*is_ptr)) {
209+ Log::error("Cannot open file %s!\n", filename.c_str());
210+ return false;
211+ }
212+
213+ /* Set up all the libpng structs we need */
214+ priv_->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
215+ if (!priv_->png) {
216+ Log::error("Couldn't create libpng read struct\n");
217+ return false;
218+ }
219+
220+ priv_->info = png_create_info_struct(priv_->png);
221+ if (!priv_->info) {
222+ Log::error("Couldn't create libpng info struct\n");
223+ return false;
224+ }
225+
226+ /* Set up libpng error handling */
227+ if (setjmp(png_jmpbuf(priv_->png))) {
228+ Log::error("libpng error while reading file %s\n", filename.c_str());
229+ return false;
230+ }
231+
232+ /* Read the image information and data */
233+ png_set_read_fn(priv_->png, reinterpret_cast<voidp>(is_ptr.get()),
234+ PNGReaderPrivate::png_read_fn);
235+
236+ png_read_png(priv_->png, priv_->info, png_transforms, 0);
237+
238+ priv_->rows = png_get_rows(priv_->png, priv_->info);
239+
240+ priv_->current_row = 0;
241+ priv_->row_stride = width() * pixelBytes();
242+
243+ return true;
244+}
245+
246+void
247+PNGReader::finish()
248+{
249+ if (priv_->png)
250+ {
251+ png_destroy_read_struct(&priv_->png, &priv_->info, 0);
252+ }
253+}
254+
255+
256+/********
257+ * JPEG *
258+ ********/
259+
260+struct JPEGErrorMgr
261+{
262+ struct jpeg_error_mgr pub;
263+ jmp_buf jmp_buffer;
264+
265+ JPEGErrorMgr()
266+ {
267+ jpeg_std_error(&pub);
268+ pub.error_exit = error_exit;
269+ }
270+
271+ static void error_exit(j_common_ptr cinfo)
272+ {
273+ JPEGErrorMgr *err =
274+ reinterpret_cast<JPEGErrorMgr *>(cinfo->err);
275+
276+ char buffer[JMSG_LENGTH_MAX];
277+
278+ /* Create the message */
279+ (*cinfo->err->format_message)(cinfo, buffer);
280+ std::string msg(std::string(buffer) + "\n");
281+ Log::error(msg.c_str());
282+
283+ longjmp(err->jmp_buffer, 1);
284+ }
285+};
286+
287+struct JPEGIStreamSourceMgr
288+{
289+ static const int BUFFER_SIZE = 4096;
290+ struct jpeg_source_mgr pub;
291+ std::istream *is;
292+ JOCTET buffer[BUFFER_SIZE];
293+
294+ JPEGIStreamSourceMgr(const std::string& filename) : is(0)
295+ {
296+ is = Util::get_resource(filename);
297+
298+ /* Fill in jpeg_source_mgr pub struct */
299+ pub.init_source = init_source;
300+ pub.fill_input_buffer = fill_input_buffer;
301+ pub.skip_input_data = skip_input_data;
302+ pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
303+ pub.term_source = term_source;
304+ pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
305+ pub.next_input_byte = NULL; /* until buffer loaded */
306+ }
307+
308+ ~JPEGIStreamSourceMgr()
309+ {
310+ delete is;
311+ }
312+
313+ bool error()
314+ {
315+ return !is || (is->fail() && !is->eof());
316+ }
317+
318+ static void init_source(j_decompress_ptr cinfo)
319+ {
320+ static_cast<void>(cinfo);
321+ }
322+
323+ static boolean fill_input_buffer(j_decompress_ptr cinfo)
324+ {
325+ JPEGIStreamSourceMgr *src =
326+ reinterpret_cast<JPEGIStreamSourceMgr *>(cinfo->src);
327+
328+ src->is->read(reinterpret_cast<char *>(src->buffer), BUFFER_SIZE);
329+
330+ src->pub.next_input_byte = src->buffer;
331+ src->pub.bytes_in_buffer = src->is->gcount();
332+
333+ /*
334+ * If the decoder needs more data, but we have no more bytes left to
335+ * read mark the end of input.
336+ */
337+ if (src->pub.bytes_in_buffer == 0) {
338+ src->pub.bytes_in_buffer = 2;
339+ src->buffer[0] = 0xFF;
340+ src->buffer[0] = JPEG_EOI;
341+ }
342+
343+ return TRUE;
344+ }
345+
346+ static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
347+ {
348+ JPEGIStreamSourceMgr *src =
349+ reinterpret_cast<JPEGIStreamSourceMgr *>(cinfo->src);
350+
351+ if (num_bytes > 0) {
352+ size_t n = static_cast<size_t>(num_bytes);
353+ while (n > src->pub.bytes_in_buffer) {
354+ n -= src->pub.bytes_in_buffer;
355+ (*src->fill_input_buffer)(cinfo);
356+ }
357+ src->pub.next_input_byte += n;
358+ src->pub.bytes_in_buffer -= n;
359+ }
360+ }
361+
362+ static void term_source(j_decompress_ptr cinfo)
363+ {
364+ static_cast<void>(cinfo);
365+ }
366+};
367+
368+struct JPEGReaderPrivate
369+{
370+ JPEGReaderPrivate(const std::string& filename) :
371+ source_mgr(filename), jpeg_error(false) {}
372+
373+ struct jpeg_decompress_struct cinfo;
374+ JPEGErrorMgr error_mgr;
375+ JPEGIStreamSourceMgr source_mgr;
376+ bool jpeg_error;
377+};
378+
379+
380+JPEGReader::JPEGReader(const std::string& filename) :
381+ priv_(new JPEGReaderPrivate(filename))
382+{
383+ priv_->jpeg_error = !init(filename);
384+}
385+
386+JPEGReader::~JPEGReader()
387+{
388+ finish();
389+ delete priv_;
390+}
391+
392+bool
393+JPEGReader::error()
394+{
395+ return priv_->jpeg_error || priv_->source_mgr.error();
396+}
397+
398+bool
399+JPEGReader::nextRow(unsigned char *dst)
400+{
401+ bool ret = true;
402+ unsigned char *buffer[1];
403+ buffer[0] = dst;
404+
405+ /* Set up error handling */
406+ if (setjmp(priv_->error_mgr.jmp_buffer)) {
407+ return false;
408+ }
409+
410+ /* While there are lines left, read next line */
411+ if (priv_->cinfo.output_scanline < priv_->cinfo.output_height) {
412+ jpeg_read_scanlines(&priv_->cinfo, buffer, 1);
413+ }
414+ else {
415+ jpeg_finish_decompress(&priv_->cinfo);
416+ ret = false;
417+ }
418+
419+ return ret;
420+}
421+
422+unsigned int
423+JPEGReader::width() const
424+{
425+ return priv_->cinfo.output_width;
426+}
427+
428+unsigned int
429+JPEGReader::height() const
430+{
431+ return priv_->cinfo.output_height;
432+}
433+
434+unsigned int
435+JPEGReader::pixelBytes() const
436+{
437+ return priv_->cinfo.output_components;
438+}
439+
440+bool
441+JPEGReader::init(const std::string& filename)
442+{
443+ Log::debug("Reading JPEG file %s\n", filename.c_str());
444+
445+ /* Initialize error manager */
446+ priv_->cinfo.err = reinterpret_cast<jpeg_error_mgr*>(&priv_->error_mgr);
447+
448+ if (setjmp(priv_->error_mgr.jmp_buffer)) {
449+ return false;
450+ }
451+
452+ jpeg_create_decompress(&priv_->cinfo);
453+ priv_->cinfo.src = reinterpret_cast<jpeg_source_mgr*>(&priv_->source_mgr);
454+
455+ /* Read header */
456+ jpeg_read_header(&priv_->cinfo, TRUE);
457+
458+ jpeg_start_decompress(&priv_->cinfo);
459+
460+ return true;
461+}
462+
463+void
464+JPEGReader::finish()
465+{
466+ jpeg_destroy_decompress(&priv_->cinfo);
467+}
468+
469+
470+
471
472=== added file 'src/image-reader.h'
473--- src/image-reader.h 1970-01-01 00:00:00 +0000
474+++ src/image-reader.h 2012-06-27 16:20:24 +0000
475@@ -0,0 +1,77 @@
476+/*
477+ * Copyright © 2012 Linaro Limited
478+ *
479+ * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
480+ *
481+ * glmark2 is free software: you can redistribute it and/or modify it under the
482+ * terms of the GNU General Public License as published by the Free Software
483+ * Foundation, either version 3 of the License, or (at your option) any later
484+ * version.
485+ *
486+ * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
487+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
488+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
489+ * details.
490+ *
491+ * You should have received a copy of the GNU General Public License along with
492+ * glmark2. If not, see <http://www.gnu.org/licenses/>.
493+ *
494+ * Authors:
495+ * Alexandros Frantzis
496+ */
497+#include <string>
498+
499+class ImageReader
500+{
501+public:
502+ virtual bool error() = 0;
503+ virtual bool nextRow(unsigned char *dst) = 0;
504+ virtual unsigned int width() const = 0;
505+ virtual unsigned int height() const = 0;
506+ virtual unsigned int pixelBytes() const = 0;
507+ virtual ~ImageReader() {}
508+};
509+
510+class PNGReaderPrivate;
511+
512+class PNGReader : public ImageReader
513+{
514+public:
515+ PNGReader(const std::string& filename);
516+
517+ virtual ~PNGReader();
518+ bool error();
519+ bool nextRow(unsigned char *dst);
520+
521+ unsigned int width() const;
522+ unsigned int height() const;
523+ unsigned int pixelBytes() const;
524+
525+private:
526+ bool init(const std::string& filename);
527+ void finish();
528+
529+ PNGReaderPrivate *priv_;
530+};
531+
532+class JPEGReaderPrivate;
533+
534+class JPEGReader : public ImageReader
535+{
536+public:
537+ JPEGReader(const std::string& filename);
538+
539+ virtual ~JPEGReader();
540+ bool error();
541+ bool nextRow(unsigned char *dst);
542+ unsigned int width() const;
543+ unsigned int height() const;
544+ unsigned int pixelBytes() const;
545+
546+private:
547+ bool init(const std::string& filename);
548+ void finish();
549+
550+ JPEGReaderPrivate *priv_;
551+};
552+
553
554=== added directory 'src/libjpeg-turbo'
555=== added file 'src/libjpeg-turbo/README'
556--- src/libjpeg-turbo/README 1970-01-01 00:00:00 +0000
557+++ src/libjpeg-turbo/README 2012-06-27 16:20:24 +0000
558@@ -0,0 +1,290 @@
559+libjpeg-turbo note: This file contains portions of the libjpeg v6b and v8
560+README files, with additional wordsmithing by The libjpeg-turbo Project.
561+It is included only for reference, as some parts of it may not apply to
562+libjpeg-turbo. Please see README-turbo.txt for information specific to
563+libjpeg-turbo.
564+
565+
566+The Independent JPEG Group's JPEG software
567+==========================================
568+
569+This distribution contains a release of the Independent JPEG Group's free JPEG
570+software. You are welcome to redistribute this software and to use it for any
571+purpose, subject to the conditions under LEGAL ISSUES, below.
572+
573+This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone,
574+Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson,
575+Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers,
576+and other members of the Independent JPEG Group.
577+
578+IJG is not affiliated with the official ISO JPEG standards committee.
579+
580+
581+DOCUMENTATION ROADMAP
582+=====================
583+
584+This file contains the following sections:
585+
586+OVERVIEW General description of JPEG and the IJG software.
587+LEGAL ISSUES Copyright, lack of warranty, terms of distribution.
588+REFERENCES Where to learn more about JPEG.
589+ARCHIVE LOCATIONS Where to find newer versions of this software.
590+FILE FORMAT WARS Software *not* to get.
591+TO DO Plans for future IJG releases.
592+
593+Other documentation files in the distribution are:
594+
595+User documentation:
596+ install.txt How to configure and install the IJG software.
597+ usage.txt Usage instructions for cjpeg, djpeg, jpegtran,
598+ rdjpgcom, and wrjpgcom.
599+ *.1 Unix-style man pages for programs (same info as usage.txt).
600+ wizard.txt Advanced usage instructions for JPEG wizards only.
601+ change.log Version-to-version change highlights.
602+Programmer and internal documentation:
603+ libjpeg.txt How to use the JPEG library in your own programs.
604+ example.c Sample code for calling the JPEG library.
605+ structure.txt Overview of the JPEG library's internal structure.
606+ filelist.txt Road map of IJG files.
607+ coderules.txt Coding style rules --- please read if you contribute code.
608+
609+Please read at least the files install.txt and usage.txt. Some information
610+can also be found in the JPEG FAQ (Frequently Asked Questions) article. See
611+ARCHIVE LOCATIONS below to find out where to obtain the FAQ article.
612+
613+If you want to understand how the JPEG code works, we suggest reading one or
614+more of the REFERENCES, then looking at the documentation files (in roughly
615+the order listed) before diving into the code.
616+
617+
618+OVERVIEW
619+========
620+
621+This package contains C software to implement JPEG image encoding, decoding,
622+and transcoding. JPEG (pronounced "jay-peg") is a standardized compression
623+method for full-color and gray-scale images. JPEG's strong suit is compressing
624+photographic images or other types of images that have smooth color and
625+brightness transitions between neighboring pixels. Images with sharp lines or
626+other abrupt features may not compress well with JPEG, and a higher JPEG
627+quality may have to be used to avoid visible compression artifacts with such
628+images.
629+
630+JPEG is lossy, meaning that the output pixels are not necessarily identical to
631+the input pixels. However, on photographic content and other "smooth" images,
632+very good compression ratios can be obtained with no visible compression
633+artifacts, and extremely high compression ratios are possible if you are
634+willing to sacrifice image quality (by reducing the "quality" setting in the
635+compressor.)
636+
637+This software implements JPEG baseline, extended-sequential, and progressive
638+compression processes. Provision is made for supporting all variants of these
639+processes, although some uncommon parameter settings aren't implemented yet.
640+We have made no provision for supporting the hierarchical or lossless
641+processes defined in the standard.
642+
643+We provide a set of library routines for reading and writing JPEG image files,
644+plus two sample applications "cjpeg" and "djpeg", which use the library to
645+perform conversion between JPEG and some other popular image file formats.
646+The library is intended to be reused in other applications.
647+
648+In order to support file conversion and viewing software, we have included
649+considerable functionality beyond the bare JPEG coding/decoding capability;
650+for example, the color quantization modules are not strictly part of JPEG
651+decoding, but they are essential for output to colormapped file formats or
652+colormapped displays. These extra functions can be compiled out of the
653+library if not required for a particular application.
654+
655+We have also included "jpegtran", a utility for lossless transcoding between
656+different JPEG processes, and "rdjpgcom" and "wrjpgcom", two simple
657+applications for inserting and extracting textual comments in JFIF files.
658+
659+The emphasis in designing this software has been on achieving portability and
660+flexibility, while also making it fast enough to be useful. In particular,
661+the software is not intended to be read as a tutorial on JPEG. (See the
662+REFERENCES section for introductory material.) Rather, it is intended to
663+be reliable, portable, industrial-strength code. We do not claim to have
664+achieved that goal in every aspect of the software, but we strive for it.
665+
666+We welcome the use of this software as a component of commercial products.
667+No royalty is required, but we do ask for an acknowledgement in product
668+documentation, as described under LEGAL ISSUES.
669+
670+
671+LEGAL ISSUES
672+============
673+
674+In plain English:
675+
676+1. We don't promise that this software works. (But if you find any bugs,
677+ please let us know!)
678+2. You can use this software for whatever you want. You don't have to pay us.
679+3. You may not pretend that you wrote this software. If you use it in a
680+ program, you must acknowledge somewhere in your documentation that
681+ you've used the IJG code.
682+
683+In legalese:
684+
685+The authors make NO WARRANTY or representation, either express or implied,
686+with respect to this software, its quality, accuracy, merchantability, or
687+fitness for a particular purpose. This software is provided "AS IS", and you,
688+its user, assume the entire risk as to its quality and accuracy.
689+
690+This software is copyright (C) 1991-2010, Thomas G. Lane, Guido Vollbeding.
691+All Rights Reserved except as specified below.
692+
693+Permission is hereby granted to use, copy, modify, and distribute this
694+software (or portions thereof) for any purpose, without fee, subject to these
695+conditions:
696+(1) If any part of the source code for this software is distributed, then this
697+README file must be included, with this copyright and no-warranty notice
698+unaltered; and any additions, deletions, or changes to the original files
699+must be clearly indicated in accompanying documentation.
700+(2) If only executable code is distributed, then the accompanying
701+documentation must state that "this software is based in part on the work of
702+the Independent JPEG Group".
703+(3) Permission for use of this software is granted only if the user accepts
704+full responsibility for any undesirable consequences; the authors accept
705+NO LIABILITY for damages of any kind.
706+
707+These conditions apply to any software derived from or based on the IJG code,
708+not just to the unmodified library. If you use our work, you ought to
709+acknowledge us.
710+
711+Permission is NOT granted for the use of any IJG author's name or company name
712+in advertising or publicity relating to this software or products derived from
713+it. This software may be referred to only as "the Independent JPEG Group's
714+software".
715+
716+We specifically permit and encourage the use of this software as the basis of
717+commercial products, provided that all warranty or liability claims are
718+assumed by the product vendor.
719+
720+
721+ansi2knr.c is included in this distribution by permission of L. Peter Deutsch,
722+sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA.
723+ansi2knr.c is NOT covered by the above copyright and conditions, but instead
724+by the usual distribution terms of the Free Software Foundation; principally,
725+that you must include source code if you redistribute it. (See the file
726+ansi2knr.c for full details.) However, since ansi2knr.c is not needed as part
727+of any program generated from the IJG code, this does not limit you more than
728+the foregoing paragraphs do.
729+
730+The Unix configuration script "configure" was produced with GNU Autoconf.
731+It is copyright by the Free Software Foundation but is freely distributable.
732+The same holds for its supporting scripts (config.guess, config.sub,
733+ltmain.sh). Another support script, install-sh, is copyright by X Consortium
734+but is also freely distributable.
735+
736+The IJG distribution formerly included code to read and write GIF files.
737+To avoid entanglement with the Unisys LZW patent, GIF reading support has
738+been removed altogether, and the GIF writer has been simplified to produce
739+"uncompressed GIFs". This technique does not use the LZW algorithm; the
740+resulting GIF files are larger than usual, but are readable by all standard
741+GIF decoders.
742+
743+We are required to state that
744+ "The Graphics Interchange Format(c) is the Copyright property of
745+ CompuServe Incorporated. GIF(sm) is a Service Mark property of
746+ CompuServe Incorporated."
747+
748+
749+REFERENCES
750+==========
751+
752+We recommend reading one or more of these references before trying to
753+understand the innards of the JPEG software.
754+
755+The best short technical introduction to the JPEG compression algorithm is
756+ Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
757+ Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
758+(Adjacent articles in that issue discuss MPEG motion picture compression,
759+applications of JPEG, and related topics.) If you don't have the CACM issue
760+handy, a PostScript file containing a revised version of Wallace's article is
761+available at http://www.ijg.org/files/wallace.ps.gz. The file (actually
762+a preprint for an article that appeared in IEEE Trans. Consumer Electronics)
763+omits the sample images that appeared in CACM, but it includes corrections
764+and some added material. Note: the Wallace article is copyright ACM and IEEE,
765+and it may not be used for commercial purposes.
766+
767+A somewhat less technical, more leisurely introduction to JPEG can be found in
768+"The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by
769+M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1. This book provides
770+good explanations and example C code for a multitude of compression methods
771+including JPEG. It is an excellent source if you are comfortable reading C
772+code but don't know much about data compression in general. The book's JPEG
773+sample code is far from industrial-strength, but when you are ready to look
774+at a full implementation, you've got one here...
775+
776+The best currently available description of JPEG is the textbook "JPEG Still
777+Image Data Compression Standard" by William B. Pennebaker and Joan L.
778+Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
779+Price US$59.95, 638 pp. The book includes the complete text of the ISO JPEG
780+standards (DIS 10918-1 and draft DIS 10918-2).
781+
782+The original JPEG standard is divided into two parts, Part 1 being the actual
783+specification, while Part 2 covers compliance testing methods. Part 1 is
784+titled "Digital Compression and Coding of Continuous-tone Still Images,
785+Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS
786+10918-1, ITU-T T.81. Part 2 is titled "Digital Compression and Coding of
787+Continuous-tone Still Images, Part 2: Compliance testing" and has document
788+numbers ISO/IEC IS 10918-2, ITU-T T.83.
789+
790+The JPEG standard does not specify all details of an interchangeable file
791+format. For the omitted details we follow the "JFIF" conventions, revision
792+1.02. JFIF 1.02 has been adopted as an Ecma International Technical Report
793+and thus received a formal publication status. It is available as a free
794+download in PDF format from
795+http://www.ecma-international.org/publications/techreports/E-TR-098.htm.
796+A PostScript version of the JFIF document is available at
797+http://www.ijg.org/files/jfif.ps.gz. There is also a plain text version at
798+http://www.ijg.org/files/jfif.txt.gz, but it is missing the figures.
799+
800+The TIFF 6.0 file format specification can be obtained by FTP from
801+ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz. The JPEG incorporation scheme
802+found in the TIFF 6.0 spec of 3-June-92 has a number of serious problems.
803+IJG does not recommend use of the TIFF 6.0 design (TIFF Compression tag 6).
804+Instead, we recommend the JPEG design proposed by TIFF Technical Note #2
805+(Compression tag 7). Copies of this Note can be obtained from
806+http://www.ijg.org/files/. It is expected that the next revision
807+of the TIFF spec will replace the 6.0 JPEG design with the Note's design.
808+Although IJG's own code does not support TIFF/JPEG, the free libtiff library
809+uses our library to implement TIFF/JPEG per the Note.
810+
811+
812+ARCHIVE LOCATIONS
813+=================
814+
815+The "official" archive site for this software is www.ijg.org.
816+The most recent released version can always be found there in
817+directory "files". This particular version will be archived as
818+http://www.ijg.org/files/jpegsrc.v8d.tar.gz, and in Windows-compatible
819+"zip" archive format as http://www.ijg.org/files/jpegsr8d.zip.
820+
821+The JPEG FAQ (Frequently Asked Questions) article is a source of some
822+general information about JPEG.
823+It is available on the World Wide Web at http://www.faqs.org/faqs/jpeg-faq/
824+and other news.answers archive sites, including the official news.answers
825+archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/.
826+If you don't have Web or FTP access, send e-mail to mail-server@rtfm.mit.edu
827+with body
828+ send usenet/news.answers/jpeg-faq/part1
829+ send usenet/news.answers/jpeg-faq/part2
830+
831+
832+FILE FORMAT WARS
833+================
834+
835+The ISO JPEG standards committee actually promotes different formats like
836+"JPEG 2000" or "JPEG XR", which are incompatible with original DCT-based
837+JPEG. IJG therefore does not support these formats (see REFERENCES). Indeed,
838+one of the original reasons for developing this free software was to help
839+force convergence on common, interoperable format standards for JPEG files.
840+Don't use an incompatible file format!
841+(In any case, our decoder will remain capable of reading existing JPEG
842+image files indefinitely.)
843+
844+
845+TO DO
846+=====
847+
848+Please send bug reports, offers of help, etc. to jpeg-info@jpegclub.org.
849
850=== added file 'src/libjpeg-turbo/README-turbo.txt'
851--- src/libjpeg-turbo/README-turbo.txt 1970-01-01 00:00:00 +0000
852+++ src/libjpeg-turbo/README-turbo.txt 2012-06-27 16:20:24 +0000
853@@ -0,0 +1,361 @@
854+*******************************************************************************
855+** Background
856+*******************************************************************************
857+
858+libjpeg-turbo is a derivative of libjpeg that uses SIMD instructions (MMX,
859+SSE2, NEON) to accelerate baseline JPEG compression and decompression on x86,
860+x86-64, and ARM systems. On such systems, libjpeg-turbo is generally 2-4x as
861+fast as the unmodified version of libjpeg, all else being equal.
862+
863+libjpeg-turbo was originally based on libjpeg/SIMD by Miyasaka Masaru, but
864+the TigerVNC and VirtualGL projects made numerous enhancements to the codec in
865+2009, including improved support for Mac OS X, 64-bit support, support for
866+32-bit and big-endian pixel formats (RGBX, XBGR, etc.), accelerated Huffman
867+encoding/decoding, and various bug fixes. The goal was to produce a fully
868+open-source codec that could replace the partially closed-source TurboJPEG/IPP
869+codec used by VirtualGL and TurboVNC. libjpeg-turbo generally achieves 80-120%
870+of the performance of TurboJPEG/IPP. It is faster in some areas but slower in
871+others.
872+
873+In early 2010, libjpeg-turbo spun off into its own independent project, with
874+the goal of making high-speed JPEG compression/decompression technology
875+available to a broader range of users and developers.
876+
877+
878+*******************************************************************************
879+** License
880+*******************************************************************************
881+
882+Most of libjpeg-turbo inherits the non-restrictive, BSD-style license used by
883+libjpeg (see README.) The TurboJPEG/OSS wrapper (both C and Java versions) and
884+associated test programs bear a similar license, which is reproduced below:
885+
886+Redistribution and use in source and binary forms, with or without
887+modification, are permitted provided that the following conditions are met:
888+
889+- Redistributions of source code must retain the above copyright notice,
890+ this list of conditions and the following disclaimer.
891+- Redistributions in binary form must reproduce the above copyright notice,
892+ this list of conditions and the following disclaimer in the documentation
893+ and/or other materials provided with the distribution.
894+- Neither the name of the libjpeg-turbo Project nor the names of its
895+ contributors may be used to endorse or promote products derived from this
896+ software without specific prior written permission.
897+
898+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
899+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
900+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
901+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
902+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
903+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
904+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
905+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
906+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
907+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
908+POSSIBILITY OF SUCH DAMAGE.
909+
910+
911+*******************************************************************************
912+** Using libjpeg-turbo
913+*******************************************************************************
914+
915+libjpeg-turbo includes two APIs that can be used to compress and decompress
916+JPEG images:
917+
918+ TurboJPEG API: This API provides an easy-to-use interface for compressing
919+ and decompressing JPEG images in memory. It also provides some functionality
920+ that would not be straightforward to achieve using the underlying libjpeg
921+ API, such as generating planar YUV images and performing multiple
922+ simultaneous lossless transforms on an image. The Java interface for
923+ libjpeg-turbo is written on top of the TurboJPEG API.
924+
925+ libjpeg API: This is the de facto industry-standard API for compressing and
926+ decompressing JPEG images. It is more difficult to use than the TurboJPEG
927+ API but also more powerful. libjpeg-turbo is both API/ABI-compatible and
928+ mathematically compatible with libjpeg v6b. It can also optionally be
929+ configured to be API/ABI-compatible with libjpeg v7 and v8 (see below.)
930+
931+
932+=============================
933+Replacing libjpeg at Run Time
934+=============================
935+
936+If a Unix application is dynamically linked with libjpeg, then you can replace
937+libjpeg with libjpeg-turbo at run time by manipulating LD_LIBRARY_PATH.
938+For instance:
939+
940+ [Using libjpeg]
941+ > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg
942+ real 0m0.392s
943+ user 0m0.074s
944+ sys 0m0.020s
945+
946+ [Using libjpeg-turbo]
947+ > export LD_LIBRARY_PATH=/opt/libjpeg-turbo/{lib}:$LD_LIBRARY_PATH
948+ > time cjpeg <vgl_5674_0098.ppm >vgl_5674_0098.jpg
949+ real 0m0.109s
950+ user 0m0.029s
951+ sys 0m0.010s
952+
953+NOTE: {lib} can be lib, lib32, lib64, or lib/64, depending on the O/S and
954+architecture.
955+
956+System administrators can also replace the libjpeg sym links in /usr/{lib} with
957+links to the libjpeg-turbo dynamic library located in /opt/libjpeg-turbo/{lib}.
958+This will effectively accelerate every application that uses the libjpeg
959+dynamic library on the system.
960+
961+The libjpeg-turbo SDK for Visual C++ installs the libjpeg-turbo DLL
962+(jpeg62.dll, jpeg7.dll, or jpeg8.dll, depending on whether it was built with
963+libjpeg v6b, v7, or v8 emulation) into c:\libjpeg-turbo[64]\bin, and the PATH
964+environment variable can be modified such that this directory is searched
965+before any others that might contain a libjpeg DLL. However, if a libjpeg
966+DLL exists in an application's install directory, then Windows will load this
967+DLL first whenever the application is launched. Thus, if an application ships
968+with jpeg62.dll, jpeg7.dll, or jpeg8.dll, then back up the application's
969+version of this DLL and copy c:\libjpeg-turbo[64]\bin\jpeg*.dll into the
970+application's install directory to accelerate it.
971+
972+The version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
973+Visual C++ requires the Visual C++ 2008 C run-time DLL (msvcr90.dll).
974+msvcr90.dll ships with more recent versions of Windows, but users of older
975+Windows releases can obtain it from the Visual C++ 2008 Redistributable
976+Package, which is available as a free download from Microsoft's web site.
977+
978+NOTE: Features of libjpeg that require passing a C run-time structure, such
979+as a file handle, from an application to libjpeg will probably not work with
980+the version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for
981+Visual C++, unless the application is also built to use the Visual C++ 2008 C
982+run-time DLL. In particular, this affects jpeg_stdio_dest() and
983+jpeg_stdio_src().
984+
985+Mac applications typically embed their own copies of the libjpeg dylib inside
986+the (hidden) application bundle, so it is not possible to globally replace
987+libjpeg on OS X systems. If an application uses a shared library version of
988+libjpeg, then it may be possible to replace the application's version of it.
989+This would generally involve copying libjpeg.*.dylib from libjpeg-turbo into
990+the appropriate place in the application bundle and using install_name_tool to
991+repoint the dylib to the new directory. This requires an advanced knowledge of
992+OS X and would not survive an upgrade or a re-install of the application.
993+Thus, it is not recommended for most users.
994+
995+=======================
996+Replacing TurboJPEG/IPP
997+=======================
998+
999+libjpeg-turbo is a drop-in replacement for the TurboJPEG/IPP SDK used by
1000+VirtualGL 2.1.x and TurboVNC 0.6 (and prior.) libjpeg-turbo contains a wrapper
1001+library (TurboJPEG/OSS) that emulates the TurboJPEG API using libjpeg-turbo
1002+instead of the closed-source Intel Performance Primitives. You can replace the
1003+TurboJPEG/IPP package on Linux systems with the libjpeg-turbo package in order
1004+to make existing releases of VirtualGL 2.1.x and TurboVNC 0.x use the new codec
1005+at run time. Note that the 64-bit libjpeg-turbo packages contain only 64-bit
1006+binaries, whereas the TurboJPEG/IPP 64-bit packages contained both 64-bit and
1007+32-bit binaries. Thus, to replace a TurboJPEG/IPP 64-bit package, install
1008+both the 64-bit and 32-bit versions of libjpeg-turbo.
1009+
1010+You can also build the VirtualGL 2.1.x and TurboVNC 0.6 source code with
1011+the libjpeg-turbo SDK instead of TurboJPEG/IPP. It should work identically.
1012+libjpeg-turbo also includes static library versions of TurboJPEG/OSS, which
1013+are used to build VirtualGL 2.2 and TurboVNC 1.0 and later.
1014+
1015+========================================
1016+Using libjpeg-turbo in Your Own Programs
1017+========================================
1018+
1019+For the most part, libjpeg-turbo should work identically to libjpeg, so in
1020+most cases, an application can be built against libjpeg and then run against
1021+libjpeg-turbo. On Unix systems (including Cygwin), you can build against
1022+libjpeg-turbo instead of libjpeg by setting
1023+
1024+ CPATH=/opt/libjpeg-turbo/include
1025+ and
1026+ LIBRARY_PATH=/opt/libjpeg-turbo/{lib}
1027+
1028+({lib} = lib32 or lib64, depending on whether you are building a 32-bit or a
1029+64-bit application.)
1030+
1031+If using MinGW, then set
1032+
1033+ CPATH=/c/libjpeg-turbo-gcc[64]/include
1034+ and
1035+ LIBRARY_PATH=/c/libjpeg-turbo-gcc[64]/lib
1036+
1037+Building against libjpeg-turbo is useful, for instance, if you want to build an
1038+application that leverages the libjpeg-turbo colorspace extensions (see below.)
1039+On Linux and Solaris systems, you would still need to manipulate
1040+LD_LIBRARY_PATH or create appropriate sym links to use libjpeg-turbo at run
1041+time. On such systems, you can pass -R /opt/libjpeg-turbo/{lib} to the linker
1042+to force the use of libjpeg-turbo at run time rather than libjpeg (also useful
1043+if you want to leverage the colorspace extensions), or you can link against the
1044+libjpeg-turbo static library.
1045+
1046+To force a Linux, Solaris, or MinGW application to link against the static
1047+version of libjpeg-turbo, you can use the following linker options:
1048+
1049+ -Wl,-Bstatic -ljpeg -Wl,-Bdynamic
1050+
1051+On OS X, simply add /opt/libjpeg-turbo/lib/libjpeg.a to the linker command
1052+line (this also works on Linux and Solaris.)
1053+
1054+To build Visual C++ applications using libjpeg-turbo, add
1055+c:\libjpeg-turbo[64]\include to the system or user INCLUDE environment
1056+variable and c:\libjpeg-turbo[64]\lib to the system or user LIB environment
1057+variable, and then link against either jpeg.lib (to use the DLL version of
1058+libjpeg-turbo) or jpeg-static.lib (to use the static version of libjpeg-turbo.)
1059+
1060+=====================
1061+Colorspace Extensions
1062+=====================
1063+
1064+libjpeg-turbo includes extensions that allow JPEG images to be compressed
1065+directly from (and decompressed directly to) buffers that use BGR, BGRX,
1066+RGBX, XBGR, and XRGB pixel ordering. This is implemented with ten new
1067+colorspace constants:
1068+
1069+ JCS_EXT_RGB /* red/green/blue */
1070+ JCS_EXT_RGBX /* red/green/blue/x */
1071+ JCS_EXT_BGR /* blue/green/red */
1072+ JCS_EXT_BGRX /* blue/green/red/x */
1073+ JCS_EXT_XBGR /* x/blue/green/red */
1074+ JCS_EXT_XRGB /* x/red/green/blue */
1075+ JCS_EXT_RGBA /* red/green/blue/alpha */
1076+ JCS_EXT_BGRA /* blue/green/red/alpha */
1077+ JCS_EXT_ABGR /* alpha/blue/green/red */
1078+ JCS_EXT_ARGB /* alpha/red/green/blue */
1079+
1080+Setting cinfo.in_color_space (compression) or cinfo.out_color_space
1081+(decompression) to one of these values will cause libjpeg-turbo to read the
1082+red, green, and blue values from (or write them to) the appropriate position in
1083+the pixel when compressing from/decompressing to an RGB buffer.
1084+
1085+Your application can check for the existence of these extensions at compile
1086+time with:
1087+
1088+ #ifdef JCS_EXTENSIONS
1089+
1090+At run time, attempting to use these extensions with a version of libjpeg
1091+that doesn't support them will result in a "Bogus input colorspace" error.
1092+
1093+When using the RGBX, BGRX, XBGR, and XRGB colorspaces during decompression, the
1094+X byte is undefined, and in order to ensure the best performance, libjpeg-turbo
1095+can set that byte to whatever value it wishes. If an application expects the X
1096+byte to be used as an alpha channel, then it should specify JCS_EXT_RGBA,
1097+JCS_EXT_BGRA, JCS_EXT_ABGR, or JCS_EXT_ARGB. When these colorspace constants
1098+are used, the X byte is guaranteed to be 0xFF, which is interpreted as opaque.
1099+
1100+Your application can check for the existence of the alpha channel colorspace
1101+extensions at compile time with:
1102+
1103+ #ifdef JCS_ALPHA_EXTENSIONS
1104+
1105+jcstest.c, located in the libjpeg-turbo source tree, demonstrates how to check
1106+for the existence of the colorspace extensions at compile time and run time.
1107+
1108+=================================
1109+libjpeg v7 and v8 API/ABI support
1110+=================================
1111+
1112+With libjpeg v7 and v8, new features were added that necessitated extending the
1113+compression and decompression structures. Unfortunately, due to the exposed
1114+nature of those structures, extending them also necessitated breaking backward
1115+ABI compatibility with previous libjpeg releases. Thus, programs that are
1116+built to use libjpeg v7 or v8 did not work with libjpeg-turbo, since it is
1117+based on the libjpeg v6b code base. Although libjpeg v7 and v8 are still not
1118+as widely used as v6b, enough programs (including a few Linux distros) have
1119+made the switch that it was desirable to provide support for the libjpeg v7/v8
1120+API/ABI in libjpeg-turbo. Although libjpeg-turbo can now be configured as a
1121+drop-in replacement for libjpeg v7 or v8, it should be noted that not all of
1122+the features in libjpeg v7 and v8 are supported (see below.)
1123+
1124+By passing an argument of --with-jpeg7 or --with-jpeg8 to configure, or an
1125+argument of -DWITH_JPEG7=1 or -DWITH_JPEG8=1 to cmake, you can build a version
1126+of libjpeg-turbo that emulates the libjpeg v7 or v8 API/ABI, so that programs
1127+that are built against libjpeg v7 or v8 can be run with libjpeg-turbo. The
1128+following section describes which libjpeg v7+ features are supported and which
1129+aren't.
1130+
1131+libjpeg v7 and v8 Features:
1132+---------------------------
1133+
1134+Fully supported:
1135+
1136+-- cjpeg: Separate quality settings for luminance and chrominance
1137+ Note that the libpjeg v7+ API was extended to accommodate this feature only
1138+ for convenience purposes. It has always been possible to implement this
1139+ feature with libjpeg v6b (see rdswitch.c for an example.)
1140+
1141+-- cjpeg: 32-bit BMP support
1142+
1143+-- jpegtran: lossless cropping
1144+
1145+-- jpegtran: -perfect option
1146+
1147+-- rdjpgcom: -raw option
1148+
1149+-- rdjpgcom: locale awareness
1150+
1151+
1152+Fully supported when using libjpeg v7/v8 emulation:
1153+
1154+-- libjpeg: In-memory source and destination managers
1155+
1156+
1157+Not supported:
1158+
1159+-- libjpeg: DCT scaling in compressor
1160+ cinfo.scale_num and cinfo.scale_denom are silently ignored.
1161+ There is no technical reason why DCT scaling cannot be supported, but
1162+ without the SmartScale extension (see below), it would only be able to
1163+ down-scale using ratios of 1/2, 8/15, 4/7, 8/13, 2/3, 8/11, 4/5, and 8/9,
1164+ which is of limited usefulness.
1165+
1166+-- libjpeg: SmartScale
1167+ cinfo.block_size is silently ignored.
1168+ SmartScale is an extension to the JPEG format that allows for DCT block
1169+ sizes other than 8x8. It would be difficult to support this feature while
1170+ retaining backward compatibility with libjpeg v6b.
1171+
1172+-- libjpeg: IDCT scaling extensions in decompressor
1173+ libjpeg-turbo still supports IDCT scaling with scaling factors of 1/2, 1/4,
1174+ and 1/8 (same as libjpeg v6b.)
1175+
1176+-- libjpeg: Fancy downsampling in compressor
1177+ cinfo.do_fancy_downsampling is silently ignored.
1178+ This requires the DCT scaling feature, which is not supported.
1179+
1180+-- jpegtran: Scaling
1181+ This requires both the DCT scaling and SmartScale features, which are not
1182+ supported.
1183+
1184+-- Lossless RGB JPEG files
1185+ This requires the SmartScale feature, which is not supported.
1186+
1187+
1188+*******************************************************************************
1189+** Performance pitfalls
1190+*******************************************************************************
1191+
1192+===============
1193+Restart Markers
1194+===============
1195+
1196+The optimized Huffman decoder in libjpeg-turbo does not handle restart markers
1197+in a way that makes the rest of the libjpeg infrastructure happy, so it is
1198+necessary to use the slow Huffman decoder when decompressing a JPEG image that
1199+has restart markers. This can cause the decompression performance to drop by
1200+as much as 20%, but the performance will still be much greater than that of
1201+libjpeg. Many consumer packages, such as PhotoShop, use restart markers when
1202+generating JPEG images, so images generated by those programs will experience
1203+this issue.
1204+
1205+===============================================
1206+Fast Integer Forward DCT at High Quality Levels
1207+===============================================
1208+
1209+The algorithm used by the SIMD-accelerated quantization function cannot produce
1210+correct results whenever the fast integer forward DCT is used along with a JPEG
1211+quality of 98-100. Thus, libjpeg-turbo must use the non-SIMD quantization
1212+function in those cases. This causes performance to drop by as much as 40%.
1213+It is therefore strongly advised that you use the slow integer forward DCT
1214+whenever encoding images with a JPEG quality of 98 or higher.
1215
1216=== added file 'src/libjpeg-turbo/config.h'
1217--- src/libjpeg-turbo/config.h 1970-01-01 00:00:00 +0000
1218+++ src/libjpeg-turbo/config.h 2012-06-27 16:20:24 +0000
1219@@ -0,0 +1,137 @@
1220+/* config.h. Generated from config.h.in by configure. */
1221+/* config.h.in. Generated from configure.ac by autoheader. */
1222+
1223+/* Build number */
1224+#define BUILD "20120626"
1225+
1226+/* Support arithmetic encoding */
1227+#define C_ARITH_CODING_SUPPORTED 1
1228+
1229+/* Support arithmetic decoding */
1230+#define D_ARITH_CODING_SUPPORTED 1
1231+
1232+/* Define to 1 if you have the <dlfcn.h> header file. */
1233+#define HAVE_DLFCN_H 1
1234+
1235+/* Define to 1 if you have the <inttypes.h> header file. */
1236+#define HAVE_INTTYPES_H 1
1237+
1238+/* Define to 1 if you have the <jni.h> header file. */
1239+/* #undef HAVE_JNI_H */
1240+
1241+/* Define to 1 if you have the `memcpy' function. */
1242+#define HAVE_MEMCPY 1
1243+
1244+/* Define to 1 if you have the <memory.h> header file. */
1245+#define HAVE_MEMORY_H 1
1246+
1247+/* Define to 1 if you have the `memset' function. */
1248+#define HAVE_MEMSET 1
1249+
1250+/* Define if your compiler supports prototypes */
1251+#define HAVE_PROTOTYPES 1
1252+
1253+/* Define to 1 if you have the <stddef.h> header file. */
1254+#define HAVE_STDDEF_H 1
1255+
1256+/* Define to 1 if you have the <stdint.h> header file. */
1257+#define HAVE_STDINT_H 1
1258+
1259+/* Define to 1 if you have the <stdlib.h> header file. */
1260+#define HAVE_STDLIB_H 1
1261+
1262+/* Define to 1 if you have the <strings.h> header file. */
1263+#define HAVE_STRINGS_H 1
1264+
1265+/* Define to 1 if you have the <string.h> header file. */
1266+#define HAVE_STRING_H 1
1267+
1268+/* Define to 1 if you have the <sys/stat.h> header file. */
1269+#define HAVE_SYS_STAT_H 1
1270+
1271+/* Define to 1 if you have the <sys/types.h> header file. */
1272+#define HAVE_SYS_TYPES_H 1
1273+
1274+/* Define to 1 if you have the <unistd.h> header file. */
1275+#define HAVE_UNISTD_H 1
1276+
1277+/* Define to 1 if the system has the type `unsigned char'. */
1278+#define HAVE_UNSIGNED_CHAR 1
1279+
1280+/* Define to 1 if the system has the type `unsigned short'. */
1281+#define HAVE_UNSIGNED_SHORT 1
1282+
1283+/* Compiler does not support pointers to undefined structures. */
1284+/* #undef INCOMPLETE_TYPES_BROKEN */
1285+
1286+/* How to obtain function inlining. */
1287+#define INLINE __attribute__((always_inline))
1288+
1289+/* libjpeg API version */
1290+#define JPEG_LIB_VERSION 62
1291+
1292+/* libjpeg-turbo version */
1293+#define LIBJPEG_TURBO_VERSION 1.2.0
1294+
1295+/* Define to the sub-directory in which libtool stores uninstalled libraries.
1296+ */
1297+#define LT_OBJDIR ".libs/"
1298+
1299+/* Define if you have BSD-like bzero and bcopy */
1300+/* #undef NEED_BSD_STRINGS */
1301+
1302+/* Define if you need short function names */
1303+/* #undef NEED_SHORT_EXTERNAL_NAMES */
1304+
1305+/* Define if you have sys/types.h */
1306+#define NEED_SYS_TYPES_H 1
1307+
1308+/* Name of package */
1309+#define PACKAGE "libjpeg-turbo"
1310+
1311+/* Define to the address where bug reports for this package should be sent. */
1312+#define PACKAGE_BUGREPORT ""
1313+
1314+/* Define to the full name of this package. */
1315+#define PACKAGE_NAME "libjpeg-turbo"
1316+
1317+/* Define to the full name and version of this package. */
1318+#define PACKAGE_STRING "libjpeg-turbo 1.2.0"
1319+
1320+/* Define to the one symbol short name of this package. */
1321+#define PACKAGE_TARNAME "libjpeg-turbo"
1322+
1323+/* Define to the home page for this package. */
1324+#define PACKAGE_URL ""
1325+
1326+/* Define to the version of this package. */
1327+#define PACKAGE_VERSION "1.2.0"
1328+
1329+/* Define if shift is unsigned */
1330+/* #undef RIGHT_SHIFT_IS_UNSIGNED */
1331+
1332+/* Define to 1 if you have the ANSI C header files. */
1333+#define STDC_HEADERS 1
1334+
1335+/* Version number of package */
1336+#define VERSION "1.2.0"
1337+
1338+/* Use accelerated SIMD routines. */
1339+#define WITH_SIMD 1
1340+
1341+/* Define to 1 if type `char' is unsigned and you are not using gcc. */
1342+#ifndef __CHAR_UNSIGNED__
1343+/* # undef __CHAR_UNSIGNED__ */
1344+#endif
1345+
1346+/* Define to empty if `const' does not conform to ANSI C. */
1347+/* #undef const */
1348+
1349+/* Define to `__inline__' or `__inline' if that's what the C compiler
1350+ calls it, or to nothing if 'inline' is not supported under any name. */
1351+#ifndef __cplusplus
1352+/* #undef inline */
1353+#endif
1354+
1355+/* Define to `unsigned int' if <sys/types.h> does not define. */
1356+/* #undef size_t */
1357
1358=== added file 'src/libjpeg-turbo/jaricom.c'
1359--- src/libjpeg-turbo/jaricom.c 1970-01-01 00:00:00 +0000
1360+++ src/libjpeg-turbo/jaricom.c 2012-06-27 16:20:24 +0000
1361@@ -0,0 +1,153 @@
1362+/*
1363+ * jaricom.c
1364+ *
1365+ * Developed 1997-2009 by Guido Vollbeding.
1366+ * This file is part of the Independent JPEG Group's software.
1367+ * For conditions of distribution and use, see the accompanying README file.
1368+ *
1369+ * This file contains probability estimation tables for common use in
1370+ * arithmetic entropy encoding and decoding routines.
1371+ *
1372+ * This data represents Table D.2 in the JPEG spec (ISO/IEC IS 10918-1
1373+ * and CCITT Recommendation ITU-T T.81) and Table 24 in the JBIG spec
1374+ * (ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82).
1375+ */
1376+
1377+#define JPEG_INTERNALS
1378+#include "jinclude.h"
1379+#include "jpeglib.h"
1380+
1381+/* The following #define specifies the packing of the four components
1382+ * into the compact INT32 representation.
1383+ * Note that this formula must match the actual arithmetic encoder
1384+ * and decoder implementation. The implementation has to be changed
1385+ * if this formula is changed.
1386+ * The current organization is leaned on Markus Kuhn's JBIG
1387+ * implementation (jbig_tab.c).
1388+ */
1389+
1390+#define V(i,a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b)
1391+
1392+const INT32 jpeg_aritab[113+1] = {
1393+/*
1394+ * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS
1395+ */
1396+ V( 0, 0x5a1d, 1, 1, 1 ),
1397+ V( 1, 0x2586, 14, 2, 0 ),
1398+ V( 2, 0x1114, 16, 3, 0 ),
1399+ V( 3, 0x080b, 18, 4, 0 ),
1400+ V( 4, 0x03d8, 20, 5, 0 ),
1401+ V( 5, 0x01da, 23, 6, 0 ),
1402+ V( 6, 0x00e5, 25, 7, 0 ),
1403+ V( 7, 0x006f, 28, 8, 0 ),
1404+ V( 8, 0x0036, 30, 9, 0 ),
1405+ V( 9, 0x001a, 33, 10, 0 ),
1406+ V( 10, 0x000d, 35, 11, 0 ),
1407+ V( 11, 0x0006, 9, 12, 0 ),
1408+ V( 12, 0x0003, 10, 13, 0 ),
1409+ V( 13, 0x0001, 12, 13, 0 ),
1410+ V( 14, 0x5a7f, 15, 15, 1 ),
1411+ V( 15, 0x3f25, 36, 16, 0 ),
1412+ V( 16, 0x2cf2, 38, 17, 0 ),
1413+ V( 17, 0x207c, 39, 18, 0 ),
1414+ V( 18, 0x17b9, 40, 19, 0 ),
1415+ V( 19, 0x1182, 42, 20, 0 ),
1416+ V( 20, 0x0cef, 43, 21, 0 ),
1417+ V( 21, 0x09a1, 45, 22, 0 ),
1418+ V( 22, 0x072f, 46, 23, 0 ),
1419+ V( 23, 0x055c, 48, 24, 0 ),
1420+ V( 24, 0x0406, 49, 25, 0 ),
1421+ V( 25, 0x0303, 51, 26, 0 ),
1422+ V( 26, 0x0240, 52, 27, 0 ),
1423+ V( 27, 0x01b1, 54, 28, 0 ),
1424+ V( 28, 0x0144, 56, 29, 0 ),
1425+ V( 29, 0x00f5, 57, 30, 0 ),
1426+ V( 30, 0x00b7, 59, 31, 0 ),
1427+ V( 31, 0x008a, 60, 32, 0 ),
1428+ V( 32, 0x0068, 62, 33, 0 ),
1429+ V( 33, 0x004e, 63, 34, 0 ),
1430+ V( 34, 0x003b, 32, 35, 0 ),
1431+ V( 35, 0x002c, 33, 9, 0 ),
1432+ V( 36, 0x5ae1, 37, 37, 1 ),
1433+ V( 37, 0x484c, 64, 38, 0 ),
1434+ V( 38, 0x3a0d, 65, 39, 0 ),
1435+ V( 39, 0x2ef1, 67, 40, 0 ),
1436+ V( 40, 0x261f, 68, 41, 0 ),
1437+ V( 41, 0x1f33, 69, 42, 0 ),
1438+ V( 42, 0x19a8, 70, 43, 0 ),
1439+ V( 43, 0x1518, 72, 44, 0 ),
1440+ V( 44, 0x1177, 73, 45, 0 ),
1441+ V( 45, 0x0e74, 74, 46, 0 ),
1442+ V( 46, 0x0bfb, 75, 47, 0 ),
1443+ V( 47, 0x09f8, 77, 48, 0 ),
1444+ V( 48, 0x0861, 78, 49, 0 ),
1445+ V( 49, 0x0706, 79, 50, 0 ),
1446+ V( 50, 0x05cd, 48, 51, 0 ),
1447+ V( 51, 0x04de, 50, 52, 0 ),
1448+ V( 52, 0x040f, 50, 53, 0 ),
1449+ V( 53, 0x0363, 51, 54, 0 ),
1450+ V( 54, 0x02d4, 52, 55, 0 ),
1451+ V( 55, 0x025c, 53, 56, 0 ),
1452+ V( 56, 0x01f8, 54, 57, 0 ),
1453+ V( 57, 0x01a4, 55, 58, 0 ),
1454+ V( 58, 0x0160, 56, 59, 0 ),
1455+ V( 59, 0x0125, 57, 60, 0 ),
1456+ V( 60, 0x00f6, 58, 61, 0 ),
1457+ V( 61, 0x00cb, 59, 62, 0 ),
1458+ V( 62, 0x00ab, 61, 63, 0 ),
1459+ V( 63, 0x008f, 61, 32, 0 ),
1460+ V( 64, 0x5b12, 65, 65, 1 ),
1461+ V( 65, 0x4d04, 80, 66, 0 ),
1462+ V( 66, 0x412c, 81, 67, 0 ),
1463+ V( 67, 0x37d8, 82, 68, 0 ),
1464+ V( 68, 0x2fe8, 83, 69, 0 ),
1465+ V( 69, 0x293c, 84, 70, 0 ),
1466+ V( 70, 0x2379, 86, 71, 0 ),
1467+ V( 71, 0x1edf, 87, 72, 0 ),
1468+ V( 72, 0x1aa9, 87, 73, 0 ),
1469+ V( 73, 0x174e, 72, 74, 0 ),
1470+ V( 74, 0x1424, 72, 75, 0 ),
1471+ V( 75, 0x119c, 74, 76, 0 ),
1472+ V( 76, 0x0f6b, 74, 77, 0 ),
1473+ V( 77, 0x0d51, 75, 78, 0 ),
1474+ V( 78, 0x0bb6, 77, 79, 0 ),
1475+ V( 79, 0x0a40, 77, 48, 0 ),
1476+ V( 80, 0x5832, 80, 81, 1 ),
1477+ V( 81, 0x4d1c, 88, 82, 0 ),
1478+ V( 82, 0x438e, 89, 83, 0 ),
1479+ V( 83, 0x3bdd, 90, 84, 0 ),
1480+ V( 84, 0x34ee, 91, 85, 0 ),
1481+ V( 85, 0x2eae, 92, 86, 0 ),
1482+ V( 86, 0x299a, 93, 87, 0 ),
1483+ V( 87, 0x2516, 86, 71, 0 ),
1484+ V( 88, 0x5570, 88, 89, 1 ),
1485+ V( 89, 0x4ca9, 95, 90, 0 ),
1486+ V( 90, 0x44d9, 96, 91, 0 ),
1487+ V( 91, 0x3e22, 97, 92, 0 ),
1488+ V( 92, 0x3824, 99, 93, 0 ),
1489+ V( 93, 0x32b4, 99, 94, 0 ),
1490+ V( 94, 0x2e17, 93, 86, 0 ),
1491+ V( 95, 0x56a8, 95, 96, 1 ),
1492+ V( 96, 0x4f46, 101, 97, 0 ),
1493+ V( 97, 0x47e5, 102, 98, 0 ),
1494+ V( 98, 0x41cf, 103, 99, 0 ),
1495+ V( 99, 0x3c3d, 104, 100, 0 ),
1496+ V( 100, 0x375e, 99, 93, 0 ),
1497+ V( 101, 0x5231, 105, 102, 0 ),
1498+ V( 102, 0x4c0f, 106, 103, 0 ),
1499+ V( 103, 0x4639, 107, 104, 0 ),
1500+ V( 104, 0x415e, 103, 99, 0 ),
1501+ V( 105, 0x5627, 105, 106, 1 ),
1502+ V( 106, 0x50e7, 108, 107, 0 ),
1503+ V( 107, 0x4b85, 109, 103, 0 ),
1504+ V( 108, 0x5597, 110, 109, 0 ),
1505+ V( 109, 0x504f, 111, 107, 0 ),
1506+ V( 110, 0x5a10, 110, 111, 1 ),
1507+ V( 111, 0x5522, 112, 109, 0 ),
1508+ V( 112, 0x59eb, 112, 111, 1 ),
1509+/*
1510+ * This last entry is used for fixed probability estimate of 0.5
1511+ * as recommended in Section 10.3 Table 5 of ITU-T Rec. T.851.
1512+ */
1513+ V( 113, 0x5a1d, 113, 113, 0 )
1514+};
1515
1516=== added file 'src/libjpeg-turbo/jcapimin.c'
1517--- src/libjpeg-turbo/jcapimin.c 1970-01-01 00:00:00 +0000
1518+++ src/libjpeg-turbo/jcapimin.c 2012-06-27 16:20:24 +0000
1519@@ -0,0 +1,292 @@
1520+/*
1521+ * jcapimin.c
1522+ *
1523+ * Copyright (C) 1994-1998, Thomas G. Lane.
1524+ * Modified 2003-2010 by Guido Vollbeding.
1525+ * This file is part of the Independent JPEG Group's software.
1526+ * For conditions of distribution and use, see the accompanying README file.
1527+ *
1528+ * This file contains application interface code for the compression half
1529+ * of the JPEG library. These are the "minimum" API routines that may be
1530+ * needed in either the normal full-compression case or the transcoding-only
1531+ * case.
1532+ *
1533+ * Most of the routines intended to be called directly by an application
1534+ * are in this file or in jcapistd.c. But also see jcparam.c for
1535+ * parameter-setup helper routines, jcomapi.c for routines shared by
1536+ * compression and decompression, and jctrans.c for the transcoding case.
1537+ */
1538+
1539+#define JPEG_INTERNALS
1540+#include "jinclude.h"
1541+#include "jpeglib.h"
1542+
1543+
1544+/*
1545+ * Initialization of a JPEG compression object.
1546+ * The error manager must already be set up (in case memory manager fails).
1547+ */
1548+
1549+GLOBAL(void)
1550+jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
1551+{
1552+ int i;
1553+
1554+ /* Guard against version mismatches between library and caller. */
1555+ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
1556+ if (version != JPEG_LIB_VERSION)
1557+ ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
1558+ if (structsize != SIZEOF(struct jpeg_compress_struct))
1559+ ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
1560+ (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
1561+
1562+ /* For debugging purposes, we zero the whole master structure.
1563+ * But the application has already set the err pointer, and may have set
1564+ * client_data, so we have to save and restore those fields.
1565+ * Note: if application hasn't set client_data, tools like Purify may
1566+ * complain here.
1567+ */
1568+ {
1569+ struct jpeg_error_mgr * err = cinfo->err;
1570+ void * client_data = cinfo->client_data; /* ignore Purify complaint here */
1571+ MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
1572+ cinfo->err = err;
1573+ cinfo->client_data = client_data;
1574+ }
1575+ cinfo->is_decompressor = FALSE;
1576+
1577+ /* Initialize a memory manager instance for this object */
1578+ jinit_memory_mgr((j_common_ptr) cinfo);
1579+
1580+ /* Zero out pointers to permanent structures. */
1581+ cinfo->progress = NULL;
1582+ cinfo->dest = NULL;
1583+
1584+ cinfo->comp_info = NULL;
1585+
1586+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
1587+ cinfo->quant_tbl_ptrs[i] = NULL;
1588+#if JPEG_LIB_VERSION >= 70
1589+ cinfo->q_scale_factor[i] = 100;
1590+#endif
1591+ }
1592+
1593+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
1594+ cinfo->dc_huff_tbl_ptrs[i] = NULL;
1595+ cinfo->ac_huff_tbl_ptrs[i] = NULL;
1596+ }
1597+
1598+#if JPEG_LIB_VERSION >= 80
1599+ /* Must do it here for emit_dqt in case jpeg_write_tables is used */
1600+ cinfo->block_size = DCTSIZE;
1601+ cinfo->natural_order = jpeg_natural_order;
1602+ cinfo->lim_Se = DCTSIZE2-1;
1603+#endif
1604+
1605+ cinfo->script_space = NULL;
1606+
1607+ cinfo->input_gamma = 1.0; /* in case application forgets */
1608+
1609+ /* OK, I'm ready */
1610+ cinfo->global_state = CSTATE_START;
1611+}
1612+
1613+
1614+/*
1615+ * Destruction of a JPEG compression object
1616+ */
1617+
1618+GLOBAL(void)
1619+jpeg_destroy_compress (j_compress_ptr cinfo)
1620+{
1621+ jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
1622+}
1623+
1624+
1625+/*
1626+ * Abort processing of a JPEG compression operation,
1627+ * but don't destroy the object itself.
1628+ */
1629+
1630+GLOBAL(void)
1631+jpeg_abort_compress (j_compress_ptr cinfo)
1632+{
1633+ jpeg_abort((j_common_ptr) cinfo); /* use common routine */
1634+}
1635+
1636+
1637+/*
1638+ * Forcibly suppress or un-suppress all quantization and Huffman tables.
1639+ * Marks all currently defined tables as already written (if suppress)
1640+ * or not written (if !suppress). This will control whether they get emitted
1641+ * by a subsequent jpeg_start_compress call.
1642+ *
1643+ * This routine is exported for use by applications that want to produce
1644+ * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
1645+ * since it is called by jpeg_start_compress, we put it here --- otherwise
1646+ * jcparam.o would be linked whether the application used it or not.
1647+ */
1648+
1649+GLOBAL(void)
1650+jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
1651+{
1652+ int i;
1653+ JQUANT_TBL * qtbl;
1654+ JHUFF_TBL * htbl;
1655+
1656+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
1657+ if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
1658+ qtbl->sent_table = suppress;
1659+ }
1660+
1661+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
1662+ if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
1663+ htbl->sent_table = suppress;
1664+ if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
1665+ htbl->sent_table = suppress;
1666+ }
1667+}
1668+
1669+
1670+/*
1671+ * Finish JPEG compression.
1672+ *
1673+ * If a multipass operating mode was selected, this may do a great deal of
1674+ * work including most of the actual output.
1675+ */
1676+
1677+GLOBAL(void)
1678+jpeg_finish_compress (j_compress_ptr cinfo)
1679+{
1680+ JDIMENSION iMCU_row;
1681+
1682+ if (cinfo->global_state == CSTATE_SCANNING ||
1683+ cinfo->global_state == CSTATE_RAW_OK) {
1684+ /* Terminate first pass */
1685+ if (cinfo->next_scanline < cinfo->image_height)
1686+ ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
1687+ (*cinfo->master->finish_pass) (cinfo);
1688+ } else if (cinfo->global_state != CSTATE_WRCOEFS)
1689+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1690+ /* Perform any remaining passes */
1691+ while (! cinfo->master->is_last_pass) {
1692+ (*cinfo->master->prepare_for_pass) (cinfo);
1693+ for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
1694+ if (cinfo->progress != NULL) {
1695+ cinfo->progress->pass_counter = (long) iMCU_row;
1696+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
1697+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
1698+ }
1699+ /* We bypass the main controller and invoke coef controller directly;
1700+ * all work is being done from the coefficient buffer.
1701+ */
1702+ if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
1703+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
1704+ }
1705+ (*cinfo->master->finish_pass) (cinfo);
1706+ }
1707+ /* Write EOI, do final cleanup */
1708+ (*cinfo->marker->write_file_trailer) (cinfo);
1709+ (*cinfo->dest->term_destination) (cinfo);
1710+ /* We can use jpeg_abort to release memory and reset global_state */
1711+ jpeg_abort((j_common_ptr) cinfo);
1712+}
1713+
1714+
1715+/*
1716+ * Write a special marker.
1717+ * This is only recommended for writing COM or APPn markers.
1718+ * Must be called after jpeg_start_compress() and before
1719+ * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
1720+ */
1721+
1722+GLOBAL(void)
1723+jpeg_write_marker (j_compress_ptr cinfo, int marker,
1724+ const JOCTET *dataptr, unsigned int datalen)
1725+{
1726+ JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
1727+
1728+ if (cinfo->next_scanline != 0 ||
1729+ (cinfo->global_state != CSTATE_SCANNING &&
1730+ cinfo->global_state != CSTATE_RAW_OK &&
1731+ cinfo->global_state != CSTATE_WRCOEFS))
1732+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1733+
1734+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
1735+ write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
1736+ while (datalen--) {
1737+ (*write_marker_byte) (cinfo, *dataptr);
1738+ dataptr++;
1739+ }
1740+}
1741+
1742+/* Same, but piecemeal. */
1743+
1744+GLOBAL(void)
1745+jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
1746+{
1747+ if (cinfo->next_scanline != 0 ||
1748+ (cinfo->global_state != CSTATE_SCANNING &&
1749+ cinfo->global_state != CSTATE_RAW_OK &&
1750+ cinfo->global_state != CSTATE_WRCOEFS))
1751+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1752+
1753+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
1754+}
1755+
1756+GLOBAL(void)
1757+jpeg_write_m_byte (j_compress_ptr cinfo, int val)
1758+{
1759+ (*cinfo->marker->write_marker_byte) (cinfo, val);
1760+}
1761+
1762+
1763+/*
1764+ * Alternate compression function: just write an abbreviated table file.
1765+ * Before calling this, all parameters and a data destination must be set up.
1766+ *
1767+ * To produce a pair of files containing abbreviated tables and abbreviated
1768+ * image data, one would proceed as follows:
1769+ *
1770+ * initialize JPEG object
1771+ * set JPEG parameters
1772+ * set destination to table file
1773+ * jpeg_write_tables(cinfo);
1774+ * set destination to image file
1775+ * jpeg_start_compress(cinfo, FALSE);
1776+ * write data...
1777+ * jpeg_finish_compress(cinfo);
1778+ *
1779+ * jpeg_write_tables has the side effect of marking all tables written
1780+ * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
1781+ * will not re-emit the tables unless it is passed write_all_tables=TRUE.
1782+ */
1783+
1784+GLOBAL(void)
1785+jpeg_write_tables (j_compress_ptr cinfo)
1786+{
1787+ if (cinfo->global_state != CSTATE_START)
1788+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1789+
1790+ /* (Re)initialize error mgr and destination modules */
1791+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
1792+ (*cinfo->dest->init_destination) (cinfo);
1793+ /* Initialize the marker writer ... bit of a crock to do it here. */
1794+ jinit_marker_writer(cinfo);
1795+ /* Write them tables! */
1796+ (*cinfo->marker->write_tables_only) (cinfo);
1797+ /* And clean up. */
1798+ (*cinfo->dest->term_destination) (cinfo);
1799+ /*
1800+ * In library releases up through v6a, we called jpeg_abort() here to free
1801+ * any working memory allocated by the destination manager and marker
1802+ * writer. Some applications had a problem with that: they allocated space
1803+ * of their own from the library memory manager, and didn't want it to go
1804+ * away during write_tables. So now we do nothing. This will cause a
1805+ * memory leak if an app calls write_tables repeatedly without doing a full
1806+ * compression cycle or otherwise resetting the JPEG object. However, that
1807+ * seems less bad than unexpectedly freeing memory in the normal case.
1808+ * An app that prefers the old behavior can call jpeg_abort for itself after
1809+ * each call to jpeg_write_tables().
1810+ */
1811+}
1812
1813=== added file 'src/libjpeg-turbo/jcapistd.c'
1814--- src/libjpeg-turbo/jcapistd.c 1970-01-01 00:00:00 +0000
1815+++ src/libjpeg-turbo/jcapistd.c 2012-06-27 16:20:24 +0000
1816@@ -0,0 +1,161 @@
1817+/*
1818+ * jcapistd.c
1819+ *
1820+ * Copyright (C) 1994-1996, Thomas G. Lane.
1821+ * This file is part of the Independent JPEG Group's software.
1822+ * For conditions of distribution and use, see the accompanying README file.
1823+ *
1824+ * This file contains application interface code for the compression half
1825+ * of the JPEG library. These are the "standard" API routines that are
1826+ * used in the normal full-compression case. They are not used by a
1827+ * transcoding-only application. Note that if an application links in
1828+ * jpeg_start_compress, it will end up linking in the entire compressor.
1829+ * We thus must separate this file from jcapimin.c to avoid linking the
1830+ * whole compression library into a transcoder.
1831+ */
1832+
1833+#define JPEG_INTERNALS
1834+#include "jinclude.h"
1835+#include "jpeglib.h"
1836+
1837+
1838+/*
1839+ * Compression initialization.
1840+ * Before calling this, all parameters and a data destination must be set up.
1841+ *
1842+ * We require a write_all_tables parameter as a failsafe check when writing
1843+ * multiple datastreams from the same compression object. Since prior runs
1844+ * will have left all the tables marked sent_table=TRUE, a subsequent run
1845+ * would emit an abbreviated stream (no tables) by default. This may be what
1846+ * is wanted, but for safety's sake it should not be the default behavior:
1847+ * programmers should have to make a deliberate choice to emit abbreviated
1848+ * images. Therefore the documentation and examples should encourage people
1849+ * to pass write_all_tables=TRUE; then it will take active thought to do the
1850+ * wrong thing.
1851+ */
1852+
1853+GLOBAL(void)
1854+jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
1855+{
1856+ if (cinfo->global_state != CSTATE_START)
1857+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1858+
1859+ if (write_all_tables)
1860+ jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
1861+
1862+ /* (Re)initialize error mgr and destination modules */
1863+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
1864+ (*cinfo->dest->init_destination) (cinfo);
1865+ /* Perform master selection of active modules */
1866+ jinit_compress_master(cinfo);
1867+ /* Set up for the first pass */
1868+ (*cinfo->master->prepare_for_pass) (cinfo);
1869+ /* Ready for application to drive first pass through jpeg_write_scanlines
1870+ * or jpeg_write_raw_data.
1871+ */
1872+ cinfo->next_scanline = 0;
1873+ cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
1874+}
1875+
1876+
1877+/*
1878+ * Write some scanlines of data to the JPEG compressor.
1879+ *
1880+ * The return value will be the number of lines actually written.
1881+ * This should be less than the supplied num_lines only in case that
1882+ * the data destination module has requested suspension of the compressor,
1883+ * or if more than image_height scanlines are passed in.
1884+ *
1885+ * Note: we warn about excess calls to jpeg_write_scanlines() since
1886+ * this likely signals an application programmer error. However,
1887+ * excess scanlines passed in the last valid call are *silently* ignored,
1888+ * so that the application need not adjust num_lines for end-of-image
1889+ * when using a multiple-scanline buffer.
1890+ */
1891+
1892+GLOBAL(JDIMENSION)
1893+jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
1894+ JDIMENSION num_lines)
1895+{
1896+ JDIMENSION row_ctr, rows_left;
1897+
1898+ if (cinfo->global_state != CSTATE_SCANNING)
1899+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1900+ if (cinfo->next_scanline >= cinfo->image_height)
1901+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
1902+
1903+ /* Call progress monitor hook if present */
1904+ if (cinfo->progress != NULL) {
1905+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
1906+ cinfo->progress->pass_limit = (long) cinfo->image_height;
1907+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
1908+ }
1909+
1910+ /* Give master control module another chance if this is first call to
1911+ * jpeg_write_scanlines. This lets output of the frame/scan headers be
1912+ * delayed so that application can write COM, etc, markers between
1913+ * jpeg_start_compress and jpeg_write_scanlines.
1914+ */
1915+ if (cinfo->master->call_pass_startup)
1916+ (*cinfo->master->pass_startup) (cinfo);
1917+
1918+ /* Ignore any extra scanlines at bottom of image. */
1919+ rows_left = cinfo->image_height - cinfo->next_scanline;
1920+ if (num_lines > rows_left)
1921+ num_lines = rows_left;
1922+
1923+ row_ctr = 0;
1924+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
1925+ cinfo->next_scanline += row_ctr;
1926+ return row_ctr;
1927+}
1928+
1929+
1930+/*
1931+ * Alternate entry point to write raw data.
1932+ * Processes exactly one iMCU row per call, unless suspended.
1933+ */
1934+
1935+GLOBAL(JDIMENSION)
1936+jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
1937+ JDIMENSION num_lines)
1938+{
1939+ JDIMENSION lines_per_iMCU_row;
1940+
1941+ if (cinfo->global_state != CSTATE_RAW_OK)
1942+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
1943+ if (cinfo->next_scanline >= cinfo->image_height) {
1944+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
1945+ return 0;
1946+ }
1947+
1948+ /* Call progress monitor hook if present */
1949+ if (cinfo->progress != NULL) {
1950+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
1951+ cinfo->progress->pass_limit = (long) cinfo->image_height;
1952+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
1953+ }
1954+
1955+ /* Give master control module another chance if this is first call to
1956+ * jpeg_write_raw_data. This lets output of the frame/scan headers be
1957+ * delayed so that application can write COM, etc, markers between
1958+ * jpeg_start_compress and jpeg_write_raw_data.
1959+ */
1960+ if (cinfo->master->call_pass_startup)
1961+ (*cinfo->master->pass_startup) (cinfo);
1962+
1963+ /* Verify that at least one iMCU row has been passed. */
1964+ lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
1965+ if (num_lines < lines_per_iMCU_row)
1966+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
1967+
1968+ /* Directly compress the row. */
1969+ if (! (*cinfo->coef->compress_data) (cinfo, data)) {
1970+ /* If compressor did not consume the whole row, suspend processing. */
1971+ return 0;
1972+ }
1973+
1974+ /* OK, we processed one iMCU row. */
1975+ cinfo->next_scanline += lines_per_iMCU_row;
1976+ return lines_per_iMCU_row;
1977+}
1978
1979=== added file 'src/libjpeg-turbo/jcarith.c'
1980--- src/libjpeg-turbo/jcarith.c 1970-01-01 00:00:00 +0000
1981+++ src/libjpeg-turbo/jcarith.c 2012-06-27 16:20:24 +0000
1982@@ -0,0 +1,925 @@
1983+/*
1984+ * jcarith.c
1985+ *
1986+ * Developed 1997-2009 by Guido Vollbeding.
1987+ * This file is part of the Independent JPEG Group's software.
1988+ * For conditions of distribution and use, see the accompanying README file.
1989+ *
1990+ * This file contains portable arithmetic entropy encoding routines for JPEG
1991+ * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
1992+ *
1993+ * Both sequential and progressive modes are supported in this single module.
1994+ *
1995+ * Suspension is not currently supported in this module.
1996+ */
1997+
1998+#define JPEG_INTERNALS
1999+#include "jinclude.h"
2000+#include "jpeglib.h"
2001+
2002+
2003+/* Expanded entropy encoder object for arithmetic encoding. */
2004+
2005+typedef struct {
2006+ struct jpeg_entropy_encoder pub; /* public fields */
2007+
2008+ INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */
2009+ INT32 a; /* A register, normalized size of coding interval */
2010+ INT32 sc; /* counter for stacked 0xFF values which might overflow */
2011+ INT32 zc; /* counter for pending 0x00 output values which might *
2012+ * be discarded at the end ("Pacman" termination) */
2013+ int ct; /* bit shift counter, determines when next byte will be written */
2014+ int buffer; /* buffer for most recent output byte != 0xFF */
2015+
2016+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
2017+ int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
2018+
2019+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
2020+ int next_restart_num; /* next restart number to write (0-7) */
2021+
2022+ /* Pointers to statistics areas (these workspaces have image lifespan) */
2023+ unsigned char * dc_stats[NUM_ARITH_TBLS];
2024+ unsigned char * ac_stats[NUM_ARITH_TBLS];
2025+
2026+ /* Statistics bin for coding with fixed probability 0.5 */
2027+ unsigned char fixed_bin[4];
2028+} arith_entropy_encoder;
2029+
2030+typedef arith_entropy_encoder * arith_entropy_ptr;
2031+
2032+/* The following two definitions specify the allocation chunk size
2033+ * for the statistics area.
2034+ * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
2035+ * 49 statistics bins for DC, and 245 statistics bins for AC coding.
2036+ *
2037+ * We use a compact representation with 1 byte per statistics bin,
2038+ * thus the numbers directly represent byte sizes.
2039+ * This 1 byte per statistics bin contains the meaning of the MPS
2040+ * (more probable symbol) in the highest bit (mask 0x80), and the
2041+ * index into the probability estimation state machine table
2042+ * in the lower bits (mask 0x7F).
2043+ */
2044+
2045+#define DC_STAT_BINS 64
2046+#define AC_STAT_BINS 256
2047+
2048+/* NOTE: Uncomment the following #define if you want to use the
2049+ * given formula for calculating the AC conditioning parameter Kx
2050+ * for spectral selection progressive coding in section G.1.3.2
2051+ * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4).
2052+ * Although the spec and P&M authors claim that this "has proven
2053+ * to give good results for 8 bit precision samples", I'm not
2054+ * convinced yet that this is really beneficial.
2055+ * Early tests gave only very marginal compression enhancements
2056+ * (a few - around 5 or so - bytes even for very large files),
2057+ * which would turn out rather negative if we'd suppress the
2058+ * DAC (Define Arithmetic Conditioning) marker segments for
2059+ * the default parameters in the future.
2060+ * Note that currently the marker writing module emits 12-byte
2061+ * DAC segments for a full-component scan in a color image.
2062+ * This is not worth worrying about IMHO. However, since the
2063+ * spec defines the default values to be used if the tables
2064+ * are omitted (unlike Huffman tables, which are required
2065+ * anyway), one might optimize this behaviour in the future,
2066+ * and then it would be disadvantageous to use custom tables if
2067+ * they don't provide sufficient gain to exceed the DAC size.
2068+ *
2069+ * On the other hand, I'd consider it as a reasonable result
2070+ * that the conditioning has no significant influence on the
2071+ * compression performance. This means that the basic
2072+ * statistical model is already rather stable.
2073+ *
2074+ * Thus, at the moment, we use the default conditioning values
2075+ * anyway, and do not use the custom formula.
2076+ *
2077+#define CALCULATE_SPECTRAL_CONDITIONING
2078+ */
2079+
2080+/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
2081+ * We assume that int right shift is unsigned if INT32 right shift is,
2082+ * which should be safe.
2083+ */
2084+
2085+#ifdef RIGHT_SHIFT_IS_UNSIGNED
2086+#define ISHIFT_TEMPS int ishift_temp;
2087+#define IRIGHT_SHIFT(x,shft) \
2088+ ((ishift_temp = (x)) < 0 ? \
2089+ (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
2090+ (ishift_temp >> (shft)))
2091+#else
2092+#define ISHIFT_TEMPS
2093+#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
2094+#endif
2095+
2096+
2097+LOCAL(void)
2098+emit_byte (int val, j_compress_ptr cinfo)
2099+/* Write next output byte; we do not support suspension in this module. */
2100+{
2101+ struct jpeg_destination_mgr * dest = cinfo->dest;
2102+
2103+ *dest->next_output_byte++ = (JOCTET) val;
2104+ if (--dest->free_in_buffer == 0)
2105+ if (! (*dest->empty_output_buffer) (cinfo))
2106+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
2107+}
2108+
2109+
2110+/*
2111+ * Finish up at the end of an arithmetic-compressed scan.
2112+ */
2113+
2114+METHODDEF(void)
2115+finish_pass (j_compress_ptr cinfo)
2116+{
2117+ arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
2118+ INT32 temp;
2119+
2120+ /* Section D.1.8: Termination of encoding */
2121+
2122+ /* Find the e->c in the coding interval with the largest
2123+ * number of trailing zero bits */
2124+ if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c)
2125+ e->c = temp + 0x8000L;
2126+ else
2127+ e->c = temp;
2128+ /* Send remaining bytes to output */
2129+ e->c <<= e->ct;
2130+ if (e->c & 0xF8000000L) {
2131+ /* One final overflow has to be handled */
2132+ if (e->buffer >= 0) {
2133+ if (e->zc)
2134+ do emit_byte(0x00, cinfo);
2135+ while (--e->zc);
2136+ emit_byte(e->buffer + 1, cinfo);
2137+ if (e->buffer + 1 == 0xFF)
2138+ emit_byte(0x00, cinfo);
2139+ }
2140+ e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
2141+ e->sc = 0;
2142+ } else {
2143+ if (e->buffer == 0)
2144+ ++e->zc;
2145+ else if (e->buffer >= 0) {
2146+ if (e->zc)
2147+ do emit_byte(0x00, cinfo);
2148+ while (--e->zc);
2149+ emit_byte(e->buffer, cinfo);
2150+ }
2151+ if (e->sc) {
2152+ if (e->zc)
2153+ do emit_byte(0x00, cinfo);
2154+ while (--e->zc);
2155+ do {
2156+ emit_byte(0xFF, cinfo);
2157+ emit_byte(0x00, cinfo);
2158+ } while (--e->sc);
2159+ }
2160+ }
2161+ /* Output final bytes only if they are not 0x00 */
2162+ if (e->c & 0x7FFF800L) {
2163+ if (e->zc) /* output final pending zero bytes */
2164+ do emit_byte(0x00, cinfo);
2165+ while (--e->zc);
2166+ emit_byte((e->c >> 19) & 0xFF, cinfo);
2167+ if (((e->c >> 19) & 0xFF) == 0xFF)
2168+ emit_byte(0x00, cinfo);
2169+ if (e->c & 0x7F800L) {
2170+ emit_byte((e->c >> 11) & 0xFF, cinfo);
2171+ if (((e->c >> 11) & 0xFF) == 0xFF)
2172+ emit_byte(0x00, cinfo);
2173+ }
2174+ }
2175+}
2176+
2177+
2178+/*
2179+ * The core arithmetic encoding routine (common in JPEG and JBIG).
2180+ * This needs to go as fast as possible.
2181+ * Machine-dependent optimization facilities
2182+ * are not utilized in this portable implementation.
2183+ * However, this code should be fairly efficient and
2184+ * may be a good base for further optimizations anyway.
2185+ *
2186+ * Parameter 'val' to be encoded may be 0 or 1 (binary decision).
2187+ *
2188+ * Note: I've added full "Pacman" termination support to the
2189+ * byte output routines, which is equivalent to the optional
2190+ * Discard_final_zeros procedure (Figure D.15) in the spec.
2191+ * Thus, we always produce the shortest possible output
2192+ * stream compliant to the spec (no trailing zero bytes,
2193+ * except for FF stuffing).
2194+ *
2195+ * I've also introduced a new scheme for accessing
2196+ * the probability estimation state machine table,
2197+ * derived from Markus Kuhn's JBIG implementation.
2198+ */
2199+
2200+LOCAL(void)
2201+arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
2202+{
2203+ register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
2204+ register unsigned char nl, nm;
2205+ register INT32 qe, temp;
2206+ register int sv;
2207+
2208+ /* Fetch values from our compact representation of Table D.2:
2209+ * Qe values and probability estimation state machine
2210+ */
2211+ sv = *st;
2212+ qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
2213+ nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
2214+ nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
2215+
2216+ /* Encode & estimation procedures per sections D.1.4 & D.1.5 */
2217+ e->a -= qe;
2218+ if (val != (sv >> 7)) {
2219+ /* Encode the less probable symbol */
2220+ if (e->a >= qe) {
2221+ /* If the interval size (qe) for the less probable symbol (LPS)
2222+ * is larger than the interval size for the MPS, then exchange
2223+ * the two symbols for coding efficiency, otherwise code the LPS
2224+ * as usual: */
2225+ e->c += e->a;
2226+ e->a = qe;
2227+ }
2228+ *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
2229+ } else {
2230+ /* Encode the more probable symbol */
2231+ if (e->a >= 0x8000L)
2232+ return; /* A >= 0x8000 -> ready, no renormalization required */
2233+ if (e->a < qe) {
2234+ /* If the interval size (qe) for the less probable symbol (LPS)
2235+ * is larger than the interval size for the MPS, then exchange
2236+ * the two symbols for coding efficiency: */
2237+ e->c += e->a;
2238+ e->a = qe;
2239+ }
2240+ *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
2241+ }
2242+
2243+ /* Renormalization & data output per section D.1.6 */
2244+ do {
2245+ e->a <<= 1;
2246+ e->c <<= 1;
2247+ if (--e->ct == 0) {
2248+ /* Another byte is ready for output */
2249+ temp = e->c >> 19;
2250+ if (temp > 0xFF) {
2251+ /* Handle overflow over all stacked 0xFF bytes */
2252+ if (e->buffer >= 0) {
2253+ if (e->zc)
2254+ do emit_byte(0x00, cinfo);
2255+ while (--e->zc);
2256+ emit_byte(e->buffer + 1, cinfo);
2257+ if (e->buffer + 1 == 0xFF)
2258+ emit_byte(0x00, cinfo);
2259+ }
2260+ e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
2261+ e->sc = 0;
2262+ /* Note: The 3 spacer bits in the C register guarantee
2263+ * that the new buffer byte can't be 0xFF here
2264+ * (see page 160 in the P&M JPEG book). */
2265+ e->buffer = temp & 0xFF; /* new output byte, might overflow later */
2266+ } else if (temp == 0xFF) {
2267+ ++e->sc; /* stack 0xFF byte (which might overflow later) */
2268+ } else {
2269+ /* Output all stacked 0xFF bytes, they will not overflow any more */
2270+ if (e->buffer == 0)
2271+ ++e->zc;
2272+ else if (e->buffer >= 0) {
2273+ if (e->zc)
2274+ do emit_byte(0x00, cinfo);
2275+ while (--e->zc);
2276+ emit_byte(e->buffer, cinfo);
2277+ }
2278+ if (e->sc) {
2279+ if (e->zc)
2280+ do emit_byte(0x00, cinfo);
2281+ while (--e->zc);
2282+ do {
2283+ emit_byte(0xFF, cinfo);
2284+ emit_byte(0x00, cinfo);
2285+ } while (--e->sc);
2286+ }
2287+ e->buffer = temp & 0xFF; /* new output byte (can still overflow) */
2288+ }
2289+ e->c &= 0x7FFFFL;
2290+ e->ct += 8;
2291+ }
2292+ } while (e->a < 0x8000L);
2293+}
2294+
2295+
2296+/*
2297+ * Emit a restart marker & resynchronize predictions.
2298+ */
2299+
2300+LOCAL(void)
2301+emit_restart (j_compress_ptr cinfo, int restart_num)
2302+{
2303+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2304+ int ci;
2305+ jpeg_component_info * compptr;
2306+
2307+ finish_pass(cinfo);
2308+
2309+ emit_byte(0xFF, cinfo);
2310+ emit_byte(JPEG_RST0 + restart_num, cinfo);
2311+
2312+ /* Re-initialize statistics areas */
2313+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
2314+ compptr = cinfo->cur_comp_info[ci];
2315+ /* DC needs no table for refinement scan */
2316+ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
2317+ MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
2318+ /* Reset DC predictions to 0 */
2319+ entropy->last_dc_val[ci] = 0;
2320+ entropy->dc_context[ci] = 0;
2321+ }
2322+ /* AC needs no table when not present */
2323+ if (cinfo->progressive_mode == 0 || cinfo->Se) {
2324+ MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
2325+ }
2326+ }
2327+
2328+ /* Reset arithmetic encoding variables */
2329+ entropy->c = 0;
2330+ entropy->a = 0x10000L;
2331+ entropy->sc = 0;
2332+ entropy->zc = 0;
2333+ entropy->ct = 11;
2334+ entropy->buffer = -1; /* empty */
2335+}
2336+
2337+
2338+/*
2339+ * MCU encoding for DC initial scan (either spectral selection,
2340+ * or first pass of successive approximation).
2341+ */
2342+
2343+METHODDEF(boolean)
2344+encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2345+{
2346+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2347+ JBLOCKROW block;
2348+ unsigned char *st;
2349+ int blkn, ci, tbl;
2350+ int v, v2, m;
2351+ ISHIFT_TEMPS
2352+
2353+ /* Emit restart marker if needed */
2354+ if (cinfo->restart_interval) {
2355+ if (entropy->restarts_to_go == 0) {
2356+ emit_restart(cinfo, entropy->next_restart_num);
2357+ entropy->restarts_to_go = cinfo->restart_interval;
2358+ entropy->next_restart_num++;
2359+ entropy->next_restart_num &= 7;
2360+ }
2361+ entropy->restarts_to_go--;
2362+ }
2363+
2364+ /* Encode the MCU data blocks */
2365+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
2366+ block = MCU_data[blkn];
2367+ ci = cinfo->MCU_membership[blkn];
2368+ tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
2369+
2370+ /* Compute the DC value after the required point transform by Al.
2371+ * This is simply an arithmetic right shift.
2372+ */
2373+ m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al);
2374+
2375+ /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
2376+
2377+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
2378+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
2379+
2380+ /* Figure F.4: Encode_DC_DIFF */
2381+ if ((v = m - entropy->last_dc_val[ci]) == 0) {
2382+ arith_encode(cinfo, st, 0);
2383+ entropy->dc_context[ci] = 0; /* zero diff category */
2384+ } else {
2385+ entropy->last_dc_val[ci] = m;
2386+ arith_encode(cinfo, st, 1);
2387+ /* Figure F.6: Encoding nonzero value v */
2388+ /* Figure F.7: Encoding the sign of v */
2389+ if (v > 0) {
2390+ arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
2391+ st += 2; /* Table F.4: SP = S0 + 2 */
2392+ entropy->dc_context[ci] = 4; /* small positive diff category */
2393+ } else {
2394+ v = -v;
2395+ arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
2396+ st += 3; /* Table F.4: SN = S0 + 3 */
2397+ entropy->dc_context[ci] = 8; /* small negative diff category */
2398+ }
2399+ /* Figure F.8: Encoding the magnitude category of v */
2400+ m = 0;
2401+ if (v -= 1) {
2402+ arith_encode(cinfo, st, 1);
2403+ m = 1;
2404+ v2 = v;
2405+ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
2406+ while (v2 >>= 1) {
2407+ arith_encode(cinfo, st, 1);
2408+ m <<= 1;
2409+ st += 1;
2410+ }
2411+ }
2412+ arith_encode(cinfo, st, 0);
2413+ /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
2414+ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
2415+ entropy->dc_context[ci] = 0; /* zero diff category */
2416+ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
2417+ entropy->dc_context[ci] += 8; /* large diff category */
2418+ /* Figure F.9: Encoding the magnitude bit pattern of v */
2419+ st += 14;
2420+ while (m >>= 1)
2421+ arith_encode(cinfo, st, (m & v) ? 1 : 0);
2422+ }
2423+ }
2424+
2425+ return TRUE;
2426+}
2427+
2428+
2429+/*
2430+ * MCU encoding for AC initial scan (either spectral selection,
2431+ * or first pass of successive approximation).
2432+ */
2433+
2434+METHODDEF(boolean)
2435+encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2436+{
2437+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2438+ JBLOCKROW block;
2439+ unsigned char *st;
2440+ int tbl, k, ke;
2441+ int v, v2, m;
2442+
2443+ /* Emit restart marker if needed */
2444+ if (cinfo->restart_interval) {
2445+ if (entropy->restarts_to_go == 0) {
2446+ emit_restart(cinfo, entropy->next_restart_num);
2447+ entropy->restarts_to_go = cinfo->restart_interval;
2448+ entropy->next_restart_num++;
2449+ entropy->next_restart_num &= 7;
2450+ }
2451+ entropy->restarts_to_go--;
2452+ }
2453+
2454+ /* Encode the MCU data block */
2455+ block = MCU_data[0];
2456+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
2457+
2458+ /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
2459+
2460+ /* Establish EOB (end-of-block) index */
2461+ for (ke = cinfo->Se; ke > 0; ke--)
2462+ /* We must apply the point transform by Al. For AC coefficients this
2463+ * is an integer division with rounding towards 0. To do this portably
2464+ * in C, we shift after obtaining the absolute value.
2465+ */
2466+ if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) {
2467+ if (v >>= cinfo->Al) break;
2468+ } else {
2469+ v = -v;
2470+ if (v >>= cinfo->Al) break;
2471+ }
2472+
2473+ /* Figure F.5: Encode_AC_Coefficients */
2474+ for (k = cinfo->Ss; k <= ke; k++) {
2475+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2476+ arith_encode(cinfo, st, 0); /* EOB decision */
2477+ for (;;) {
2478+ if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
2479+ if (v >>= cinfo->Al) {
2480+ arith_encode(cinfo, st + 1, 1);
2481+ arith_encode(cinfo, entropy->fixed_bin, 0);
2482+ break;
2483+ }
2484+ } else {
2485+ v = -v;
2486+ if (v >>= cinfo->Al) {
2487+ arith_encode(cinfo, st + 1, 1);
2488+ arith_encode(cinfo, entropy->fixed_bin, 1);
2489+ break;
2490+ }
2491+ }
2492+ arith_encode(cinfo, st + 1, 0); st += 3; k++;
2493+ }
2494+ st += 2;
2495+ /* Figure F.8: Encoding the magnitude category of v */
2496+ m = 0;
2497+ if (v -= 1) {
2498+ arith_encode(cinfo, st, 1);
2499+ m = 1;
2500+ v2 = v;
2501+ if (v2 >>= 1) {
2502+ arith_encode(cinfo, st, 1);
2503+ m <<= 1;
2504+ st = entropy->ac_stats[tbl] +
2505+ (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
2506+ while (v2 >>= 1) {
2507+ arith_encode(cinfo, st, 1);
2508+ m <<= 1;
2509+ st += 1;
2510+ }
2511+ }
2512+ }
2513+ arith_encode(cinfo, st, 0);
2514+ /* Figure F.9: Encoding the magnitude bit pattern of v */
2515+ st += 14;
2516+ while (m >>= 1)
2517+ arith_encode(cinfo, st, (m & v) ? 1 : 0);
2518+ }
2519+ /* Encode EOB decision only if k <= cinfo->Se */
2520+ if (k <= cinfo->Se) {
2521+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2522+ arith_encode(cinfo, st, 1);
2523+ }
2524+
2525+ return TRUE;
2526+}
2527+
2528+
2529+/*
2530+ * MCU encoding for DC successive approximation refinement scan.
2531+ */
2532+
2533+METHODDEF(boolean)
2534+encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2535+{
2536+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2537+ unsigned char *st;
2538+ int Al, blkn;
2539+
2540+ /* Emit restart marker if needed */
2541+ if (cinfo->restart_interval) {
2542+ if (entropy->restarts_to_go == 0) {
2543+ emit_restart(cinfo, entropy->next_restart_num);
2544+ entropy->restarts_to_go = cinfo->restart_interval;
2545+ entropy->next_restart_num++;
2546+ entropy->next_restart_num &= 7;
2547+ }
2548+ entropy->restarts_to_go--;
2549+ }
2550+
2551+ st = entropy->fixed_bin; /* use fixed probability estimation */
2552+ Al = cinfo->Al;
2553+
2554+ /* Encode the MCU data blocks */
2555+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
2556+ /* We simply emit the Al'th bit of the DC coefficient value. */
2557+ arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1);
2558+ }
2559+
2560+ return TRUE;
2561+}
2562+
2563+
2564+/*
2565+ * MCU encoding for AC successive approximation refinement scan.
2566+ */
2567+
2568+METHODDEF(boolean)
2569+encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2570+{
2571+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2572+ JBLOCKROW block;
2573+ unsigned char *st;
2574+ int tbl, k, ke, kex;
2575+ int v;
2576+
2577+ /* Emit restart marker if needed */
2578+ if (cinfo->restart_interval) {
2579+ if (entropy->restarts_to_go == 0) {
2580+ emit_restart(cinfo, entropy->next_restart_num);
2581+ entropy->restarts_to_go = cinfo->restart_interval;
2582+ entropy->next_restart_num++;
2583+ entropy->next_restart_num &= 7;
2584+ }
2585+ entropy->restarts_to_go--;
2586+ }
2587+
2588+ /* Encode the MCU data block */
2589+ block = MCU_data[0];
2590+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
2591+
2592+ /* Section G.1.3.3: Encoding of AC coefficients */
2593+
2594+ /* Establish EOB (end-of-block) index */
2595+ for (ke = cinfo->Se; ke > 0; ke--)
2596+ /* We must apply the point transform by Al. For AC coefficients this
2597+ * is an integer division with rounding towards 0. To do this portably
2598+ * in C, we shift after obtaining the absolute value.
2599+ */
2600+ if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) {
2601+ if (v >>= cinfo->Al) break;
2602+ } else {
2603+ v = -v;
2604+ if (v >>= cinfo->Al) break;
2605+ }
2606+
2607+ /* Establish EOBx (previous stage end-of-block) index */
2608+ for (kex = ke; kex > 0; kex--)
2609+ if ((v = (*block)[jpeg_natural_order[kex]]) >= 0) {
2610+ if (v >>= cinfo->Ah) break;
2611+ } else {
2612+ v = -v;
2613+ if (v >>= cinfo->Ah) break;
2614+ }
2615+
2616+ /* Figure G.10: Encode_AC_Coefficients_SA */
2617+ for (k = cinfo->Ss; k <= ke; k++) {
2618+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2619+ if (k > kex)
2620+ arith_encode(cinfo, st, 0); /* EOB decision */
2621+ for (;;) {
2622+ if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
2623+ if (v >>= cinfo->Al) {
2624+ if (v >> 1) /* previously nonzero coef */
2625+ arith_encode(cinfo, st + 2, (v & 1));
2626+ else { /* newly nonzero coef */
2627+ arith_encode(cinfo, st + 1, 1);
2628+ arith_encode(cinfo, entropy->fixed_bin, 0);
2629+ }
2630+ break;
2631+ }
2632+ } else {
2633+ v = -v;
2634+ if (v >>= cinfo->Al) {
2635+ if (v >> 1) /* previously nonzero coef */
2636+ arith_encode(cinfo, st + 2, (v & 1));
2637+ else { /* newly nonzero coef */
2638+ arith_encode(cinfo, st + 1, 1);
2639+ arith_encode(cinfo, entropy->fixed_bin, 1);
2640+ }
2641+ break;
2642+ }
2643+ }
2644+ arith_encode(cinfo, st + 1, 0); st += 3; k++;
2645+ }
2646+ }
2647+ /* Encode EOB decision only if k <= cinfo->Se */
2648+ if (k <= cinfo->Se) {
2649+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2650+ arith_encode(cinfo, st, 1);
2651+ }
2652+
2653+ return TRUE;
2654+}
2655+
2656+
2657+/*
2658+ * Encode and output one MCU's worth of arithmetic-compressed coefficients.
2659+ */
2660+
2661+METHODDEF(boolean)
2662+encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
2663+{
2664+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2665+ jpeg_component_info * compptr;
2666+ JBLOCKROW block;
2667+ unsigned char *st;
2668+ int blkn, ci, tbl, k, ke;
2669+ int v, v2, m;
2670+
2671+ /* Emit restart marker if needed */
2672+ if (cinfo->restart_interval) {
2673+ if (entropy->restarts_to_go == 0) {
2674+ emit_restart(cinfo, entropy->next_restart_num);
2675+ entropy->restarts_to_go = cinfo->restart_interval;
2676+ entropy->next_restart_num++;
2677+ entropy->next_restart_num &= 7;
2678+ }
2679+ entropy->restarts_to_go--;
2680+ }
2681+
2682+ /* Encode the MCU data blocks */
2683+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
2684+ block = MCU_data[blkn];
2685+ ci = cinfo->MCU_membership[blkn];
2686+ compptr = cinfo->cur_comp_info[ci];
2687+
2688+ /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
2689+
2690+ tbl = compptr->dc_tbl_no;
2691+
2692+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
2693+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
2694+
2695+ /* Figure F.4: Encode_DC_DIFF */
2696+ if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) {
2697+ arith_encode(cinfo, st, 0);
2698+ entropy->dc_context[ci] = 0; /* zero diff category */
2699+ } else {
2700+ entropy->last_dc_val[ci] = (*block)[0];
2701+ arith_encode(cinfo, st, 1);
2702+ /* Figure F.6: Encoding nonzero value v */
2703+ /* Figure F.7: Encoding the sign of v */
2704+ if (v > 0) {
2705+ arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
2706+ st += 2; /* Table F.4: SP = S0 + 2 */
2707+ entropy->dc_context[ci] = 4; /* small positive diff category */
2708+ } else {
2709+ v = -v;
2710+ arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
2711+ st += 3; /* Table F.4: SN = S0 + 3 */
2712+ entropy->dc_context[ci] = 8; /* small negative diff category */
2713+ }
2714+ /* Figure F.8: Encoding the magnitude category of v */
2715+ m = 0;
2716+ if (v -= 1) {
2717+ arith_encode(cinfo, st, 1);
2718+ m = 1;
2719+ v2 = v;
2720+ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
2721+ while (v2 >>= 1) {
2722+ arith_encode(cinfo, st, 1);
2723+ m <<= 1;
2724+ st += 1;
2725+ }
2726+ }
2727+ arith_encode(cinfo, st, 0);
2728+ /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
2729+ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
2730+ entropy->dc_context[ci] = 0; /* zero diff category */
2731+ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
2732+ entropy->dc_context[ci] += 8; /* large diff category */
2733+ /* Figure F.9: Encoding the magnitude bit pattern of v */
2734+ st += 14;
2735+ while (m >>= 1)
2736+ arith_encode(cinfo, st, (m & v) ? 1 : 0);
2737+ }
2738+
2739+ /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
2740+
2741+ tbl = compptr->ac_tbl_no;
2742+
2743+ /* Establish EOB (end-of-block) index */
2744+ for (ke = DCTSIZE2 - 1; ke > 0; ke--)
2745+ if ((*block)[jpeg_natural_order[ke]]) break;
2746+
2747+ /* Figure F.5: Encode_AC_Coefficients */
2748+ for (k = 1; k <= ke; k++) {
2749+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2750+ arith_encode(cinfo, st, 0); /* EOB decision */
2751+ while ((v = (*block)[jpeg_natural_order[k]]) == 0) {
2752+ arith_encode(cinfo, st + 1, 0); st += 3; k++;
2753+ }
2754+ arith_encode(cinfo, st + 1, 1);
2755+ /* Figure F.6: Encoding nonzero value v */
2756+ /* Figure F.7: Encoding the sign of v */
2757+ if (v > 0) {
2758+ arith_encode(cinfo, entropy->fixed_bin, 0);
2759+ } else {
2760+ v = -v;
2761+ arith_encode(cinfo, entropy->fixed_bin, 1);
2762+ }
2763+ st += 2;
2764+ /* Figure F.8: Encoding the magnitude category of v */
2765+ m = 0;
2766+ if (v -= 1) {
2767+ arith_encode(cinfo, st, 1);
2768+ m = 1;
2769+ v2 = v;
2770+ if (v2 >>= 1) {
2771+ arith_encode(cinfo, st, 1);
2772+ m <<= 1;
2773+ st = entropy->ac_stats[tbl] +
2774+ (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
2775+ while (v2 >>= 1) {
2776+ arith_encode(cinfo, st, 1);
2777+ m <<= 1;
2778+ st += 1;
2779+ }
2780+ }
2781+ }
2782+ arith_encode(cinfo, st, 0);
2783+ /* Figure F.9: Encoding the magnitude bit pattern of v */
2784+ st += 14;
2785+ while (m >>= 1)
2786+ arith_encode(cinfo, st, (m & v) ? 1 : 0);
2787+ }
2788+ /* Encode EOB decision only if k <= DCTSIZE2 - 1 */
2789+ if (k <= DCTSIZE2 - 1) {
2790+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
2791+ arith_encode(cinfo, st, 1);
2792+ }
2793+ }
2794+
2795+ return TRUE;
2796+}
2797+
2798+
2799+/*
2800+ * Initialize for an arithmetic-compressed scan.
2801+ */
2802+
2803+METHODDEF(void)
2804+start_pass (j_compress_ptr cinfo, boolean gather_statistics)
2805+{
2806+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
2807+ int ci, tbl;
2808+ jpeg_component_info * compptr;
2809+
2810+ if (gather_statistics)
2811+ /* Make sure to avoid that in the master control logic!
2812+ * We are fully adaptive here and need no extra
2813+ * statistics gathering pass!
2814+ */
2815+ ERREXIT(cinfo, JERR_NOT_COMPILED);
2816+
2817+ /* We assume jcmaster.c already validated the progressive scan parameters. */
2818+
2819+ /* Select execution routines */
2820+ if (cinfo->progressive_mode) {
2821+ if (cinfo->Ah == 0) {
2822+ if (cinfo->Ss == 0)
2823+ entropy->pub.encode_mcu = encode_mcu_DC_first;
2824+ else
2825+ entropy->pub.encode_mcu = encode_mcu_AC_first;
2826+ } else {
2827+ if (cinfo->Ss == 0)
2828+ entropy->pub.encode_mcu = encode_mcu_DC_refine;
2829+ else
2830+ entropy->pub.encode_mcu = encode_mcu_AC_refine;
2831+ }
2832+ } else
2833+ entropy->pub.encode_mcu = encode_mcu;
2834+
2835+ /* Allocate & initialize requested statistics areas */
2836+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
2837+ compptr = cinfo->cur_comp_info[ci];
2838+ /* DC needs no table for refinement scan */
2839+ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
2840+ tbl = compptr->dc_tbl_no;
2841+ if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
2842+ ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
2843+ if (entropy->dc_stats[tbl] == NULL)
2844+ entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
2845+ ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
2846+ MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
2847+ /* Initialize DC predictions to 0 */
2848+ entropy->last_dc_val[ci] = 0;
2849+ entropy->dc_context[ci] = 0;
2850+ }
2851+ /* AC needs no table when not present */
2852+ if (cinfo->progressive_mode == 0 || cinfo->Se) {
2853+ tbl = compptr->ac_tbl_no;
2854+ if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
2855+ ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
2856+ if (entropy->ac_stats[tbl] == NULL)
2857+ entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
2858+ ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
2859+ MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
2860+#ifdef CALCULATE_SPECTRAL_CONDITIONING
2861+ if (cinfo->progressive_mode)
2862+ /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
2863+ cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4);
2864+#endif
2865+ }
2866+ }
2867+
2868+ /* Initialize arithmetic encoding variables */
2869+ entropy->c = 0;
2870+ entropy->a = 0x10000L;
2871+ entropy->sc = 0;
2872+ entropy->zc = 0;
2873+ entropy->ct = 11;
2874+ entropy->buffer = -1; /* empty */
2875+
2876+ /* Initialize restart stuff */
2877+ entropy->restarts_to_go = cinfo->restart_interval;
2878+ entropy->next_restart_num = 0;
2879+}
2880+
2881+
2882+/*
2883+ * Module initialization routine for arithmetic entropy encoding.
2884+ */
2885+
2886+GLOBAL(void)
2887+jinit_arith_encoder (j_compress_ptr cinfo)
2888+{
2889+ arith_entropy_ptr entropy;
2890+ int i;
2891+
2892+ entropy = (arith_entropy_ptr)
2893+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2894+ SIZEOF(arith_entropy_encoder));
2895+ cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
2896+ entropy->pub.start_pass = start_pass;
2897+ entropy->pub.finish_pass = finish_pass;
2898+
2899+ /* Mark tables unallocated */
2900+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
2901+ entropy->dc_stats[i] = NULL;
2902+ entropy->ac_stats[i] = NULL;
2903+ }
2904+
2905+ /* Initialize index for fixed probability estimation */
2906+ entropy->fixed_bin[0] = 113;
2907+}
2908
2909=== added file 'src/libjpeg-turbo/jccoefct.c'
2910--- src/libjpeg-turbo/jccoefct.c 1970-01-01 00:00:00 +0000
2911+++ src/libjpeg-turbo/jccoefct.c 2012-06-27 16:20:24 +0000
2912@@ -0,0 +1,449 @@
2913+/*
2914+ * jccoefct.c
2915+ *
2916+ * Copyright (C) 1994-1997, Thomas G. Lane.
2917+ * This file is part of the Independent JPEG Group's software.
2918+ * For conditions of distribution and use, see the accompanying README file.
2919+ *
2920+ * This file contains the coefficient buffer controller for compression.
2921+ * This controller is the top level of the JPEG compressor proper.
2922+ * The coefficient buffer lies between forward-DCT and entropy encoding steps.
2923+ */
2924+
2925+#define JPEG_INTERNALS
2926+#include "jinclude.h"
2927+#include "jpeglib.h"
2928+
2929+
2930+/* We use a full-image coefficient buffer when doing Huffman optimization,
2931+ * and also for writing multiple-scan JPEG files. In all cases, the DCT
2932+ * step is run during the first pass, and subsequent passes need only read
2933+ * the buffered coefficients.
2934+ */
2935+#ifdef ENTROPY_OPT_SUPPORTED
2936+#define FULL_COEF_BUFFER_SUPPORTED
2937+#else
2938+#ifdef C_MULTISCAN_FILES_SUPPORTED
2939+#define FULL_COEF_BUFFER_SUPPORTED
2940+#endif
2941+#endif
2942+
2943+
2944+/* Private buffer controller object */
2945+
2946+typedef struct {
2947+ struct jpeg_c_coef_controller pub; /* public fields */
2948+
2949+ JDIMENSION iMCU_row_num; /* iMCU row # within image */
2950+ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
2951+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
2952+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
2953+
2954+ /* For single-pass compression, it's sufficient to buffer just one MCU
2955+ * (although this may prove a bit slow in practice). We allocate a
2956+ * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
2957+ * MCU constructed and sent. (On 80x86, the workspace is FAR even though
2958+ * it's not really very big; this is to keep the module interfaces unchanged
2959+ * when a large coefficient buffer is necessary.)
2960+ * In multi-pass modes, this array points to the current MCU's blocks
2961+ * within the virtual arrays.
2962+ */
2963+ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
2964+
2965+ /* In multi-pass modes, we need a virtual block array for each component. */
2966+ jvirt_barray_ptr whole_image[MAX_COMPONENTS];
2967+} my_coef_controller;
2968+
2969+typedef my_coef_controller * my_coef_ptr;
2970+
2971+
2972+/* Forward declarations */
2973+METHODDEF(boolean) compress_data
2974+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
2975+#ifdef FULL_COEF_BUFFER_SUPPORTED
2976+METHODDEF(boolean) compress_first_pass
2977+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
2978+METHODDEF(boolean) compress_output
2979+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
2980+#endif
2981+
2982+
2983+LOCAL(void)
2984+start_iMCU_row (j_compress_ptr cinfo)
2985+/* Reset within-iMCU-row counters for a new row */
2986+{
2987+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
2988+
2989+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
2990+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
2991+ * But at the bottom of the image, process only what's left.
2992+ */
2993+ if (cinfo->comps_in_scan > 1) {
2994+ coef->MCU_rows_per_iMCU_row = 1;
2995+ } else {
2996+ if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
2997+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
2998+ else
2999+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
3000+ }
3001+
3002+ coef->mcu_ctr = 0;
3003+ coef->MCU_vert_offset = 0;
3004+}
3005+
3006+
3007+/*
3008+ * Initialize for a processing pass.
3009+ */
3010+
3011+METHODDEF(void)
3012+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
3013+{
3014+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
3015+
3016+ coef->iMCU_row_num = 0;
3017+ start_iMCU_row(cinfo);
3018+
3019+ switch (pass_mode) {
3020+ case JBUF_PASS_THRU:
3021+ if (coef->whole_image[0] != NULL)
3022+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
3023+ coef->pub.compress_data = compress_data;
3024+ break;
3025+#ifdef FULL_COEF_BUFFER_SUPPORTED
3026+ case JBUF_SAVE_AND_PASS:
3027+ if (coef->whole_image[0] == NULL)
3028+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
3029+ coef->pub.compress_data = compress_first_pass;
3030+ break;
3031+ case JBUF_CRANK_DEST:
3032+ if (coef->whole_image[0] == NULL)
3033+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
3034+ coef->pub.compress_data = compress_output;
3035+ break;
3036+#endif
3037+ default:
3038+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
3039+ break;
3040+ }
3041+}
3042+
3043+
3044+/*
3045+ * Process some data in the single-pass case.
3046+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
3047+ * per call, ie, v_samp_factor block rows for each component in the image.
3048+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
3049+ *
3050+ * NB: input_buf contains a plane for each component in image,
3051+ * which we index according to the component's SOF position.
3052+ */
3053+
3054+METHODDEF(boolean)
3055+compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
3056+{
3057+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
3058+ JDIMENSION MCU_col_num; /* index of current MCU within row */
3059+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
3060+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
3061+ int blkn, bi, ci, yindex, yoffset, blockcnt;
3062+ JDIMENSION ypos, xpos;
3063+ jpeg_component_info *compptr;
3064+
3065+ /* Loop to write as much as one whole iMCU row */
3066+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
3067+ yoffset++) {
3068+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
3069+ MCU_col_num++) {
3070+ /* Determine where data comes from in input_buf and do the DCT thing.
3071+ * Each call on forward_DCT processes a horizontal row of DCT blocks
3072+ * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
3073+ * sequentially. Dummy blocks at the right or bottom edge are filled in
3074+ * specially. The data in them does not matter for image reconstruction,
3075+ * so we fill them with values that will encode to the smallest amount of
3076+ * data, viz: all zeroes in the AC entries, DC entries equal to previous
3077+ * block's DC value. (Thanks to Thomas Kinsman for this idea.)
3078+ */
3079+ blkn = 0;
3080+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
3081+ compptr = cinfo->cur_comp_info[ci];
3082+ blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
3083+ : compptr->last_col_width;
3084+ xpos = MCU_col_num * compptr->MCU_sample_width;
3085+ ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
3086+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
3087+ if (coef->iMCU_row_num < last_iMCU_row ||
3088+ yoffset+yindex < compptr->last_row_height) {
3089+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
3090+ input_buf[compptr->component_index],
3091+ coef->MCU_buffer[blkn],
3092+ ypos, xpos, (JDIMENSION) blockcnt);
3093+ if (blockcnt < compptr->MCU_width) {
3094+ /* Create some dummy blocks at the right edge of the image. */
3095+ jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
3096+ (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
3097+ for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
3098+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
3099+ }
3100+ }
3101+ } else {
3102+ /* Create a row of dummy blocks at the bottom of the image. */
3103+ jzero_far((void FAR *) coef->MCU_buffer[blkn],
3104+ compptr->MCU_width * SIZEOF(JBLOCK));
3105+ for (bi = 0; bi < compptr->MCU_width; bi++) {
3106+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
3107+ }
3108+ }
3109+ blkn += compptr->MCU_width;
3110+ ypos += DCTSIZE;
3111+ }
3112+ }
3113+ /* Try to write the MCU. In event of a suspension failure, we will
3114+ * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
3115+ */
3116+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
3117+ /* Suspension forced; update state counters and exit */
3118+ coef->MCU_vert_offset = yoffset;
3119+ coef->mcu_ctr = MCU_col_num;
3120+ return FALSE;
3121+ }
3122+ }
3123+ /* Completed an MCU row, but perhaps not an iMCU row */
3124+ coef->mcu_ctr = 0;
3125+ }
3126+ /* Completed the iMCU row, advance counters for next one */
3127+ coef->iMCU_row_num++;
3128+ start_iMCU_row(cinfo);
3129+ return TRUE;
3130+}
3131+
3132+
3133+#ifdef FULL_COEF_BUFFER_SUPPORTED
3134+
3135+/*
3136+ * Process some data in the first pass of a multi-pass case.
3137+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
3138+ * per call, ie, v_samp_factor block rows for each component in the image.
3139+ * This amount of data is read from the source buffer, DCT'd and quantized,
3140+ * and saved into the virtual arrays. We also generate suitable dummy blocks
3141+ * as needed at the right and lower edges. (The dummy blocks are constructed
3142+ * in the virtual arrays, which have been padded appropriately.) This makes
3143+ * it possible for subsequent passes not to worry about real vs. dummy blocks.
3144+ *
3145+ * We must also emit the data to the entropy encoder. This is conveniently
3146+ * done by calling compress_output() after we've loaded the current strip
3147+ * of the virtual arrays.
3148+ *
3149+ * NB: input_buf contains a plane for each component in image. All
3150+ * components are DCT'd and loaded into the virtual arrays in this pass.
3151+ * However, it may be that only a subset of the components are emitted to
3152+ * the entropy encoder during this first pass; be careful about looking
3153+ * at the scan-dependent variables (MCU dimensions, etc).
3154+ */
3155+
3156+METHODDEF(boolean)
3157+compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
3158+{
3159+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
3160+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
3161+ JDIMENSION blocks_across, MCUs_across, MCUindex;
3162+ int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
3163+ JCOEF lastDC;
3164+ jpeg_component_info *compptr;
3165+ JBLOCKARRAY buffer;
3166+ JBLOCKROW thisblockrow, lastblockrow;
3167+
3168+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
3169+ ci++, compptr++) {
3170+ /* Align the virtual buffer for this component. */
3171+ buffer = (*cinfo->mem->access_virt_barray)
3172+ ((j_common_ptr) cinfo, coef->whole_image[ci],
3173+ coef->iMCU_row_num * compptr->v_samp_factor,
3174+ (JDIMENSION) compptr->v_samp_factor, TRUE);
3175+ /* Count non-dummy DCT block rows in this iMCU row. */
3176+ if (coef->iMCU_row_num < last_iMCU_row)
3177+ block_rows = compptr->v_samp_factor;
3178+ else {
3179+ /* NB: can't use last_row_height here, since may not be set! */
3180+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
3181+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
3182+ }
3183+ blocks_across = compptr->width_in_blocks;
3184+ h_samp_factor = compptr->h_samp_factor;
3185+ /* Count number of dummy blocks to be added at the right margin. */
3186+ ndummy = (int) (blocks_across % h_samp_factor);
3187+ if (ndummy > 0)
3188+ ndummy = h_samp_factor - ndummy;
3189+ /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
3190+ * on forward_DCT processes a complete horizontal row of DCT blocks.
3191+ */
3192+ for (block_row = 0; block_row < block_rows; block_row++) {
3193+ thisblockrow = buffer[block_row];
3194+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
3195+ input_buf[ci], thisblockrow,
3196+ (JDIMENSION) (block_row * DCTSIZE),
3197+ (JDIMENSION) 0, blocks_across);
3198+ if (ndummy > 0) {
3199+ /* Create dummy blocks at the right edge of the image. */
3200+ thisblockrow += blocks_across; /* => first dummy block */
3201+ jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
3202+ lastDC = thisblockrow[-1][0];
3203+ for (bi = 0; bi < ndummy; bi++) {
3204+ thisblockrow[bi][0] = lastDC;
3205+ }
3206+ }
3207+ }
3208+ /* If at end of image, create dummy block rows as needed.
3209+ * The tricky part here is that within each MCU, we want the DC values
3210+ * of the dummy blocks to match the last real block's DC value.
3211+ * This squeezes a few more bytes out of the resulting file...
3212+ */
3213+ if (coef->iMCU_row_num == last_iMCU_row) {
3214+ blocks_across += ndummy; /* include lower right corner */
3215+ MCUs_across = blocks_across / h_samp_factor;
3216+ for (block_row = block_rows; block_row < compptr->v_samp_factor;
3217+ block_row++) {
3218+ thisblockrow = buffer[block_row];
3219+ lastblockrow = buffer[block_row-1];
3220+ jzero_far((void FAR *) thisblockrow,
3221+ (size_t) (blocks_across * SIZEOF(JBLOCK)));
3222+ for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
3223+ lastDC = lastblockrow[h_samp_factor-1][0];
3224+ for (bi = 0; bi < h_samp_factor; bi++) {
3225+ thisblockrow[bi][0] = lastDC;
3226+ }
3227+ thisblockrow += h_samp_factor; /* advance to next MCU in row */
3228+ lastblockrow += h_samp_factor;
3229+ }
3230+ }
3231+ }
3232+ }
3233+ /* NB: compress_output will increment iMCU_row_num if successful.
3234+ * A suspension return will result in redoing all the work above next time.
3235+ */
3236+
3237+ /* Emit data to the entropy encoder, sharing code with subsequent passes */
3238+ return compress_output(cinfo, input_buf);
3239+}
3240+
3241+
3242+/*
3243+ * Process some data in subsequent passes of a multi-pass case.
3244+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
3245+ * per call, ie, v_samp_factor block rows for each component in the scan.
3246+ * The data is obtained from the virtual arrays and fed to the entropy coder.
3247+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
3248+ *
3249+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
3250+ */
3251+
3252+METHODDEF(boolean)
3253+compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
3254+{
3255+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
3256+ JDIMENSION MCU_col_num; /* index of current MCU within row */
3257+ int blkn, ci, xindex, yindex, yoffset;
3258+ JDIMENSION start_col;
3259+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
3260+ JBLOCKROW buffer_ptr;
3261+ jpeg_component_info *compptr;
3262+
3263+ /* Align the virtual buffers for the components used in this scan.
3264+ * NB: during first pass, this is safe only because the buffers will
3265+ * already be aligned properly, so jmemmgr.c won't need to do any I/O.
3266+ */
3267+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
3268+ compptr = cinfo->cur_comp_info[ci];
3269+ buffer[ci] = (*cinfo->mem->access_virt_barray)
3270+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
3271+ coef->iMCU_row_num * compptr->v_samp_factor,
3272+ (JDIMENSION) compptr->v_samp_factor, FALSE);
3273+ }
3274+
3275+ /* Loop to process one whole iMCU row */
3276+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
3277+ yoffset++) {
3278+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
3279+ MCU_col_num++) {
3280+ /* Construct list of pointers to DCT blocks belonging to this MCU */
3281+ blkn = 0; /* index of current DCT block within MCU */
3282+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
3283+ compptr = cinfo->cur_comp_info[ci];
3284+ start_col = MCU_col_num * compptr->MCU_width;
3285+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
3286+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
3287+ for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
3288+ coef->MCU_buffer[blkn++] = buffer_ptr++;
3289+ }
3290+ }
3291+ }
3292+ /* Try to write the MCU. */
3293+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
3294+ /* Suspension forced; update state counters and exit */
3295+ coef->MCU_vert_offset = yoffset;
3296+ coef->mcu_ctr = MCU_col_num;
3297+ return FALSE;
3298+ }
3299+ }
3300+ /* Completed an MCU row, but perhaps not an iMCU row */
3301+ coef->mcu_ctr = 0;
3302+ }
3303+ /* Completed the iMCU row, advance counters for next one */
3304+ coef->iMCU_row_num++;
3305+ start_iMCU_row(cinfo);
3306+ return TRUE;
3307+}
3308+
3309+#endif /* FULL_COEF_BUFFER_SUPPORTED */
3310+
3311+
3312+/*
3313+ * Initialize coefficient buffer controller.
3314+ */
3315+
3316+GLOBAL(void)
3317+jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
3318+{
3319+ my_coef_ptr coef;
3320+
3321+ coef = (my_coef_ptr)
3322+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3323+ SIZEOF(my_coef_controller));
3324+ cinfo->coef = (struct jpeg_c_coef_controller *) coef;
3325+ coef->pub.start_pass = start_pass_coef;
3326+
3327+ /* Create the coefficient buffer. */
3328+ if (need_full_buffer) {
3329+#ifdef FULL_COEF_BUFFER_SUPPORTED
3330+ /* Allocate a full-image virtual array for each component, */
3331+ /* padded to a multiple of samp_factor DCT blocks in each direction. */
3332+ int ci;
3333+ jpeg_component_info *compptr;
3334+
3335+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
3336+ ci++, compptr++) {
3337+ coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
3338+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
3339+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
3340+ (long) compptr->h_samp_factor),
3341+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
3342+ (long) compptr->v_samp_factor),
3343+ (JDIMENSION) compptr->v_samp_factor);
3344+ }
3345+#else
3346+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
3347+#endif
3348+ } else {
3349+ /* We only need a single-MCU buffer. */
3350+ JBLOCKROW buffer;
3351+ int i;
3352+
3353+ buffer = (JBLOCKROW)
3354+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3355+ C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
3356+ for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
3357+ coef->MCU_buffer[i] = buffer + i;
3358+ }
3359+ coef->whole_image[0] = NULL; /* flag for no virtual arrays */
3360+ }
3361+}
3362
3363=== added file 'src/libjpeg-turbo/jccolext.c.inc'
3364--- src/libjpeg-turbo/jccolext.c.inc 1970-01-01 00:00:00 +0000
3365+++ src/libjpeg-turbo/jccolext.c.inc 2012-06-27 16:20:24 +0000
3366@@ -0,0 +1,114 @@
3367+/*
3368+ * jccolext.c
3369+ *
3370+ * Copyright (C) 1991-1996, Thomas G. Lane.
3371+ * Copyright (C) 2009-2011, D. R. Commander.
3372+ * This file is part of the Independent JPEG Group's software.
3373+ * For conditions of distribution and use, see the accompanying README file.
3374+ *
3375+ * This file contains input colorspace conversion routines.
3376+ */
3377+
3378+
3379+/* This file is included by jccolor.c */
3380+
3381+
3382+/*
3383+ * Convert some rows of samples to the JPEG colorspace.
3384+ *
3385+ * Note that we change from the application's interleaved-pixel format
3386+ * to our internal noninterleaved, one-plane-per-component format.
3387+ * The input buffer is therefore three times as wide as the output buffer.
3388+ *
3389+ * A starting row offset is provided only for the output buffer. The caller
3390+ * can easily adjust the passed input_buf value to accommodate any row
3391+ * offset required on that side.
3392+ */
3393+
3394+INLINE
3395+LOCAL(void)
3396+rgb_ycc_convert_internal (j_compress_ptr cinfo,
3397+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3398+ JDIMENSION output_row, int num_rows)
3399+{
3400+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3401+ register int r, g, b;
3402+ register INT32 * ctab = cconvert->rgb_ycc_tab;
3403+ register JSAMPROW inptr;
3404+ register JSAMPROW outptr0, outptr1, outptr2;
3405+ register JDIMENSION col;
3406+ JDIMENSION num_cols = cinfo->image_width;
3407+
3408+ while (--num_rows >= 0) {
3409+ inptr = *input_buf++;
3410+ outptr0 = output_buf[0][output_row];
3411+ outptr1 = output_buf[1][output_row];
3412+ outptr2 = output_buf[2][output_row];
3413+ output_row++;
3414+ for (col = 0; col < num_cols; col++) {
3415+ r = GETJSAMPLE(inptr[RGB_RED]);
3416+ g = GETJSAMPLE(inptr[RGB_GREEN]);
3417+ b = GETJSAMPLE(inptr[RGB_BLUE]);
3418+ inptr += RGB_PIXELSIZE;
3419+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
3420+ * must be too; we do not need an explicit range-limiting operation.
3421+ * Hence the value being shifted is never negative, and we don't
3422+ * need the general RIGHT_SHIFT macro.
3423+ */
3424+ /* Y */
3425+ outptr0[col] = (JSAMPLE)
3426+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
3427+ >> SCALEBITS);
3428+ /* Cb */
3429+ outptr1[col] = (JSAMPLE)
3430+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
3431+ >> SCALEBITS);
3432+ /* Cr */
3433+ outptr2[col] = (JSAMPLE)
3434+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
3435+ >> SCALEBITS);
3436+ }
3437+ }
3438+}
3439+
3440+
3441+/**************** Cases other than RGB -> YCbCr **************/
3442+
3443+
3444+/*
3445+ * Convert some rows of samples to the JPEG colorspace.
3446+ * This version handles RGB->grayscale conversion, which is the same
3447+ * as the RGB->Y portion of RGB->YCbCr.
3448+ * We assume rgb_ycc_start has been called (we only use the Y tables).
3449+ */
3450+
3451+INLINE
3452+LOCAL(void)
3453+rgb_gray_convert_internal (j_compress_ptr cinfo,
3454+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3455+ JDIMENSION output_row, int num_rows)
3456+{
3457+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3458+ register int r, g, b;
3459+ register INT32 * ctab = cconvert->rgb_ycc_tab;
3460+ register JSAMPROW inptr;
3461+ register JSAMPROW outptr;
3462+ register JDIMENSION col;
3463+ JDIMENSION num_cols = cinfo->image_width;
3464+
3465+ while (--num_rows >= 0) {
3466+ inptr = *input_buf++;
3467+ outptr = output_buf[0][output_row];
3468+ output_row++;
3469+ for (col = 0; col < num_cols; col++) {
3470+ r = GETJSAMPLE(inptr[RGB_RED]);
3471+ g = GETJSAMPLE(inptr[RGB_GREEN]);
3472+ b = GETJSAMPLE(inptr[RGB_BLUE]);
3473+ inptr += RGB_PIXELSIZE;
3474+ /* Y */
3475+ outptr[col] = (JSAMPLE)
3476+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
3477+ >> SCALEBITS);
3478+ }
3479+ }
3480+}
3481
3482=== added file 'src/libjpeg-turbo/jccolor.c'
3483--- src/libjpeg-turbo/jccolor.c 1970-01-01 00:00:00 +0000
3484+++ src/libjpeg-turbo/jccolor.c 2012-06-27 16:20:24 +0000
3485@@ -0,0 +1,599 @@
3486+/*
3487+ * jccolor.c
3488+ *
3489+ * Copyright (C) 1991-1996, Thomas G. Lane.
3490+ * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
3491+ * Copyright (C) 2009-2011, D. R. Commander.
3492+ * This file is part of the Independent JPEG Group's software.
3493+ * For conditions of distribution and use, see the accompanying README file.
3494+ *
3495+ * This file contains input colorspace conversion routines.
3496+ */
3497+
3498+#define JPEG_INTERNALS
3499+#include "jinclude.h"
3500+#include "jpeglib.h"
3501+#include "jsimd.h"
3502+#include "config.h"
3503+
3504+
3505+/* Private subobject */
3506+
3507+typedef struct {
3508+ struct jpeg_color_converter pub; /* public fields */
3509+
3510+ /* Private state for RGB->YCC conversion */
3511+ INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
3512+} my_color_converter;
3513+
3514+typedef my_color_converter * my_cconvert_ptr;
3515+
3516+
3517+/**************** RGB -> YCbCr conversion: most common case **************/
3518+
3519+/*
3520+ * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
3521+ * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
3522+ * The conversion equations to be implemented are therefore
3523+ * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
3524+ * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
3525+ * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
3526+ * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
3527+ * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
3528+ * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
3529+ * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
3530+ * were not represented exactly. Now we sacrifice exact representation of
3531+ * maximum red and maximum blue in order to get exact grayscales.
3532+ *
3533+ * To avoid floating-point arithmetic, we represent the fractional constants
3534+ * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
3535+ * the products by 2^16, with appropriate rounding, to get the correct answer.
3536+ *
3537+ * For even more speed, we avoid doing any multiplications in the inner loop
3538+ * by precalculating the constants times R,G,B for all possible values.
3539+ * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
3540+ * for 12-bit samples it is still acceptable. It's not very reasonable for
3541+ * 16-bit samples, but if you want lossless storage you shouldn't be changing
3542+ * colorspace anyway.
3543+ * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
3544+ * in the tables to save adding them separately in the inner loop.
3545+ */
3546+
3547+#define SCALEBITS 16 /* speediest right-shift on some machines */
3548+#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
3549+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
3550+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
3551+
3552+/* We allocate one big table and divide it up into eight parts, instead of
3553+ * doing eight alloc_small requests. This lets us use a single table base
3554+ * address, which can be held in a register in the inner loops on many
3555+ * machines (more than can hold all eight addresses, anyway).
3556+ */
3557+
3558+#define R_Y_OFF 0 /* offset to R => Y section */
3559+#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
3560+#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
3561+#define R_CB_OFF (3*(MAXJSAMPLE+1))
3562+#define G_CB_OFF (4*(MAXJSAMPLE+1))
3563+#define B_CB_OFF (5*(MAXJSAMPLE+1))
3564+#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
3565+#define G_CR_OFF (6*(MAXJSAMPLE+1))
3566+#define B_CR_OFF (7*(MAXJSAMPLE+1))
3567+#define TABLE_SIZE (8*(MAXJSAMPLE+1))
3568+
3569+
3570+/* Include inline routines for colorspace extensions */
3571+
3572+#include "jccolext.c.inc"
3573+#undef RGB_RED
3574+#undef RGB_GREEN
3575+#undef RGB_BLUE
3576+#undef RGB_PIXELSIZE
3577+
3578+#define RGB_RED EXT_RGB_RED
3579+#define RGB_GREEN EXT_RGB_GREEN
3580+#define RGB_BLUE EXT_RGB_BLUE
3581+#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
3582+#define rgb_ycc_convert_internal extrgb_ycc_convert_internal
3583+#define rgb_gray_convert_internal extrgb_gray_convert_internal
3584+#include "jccolext.c.inc"
3585+#undef RGB_RED
3586+#undef RGB_GREEN
3587+#undef RGB_BLUE
3588+#undef RGB_PIXELSIZE
3589+#undef rgb_ycc_convert_internal
3590+#undef rgb_gray_convert_internal
3591+
3592+#define RGB_RED EXT_RGBX_RED
3593+#define RGB_GREEN EXT_RGBX_GREEN
3594+#define RGB_BLUE EXT_RGBX_BLUE
3595+#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
3596+#define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
3597+#define rgb_gray_convert_internal extrgbx_gray_convert_internal
3598+#include "jccolext.c.inc"
3599+#undef RGB_RED
3600+#undef RGB_GREEN
3601+#undef RGB_BLUE
3602+#undef RGB_PIXELSIZE
3603+#undef rgb_ycc_convert_internal
3604+#undef rgb_gray_convert_internal
3605+
3606+#define RGB_RED EXT_BGR_RED
3607+#define RGB_GREEN EXT_BGR_GREEN
3608+#define RGB_BLUE EXT_BGR_BLUE
3609+#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
3610+#define rgb_ycc_convert_internal extbgr_ycc_convert_internal
3611+#define rgb_gray_convert_internal extbgr_gray_convert_internal
3612+#include "jccolext.c.inc"
3613+#undef RGB_RED
3614+#undef RGB_GREEN
3615+#undef RGB_BLUE
3616+#undef RGB_PIXELSIZE
3617+#undef rgb_ycc_convert_internal
3618+#undef rgb_gray_convert_internal
3619+
3620+#define RGB_RED EXT_BGRX_RED
3621+#define RGB_GREEN EXT_BGRX_GREEN
3622+#define RGB_BLUE EXT_BGRX_BLUE
3623+#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
3624+#define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
3625+#define rgb_gray_convert_internal extbgrx_gray_convert_internal
3626+#include "jccolext.c.inc"
3627+#undef RGB_RED
3628+#undef RGB_GREEN
3629+#undef RGB_BLUE
3630+#undef RGB_PIXELSIZE
3631+#undef rgb_ycc_convert_internal
3632+#undef rgb_gray_convert_internal
3633+
3634+#define RGB_RED EXT_XBGR_RED
3635+#define RGB_GREEN EXT_XBGR_GREEN
3636+#define RGB_BLUE EXT_XBGR_BLUE
3637+#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
3638+#define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
3639+#define rgb_gray_convert_internal extxbgr_gray_convert_internal
3640+#include "jccolext.c.inc"
3641+#undef RGB_RED
3642+#undef RGB_GREEN
3643+#undef RGB_BLUE
3644+#undef RGB_PIXELSIZE
3645+#undef rgb_ycc_convert_internal
3646+#undef rgb_gray_convert_internal
3647+
3648+#define RGB_RED EXT_XRGB_RED
3649+#define RGB_GREEN EXT_XRGB_GREEN
3650+#define RGB_BLUE EXT_XRGB_BLUE
3651+#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
3652+#define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
3653+#define rgb_gray_convert_internal extxrgb_gray_convert_internal
3654+#include "jccolext.c.inc"
3655+#undef RGB_RED
3656+#undef RGB_GREEN
3657+#undef RGB_BLUE
3658+#undef RGB_PIXELSIZE
3659+#undef rgb_ycc_convert_internal
3660+#undef rgb_gray_convert_internal
3661+
3662+
3663+/*
3664+ * Initialize for RGB->YCC colorspace conversion.
3665+ */
3666+
3667+METHODDEF(void)
3668+rgb_ycc_start (j_compress_ptr cinfo)
3669+{
3670+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3671+ INT32 * rgb_ycc_tab;
3672+ INT32 i;
3673+
3674+ /* Allocate and fill in the conversion tables. */
3675+ cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
3676+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3677+ (TABLE_SIZE * SIZEOF(INT32)));
3678+
3679+ for (i = 0; i <= MAXJSAMPLE; i++) {
3680+ rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
3681+ rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
3682+ rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
3683+ rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
3684+ rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
3685+ /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
3686+ * This ensures that the maximum output will round to MAXJSAMPLE
3687+ * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
3688+ */
3689+ rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
3690+/* B=>Cb and R=>Cr tables are the same
3691+ rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
3692+*/
3693+ rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
3694+ rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
3695+ }
3696+}
3697+
3698+
3699+/*
3700+ * Convert some rows of samples to the JPEG colorspace.
3701+ */
3702+
3703+METHODDEF(void)
3704+rgb_ycc_convert (j_compress_ptr cinfo,
3705+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3706+ JDIMENSION output_row, int num_rows)
3707+{
3708+ switch (cinfo->in_color_space) {
3709+ case JCS_EXT_RGB:
3710+ extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3711+ num_rows);
3712+ break;
3713+ case JCS_EXT_RGBX:
3714+ case JCS_EXT_RGBA:
3715+ extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3716+ num_rows);
3717+ break;
3718+ case JCS_EXT_BGR:
3719+ extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3720+ num_rows);
3721+ break;
3722+ case JCS_EXT_BGRX:
3723+ case JCS_EXT_BGRA:
3724+ extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3725+ num_rows);
3726+ break;
3727+ case JCS_EXT_XBGR:
3728+ case JCS_EXT_ABGR:
3729+ extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3730+ num_rows);
3731+ break;
3732+ case JCS_EXT_XRGB:
3733+ case JCS_EXT_ARGB:
3734+ extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3735+ num_rows);
3736+ break;
3737+ default:
3738+ rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
3739+ num_rows);
3740+ break;
3741+ }
3742+}
3743+
3744+
3745+/**************** Cases other than RGB -> YCbCr **************/
3746+
3747+
3748+/*
3749+ * Convert some rows of samples to the JPEG colorspace.
3750+ */
3751+
3752+METHODDEF(void)
3753+rgb_gray_convert (j_compress_ptr cinfo,
3754+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3755+ JDIMENSION output_row, int num_rows)
3756+{
3757+ switch (cinfo->in_color_space) {
3758+ case JCS_EXT_RGB:
3759+ extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3760+ num_rows);
3761+ break;
3762+ case JCS_EXT_RGBX:
3763+ case JCS_EXT_RGBA:
3764+ extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3765+ num_rows);
3766+ break;
3767+ case JCS_EXT_BGR:
3768+ extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3769+ num_rows);
3770+ break;
3771+ case JCS_EXT_BGRX:
3772+ case JCS_EXT_BGRA:
3773+ extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3774+ num_rows);
3775+ break;
3776+ case JCS_EXT_XBGR:
3777+ case JCS_EXT_ABGR:
3778+ extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3779+ num_rows);
3780+ break;
3781+ case JCS_EXT_XRGB:
3782+ case JCS_EXT_ARGB:
3783+ extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3784+ num_rows);
3785+ break;
3786+ default:
3787+ rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
3788+ num_rows);
3789+ break;
3790+ }
3791+}
3792+
3793+
3794+/*
3795+ * Convert some rows of samples to the JPEG colorspace.
3796+ * This version handles Adobe-style CMYK->YCCK conversion,
3797+ * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
3798+ * conversion as above, while passing K (black) unchanged.
3799+ * We assume rgb_ycc_start has been called.
3800+ */
3801+
3802+METHODDEF(void)
3803+cmyk_ycck_convert (j_compress_ptr cinfo,
3804+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3805+ JDIMENSION output_row, int num_rows)
3806+{
3807+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3808+ register int r, g, b;
3809+ register INT32 * ctab = cconvert->rgb_ycc_tab;
3810+ register JSAMPROW inptr;
3811+ register JSAMPROW outptr0, outptr1, outptr2, outptr3;
3812+ register JDIMENSION col;
3813+ JDIMENSION num_cols = cinfo->image_width;
3814+
3815+ while (--num_rows >= 0) {
3816+ inptr = *input_buf++;
3817+ outptr0 = output_buf[0][output_row];
3818+ outptr1 = output_buf[1][output_row];
3819+ outptr2 = output_buf[2][output_row];
3820+ outptr3 = output_buf[3][output_row];
3821+ output_row++;
3822+ for (col = 0; col < num_cols; col++) {
3823+ r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
3824+ g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
3825+ b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
3826+ /* K passes through as-is */
3827+ outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
3828+ inptr += 4;
3829+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
3830+ * must be too; we do not need an explicit range-limiting operation.
3831+ * Hence the value being shifted is never negative, and we don't
3832+ * need the general RIGHT_SHIFT macro.
3833+ */
3834+ /* Y */
3835+ outptr0[col] = (JSAMPLE)
3836+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
3837+ >> SCALEBITS);
3838+ /* Cb */
3839+ outptr1[col] = (JSAMPLE)
3840+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
3841+ >> SCALEBITS);
3842+ /* Cr */
3843+ outptr2[col] = (JSAMPLE)
3844+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
3845+ >> SCALEBITS);
3846+ }
3847+ }
3848+}
3849+
3850+
3851+/*
3852+ * Convert some rows of samples to the JPEG colorspace.
3853+ * This version handles grayscale output with no conversion.
3854+ * The source can be either plain grayscale or YCbCr (since Y == gray).
3855+ */
3856+
3857+METHODDEF(void)
3858+grayscale_convert (j_compress_ptr cinfo,
3859+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3860+ JDIMENSION output_row, int num_rows)
3861+{
3862+ register JSAMPROW inptr;
3863+ register JSAMPROW outptr;
3864+ register JDIMENSION col;
3865+ JDIMENSION num_cols = cinfo->image_width;
3866+ int instride = cinfo->input_components;
3867+
3868+ while (--num_rows >= 0) {
3869+ inptr = *input_buf++;
3870+ outptr = output_buf[0][output_row];
3871+ output_row++;
3872+ for (col = 0; col < num_cols; col++) {
3873+ outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
3874+ inptr += instride;
3875+ }
3876+ }
3877+}
3878+
3879+
3880+/*
3881+ * Convert some rows of samples to the JPEG colorspace.
3882+ * This version handles multi-component colorspaces without conversion.
3883+ * We assume input_components == num_components.
3884+ */
3885+
3886+METHODDEF(void)
3887+null_convert (j_compress_ptr cinfo,
3888+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
3889+ JDIMENSION output_row, int num_rows)
3890+{
3891+ register JSAMPROW inptr;
3892+ register JSAMPROW outptr;
3893+ register JDIMENSION col;
3894+ register int ci;
3895+ int nc = cinfo->num_components;
3896+ JDIMENSION num_cols = cinfo->image_width;
3897+
3898+ while (--num_rows >= 0) {
3899+ /* It seems fastest to make a separate pass for each component. */
3900+ for (ci = 0; ci < nc; ci++) {
3901+ inptr = *input_buf;
3902+ outptr = output_buf[ci][output_row];
3903+ for (col = 0; col < num_cols; col++) {
3904+ outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
3905+ inptr += nc;
3906+ }
3907+ }
3908+ input_buf++;
3909+ output_row++;
3910+ }
3911+}
3912+
3913+
3914+/*
3915+ * Empty method for start_pass.
3916+ */
3917+
3918+METHODDEF(void)
3919+null_method (j_compress_ptr cinfo)
3920+{
3921+ /* no work needed */
3922+}
3923+
3924+
3925+/*
3926+ * Module initialization routine for input colorspace conversion.
3927+ */
3928+
3929+GLOBAL(void)
3930+jinit_color_converter (j_compress_ptr cinfo)
3931+{
3932+ my_cconvert_ptr cconvert;
3933+
3934+ cconvert = (my_cconvert_ptr)
3935+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3936+ SIZEOF(my_color_converter));
3937+ cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
3938+ /* set start_pass to null method until we find out differently */
3939+ cconvert->pub.start_pass = null_method;
3940+
3941+ /* Make sure input_components agrees with in_color_space */
3942+ switch (cinfo->in_color_space) {
3943+ case JCS_GRAYSCALE:
3944+ if (cinfo->input_components != 1)
3945+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
3946+ break;
3947+
3948+ case JCS_RGB:
3949+ case JCS_EXT_RGB:
3950+ case JCS_EXT_RGBX:
3951+ case JCS_EXT_BGR:
3952+ case JCS_EXT_BGRX:
3953+ case JCS_EXT_XBGR:
3954+ case JCS_EXT_XRGB:
3955+ case JCS_EXT_RGBA:
3956+ case JCS_EXT_BGRA:
3957+ case JCS_EXT_ABGR:
3958+ case JCS_EXT_ARGB:
3959+ if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
3960+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
3961+ break;
3962+
3963+ case JCS_YCbCr:
3964+ if (cinfo->input_components != 3)
3965+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
3966+ break;
3967+
3968+ case JCS_CMYK:
3969+ case JCS_YCCK:
3970+ if (cinfo->input_components != 4)
3971+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
3972+ break;
3973+
3974+ default: /* JCS_UNKNOWN can be anything */
3975+ if (cinfo->input_components < 1)
3976+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
3977+ break;
3978+ }
3979+
3980+ /* Check num_components, set conversion method based on requested space */
3981+ switch (cinfo->jpeg_color_space) {
3982+ case JCS_GRAYSCALE:
3983+ if (cinfo->num_components != 1)
3984+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
3985+ if (cinfo->in_color_space == JCS_GRAYSCALE)
3986+ cconvert->pub.color_convert = grayscale_convert;
3987+ else if (cinfo->in_color_space == JCS_RGB ||
3988+ cinfo->in_color_space == JCS_EXT_RGB ||
3989+ cinfo->in_color_space == JCS_EXT_RGBX ||
3990+ cinfo->in_color_space == JCS_EXT_BGR ||
3991+ cinfo->in_color_space == JCS_EXT_BGRX ||
3992+ cinfo->in_color_space == JCS_EXT_XBGR ||
3993+ cinfo->in_color_space == JCS_EXT_XRGB ||
3994+ cinfo->in_color_space == JCS_EXT_RGBA ||
3995+ cinfo->in_color_space == JCS_EXT_BGRA ||
3996+ cinfo->in_color_space == JCS_EXT_ABGR ||
3997+ cinfo->in_color_space == JCS_EXT_ARGB) {
3998+ if (jsimd_can_rgb_gray())
3999+ cconvert->pub.color_convert = jsimd_rgb_gray_convert;
4000+ else {
4001+ cconvert->pub.start_pass = rgb_ycc_start;
4002+ cconvert->pub.color_convert = rgb_gray_convert;
4003+ }
4004+ } else if (cinfo->in_color_space == JCS_YCbCr)
4005+ cconvert->pub.color_convert = grayscale_convert;
4006+ else
4007+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4008+ break;
4009+
4010+ case JCS_RGB:
4011+ case JCS_EXT_RGB:
4012+ case JCS_EXT_RGBX:
4013+ case JCS_EXT_BGR:
4014+ case JCS_EXT_BGRX:
4015+ case JCS_EXT_XBGR:
4016+ case JCS_EXT_XRGB:
4017+ case JCS_EXT_RGBA:
4018+ case JCS_EXT_BGRA:
4019+ case JCS_EXT_ABGR:
4020+ case JCS_EXT_ARGB:
4021+ if (cinfo->num_components != 3)
4022+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
4023+ if (cinfo->in_color_space == cinfo->jpeg_color_space &&
4024+ rgb_pixelsize[cinfo->in_color_space] == 3)
4025+ cconvert->pub.color_convert = null_convert;
4026+ else
4027+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4028+ break;
4029+
4030+ case JCS_YCbCr:
4031+ if (cinfo->num_components != 3)
4032+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
4033+ if (cinfo->in_color_space == JCS_RGB ||
4034+ cinfo->in_color_space == JCS_EXT_RGB ||
4035+ cinfo->in_color_space == JCS_EXT_RGBX ||
4036+ cinfo->in_color_space == JCS_EXT_BGR ||
4037+ cinfo->in_color_space == JCS_EXT_BGRX ||
4038+ cinfo->in_color_space == JCS_EXT_XBGR ||
4039+ cinfo->in_color_space == JCS_EXT_XRGB ||
4040+ cinfo->in_color_space == JCS_EXT_RGBA ||
4041+ cinfo->in_color_space == JCS_EXT_BGRA ||
4042+ cinfo->in_color_space == JCS_EXT_ABGR ||
4043+ cinfo->in_color_space == JCS_EXT_ARGB) {
4044+ if (jsimd_can_rgb_ycc())
4045+ cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
4046+ else {
4047+ cconvert->pub.start_pass = rgb_ycc_start;
4048+ cconvert->pub.color_convert = rgb_ycc_convert;
4049+ }
4050+ } else if (cinfo->in_color_space == JCS_YCbCr)
4051+ cconvert->pub.color_convert = null_convert;
4052+ else
4053+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4054+ break;
4055+
4056+ case JCS_CMYK:
4057+ if (cinfo->num_components != 4)
4058+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
4059+ if (cinfo->in_color_space == JCS_CMYK)
4060+ cconvert->pub.color_convert = null_convert;
4061+ else
4062+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4063+ break;
4064+
4065+ case JCS_YCCK:
4066+ if (cinfo->num_components != 4)
4067+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
4068+ if (cinfo->in_color_space == JCS_CMYK) {
4069+ cconvert->pub.start_pass = rgb_ycc_start;
4070+ cconvert->pub.color_convert = cmyk_ycck_convert;
4071+ } else if (cinfo->in_color_space == JCS_YCCK)
4072+ cconvert->pub.color_convert = null_convert;
4073+ else
4074+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4075+ break;
4076+
4077+ default: /* allow null conversion of JCS_UNKNOWN */
4078+ if (cinfo->jpeg_color_space != cinfo->in_color_space ||
4079+ cinfo->num_components != cinfo->input_components)
4080+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
4081+ cconvert->pub.color_convert = null_convert;
4082+ break;
4083+ }
4084+}
4085
4086=== added file 'src/libjpeg-turbo/jcdctmgr.c'
4087--- src/libjpeg-turbo/jcdctmgr.c 1970-01-01 00:00:00 +0000
4088+++ src/libjpeg-turbo/jcdctmgr.c 2012-06-27 16:20:24 +0000
4089@@ -0,0 +1,642 @@
4090+/*
4091+ * jcdctmgr.c
4092+ *
4093+ * Copyright (C) 1994-1996, Thomas G. Lane.
4094+ * Copyright (C) 1999-2006, MIYASAKA Masaru.
4095+ * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
4096+ * Copyright (C) 2011 D. R. Commander
4097+ * This file is part of the Independent JPEG Group's software.
4098+ * For conditions of distribution and use, see the accompanying README file.
4099+ *
4100+ * This file contains the forward-DCT management logic.
4101+ * This code selects a particular DCT implementation to be used,
4102+ * and it performs related housekeeping chores including coefficient
4103+ * quantization.
4104+ */
4105+
4106+#define JPEG_INTERNALS
4107+#include "jinclude.h"
4108+#include "jpeglib.h"
4109+#include "jdct.h" /* Private declarations for DCT subsystem */
4110+#include "jsimddct.h"
4111+
4112+
4113+/* Private subobject for this module */
4114+
4115+typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
4116+typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
4117+
4118+typedef JMETHOD(void, convsamp_method_ptr,
4119+ (JSAMPARRAY sample_data, JDIMENSION start_col,
4120+ DCTELEM * workspace));
4121+typedef JMETHOD(void, float_convsamp_method_ptr,
4122+ (JSAMPARRAY sample_data, JDIMENSION start_col,
4123+ FAST_FLOAT *workspace));
4124+
4125+typedef JMETHOD(void, quantize_method_ptr,
4126+ (JCOEFPTR coef_block, DCTELEM * divisors,
4127+ DCTELEM * workspace));
4128+typedef JMETHOD(void, float_quantize_method_ptr,
4129+ (JCOEFPTR coef_block, FAST_FLOAT * divisors,
4130+ FAST_FLOAT * workspace));
4131+
4132+METHODDEF(void) quantize (JCOEFPTR, DCTELEM *, DCTELEM *);
4133+
4134+typedef struct {
4135+ struct jpeg_forward_dct pub; /* public fields */
4136+
4137+ /* Pointer to the DCT routine actually in use */
4138+ forward_DCT_method_ptr dct;
4139+ convsamp_method_ptr convsamp;
4140+ quantize_method_ptr quantize;
4141+
4142+ /* The actual post-DCT divisors --- not identical to the quant table
4143+ * entries, because of scaling (especially for an unnormalized DCT).
4144+ * Each table is given in normal array order.
4145+ */
4146+ DCTELEM * divisors[NUM_QUANT_TBLS];
4147+
4148+ /* work area for FDCT subroutine */
4149+ DCTELEM * workspace;
4150+
4151+#ifdef DCT_FLOAT_SUPPORTED
4152+ /* Same as above for the floating-point case. */
4153+ float_DCT_method_ptr float_dct;
4154+ float_convsamp_method_ptr float_convsamp;
4155+ float_quantize_method_ptr float_quantize;
4156+ FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
4157+ FAST_FLOAT * float_workspace;
4158+#endif
4159+} my_fdct_controller;
4160+
4161+typedef my_fdct_controller * my_fdct_ptr;
4162+
4163+
4164+/*
4165+ * Find the highest bit in an integer through binary search.
4166+ */
4167+LOCAL(int)
4168+flss (UINT16 val)
4169+{
4170+ int bit;
4171+
4172+ bit = 16;
4173+
4174+ if (!val)
4175+ return 0;
4176+
4177+ if (!(val & 0xff00)) {
4178+ bit -= 8;
4179+ val <<= 8;
4180+ }
4181+ if (!(val & 0xf000)) {
4182+ bit -= 4;
4183+ val <<= 4;
4184+ }
4185+ if (!(val & 0xc000)) {
4186+ bit -= 2;
4187+ val <<= 2;
4188+ }
4189+ if (!(val & 0x8000)) {
4190+ bit -= 1;
4191+ val <<= 1;
4192+ }
4193+
4194+ return bit;
4195+}
4196+
4197+/*
4198+ * Compute values to do a division using reciprocal.
4199+ *
4200+ * This implementation is based on an algorithm described in
4201+ * "How to optimize for the Pentium family of microprocessors"
4202+ * (http://www.agner.org/assem/).
4203+ * More information about the basic algorithm can be found in
4204+ * the paper "Integer Division Using Reciprocals" by Robert Alverson.
4205+ *
4206+ * The basic idea is to replace x/d by x * d^-1. In order to store
4207+ * d^-1 with enough precision we shift it left a few places. It turns
4208+ * out that this algoright gives just enough precision, and also fits
4209+ * into DCTELEM:
4210+ *
4211+ * b = (the number of significant bits in divisor) - 1
4212+ * r = (word size) + b
4213+ * f = 2^r / divisor
4214+ *
4215+ * f will not be an integer for most cases, so we need to compensate
4216+ * for the rounding error introduced:
4217+ *
4218+ * no fractional part:
4219+ *
4220+ * result = input >> r
4221+ *
4222+ * fractional part of f < 0.5:
4223+ *
4224+ * round f down to nearest integer
4225+ * result = ((input + 1) * f) >> r
4226+ *
4227+ * fractional part of f > 0.5:
4228+ *
4229+ * round f up to nearest integer
4230+ * result = (input * f) >> r
4231+ *
4232+ * This is the original algorithm that gives truncated results. But we
4233+ * want properly rounded results, so we replace "input" with
4234+ * "input + divisor/2".
4235+ *
4236+ * In order to allow SIMD implementations we also tweak the values to
4237+ * allow the same calculation to be made at all times:
4238+ *
4239+ * dctbl[0] = f rounded to nearest integer
4240+ * dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5)
4241+ * dctbl[2] = 1 << ((word size) * 2 - r)
4242+ * dctbl[3] = r - (word size)
4243+ *
4244+ * dctbl[2] is for stupid instruction sets where the shift operation
4245+ * isn't member wise (e.g. MMX).
4246+ *
4247+ * The reason dctbl[2] and dctbl[3] reduce the shift with (word size)
4248+ * is that most SIMD implementations have a "multiply and store top
4249+ * half" operation.
4250+ *
4251+ * Lastly, we store each of the values in their own table instead
4252+ * of in a consecutive manner, yet again in order to allow SIMD
4253+ * routines.
4254+ */
4255+LOCAL(int)
4256+compute_reciprocal (UINT16 divisor, DCTELEM * dtbl)
4257+{
4258+ UDCTELEM2 fq, fr;
4259+ UDCTELEM c;
4260+ int b, r;
4261+
4262+ b = flss(divisor) - 1;
4263+ r = sizeof(DCTELEM) * 8 + b;
4264+
4265+ fq = ((UDCTELEM2)1 << r) / divisor;
4266+ fr = ((UDCTELEM2)1 << r) % divisor;
4267+
4268+ c = divisor / 2; /* for rounding */
4269+
4270+ if (fr == 0) { /* divisor is power of two */
4271+ /* fq will be one bit too large to fit in DCTELEM, so adjust */
4272+ fq >>= 1;
4273+ r--;
4274+ } else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */
4275+ c++;
4276+ } else { /* fractional part is > 0.5 */
4277+ fq++;
4278+ }
4279+
4280+ dtbl[DCTSIZE2 * 0] = (DCTELEM) fq; /* reciprocal */
4281+ dtbl[DCTSIZE2 * 1] = (DCTELEM) c; /* correction + roundfactor */
4282+ dtbl[DCTSIZE2 * 2] = (DCTELEM) (1 << (sizeof(DCTELEM)*8*2 - r)); /* scale */
4283+ dtbl[DCTSIZE2 * 3] = (DCTELEM) r - sizeof(DCTELEM)*8; /* shift */
4284+
4285+ if(r <= 16) return 0;
4286+ else return 1;
4287+}
4288+
4289+/*
4290+ * Initialize for a processing pass.
4291+ * Verify that all referenced Q-tables are present, and set up
4292+ * the divisor table for each one.
4293+ * In the current implementation, DCT of all components is done during
4294+ * the first pass, even if only some components will be output in the
4295+ * first scan. Hence all components should be examined here.
4296+ */
4297+
4298+METHODDEF(void)
4299+start_pass_fdctmgr (j_compress_ptr cinfo)
4300+{
4301+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
4302+ int ci, qtblno, i;
4303+ jpeg_component_info *compptr;
4304+ JQUANT_TBL * qtbl;
4305+ DCTELEM * dtbl;
4306+
4307+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
4308+ ci++, compptr++) {
4309+ qtblno = compptr->quant_tbl_no;
4310+ /* Make sure specified quantization table is present */
4311+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
4312+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
4313+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
4314+ qtbl = cinfo->quant_tbl_ptrs[qtblno];
4315+ /* Compute divisors for this quant table */
4316+ /* We may do this more than once for same table, but it's not a big deal */
4317+ switch (cinfo->dct_method) {
4318+#ifdef DCT_ISLOW_SUPPORTED
4319+ case JDCT_ISLOW:
4320+ /* For LL&M IDCT method, divisors are equal to raw quantization
4321+ * coefficients multiplied by 8 (to counteract scaling).
4322+ */
4323+ if (fdct->divisors[qtblno] == NULL) {
4324+ fdct->divisors[qtblno] = (DCTELEM *)
4325+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4326+ (DCTSIZE2 * 4) * SIZEOF(DCTELEM));
4327+ }
4328+ dtbl = fdct->divisors[qtblno];
4329+ for (i = 0; i < DCTSIZE2; i++) {
4330+ if(!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i])
4331+ && fdct->quantize == jsimd_quantize)
4332+ fdct->quantize = quantize;
4333+ }
4334+ break;
4335+#endif
4336+#ifdef DCT_IFAST_SUPPORTED
4337+ case JDCT_IFAST:
4338+ {
4339+ /* For AA&N IDCT method, divisors are equal to quantization
4340+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
4341+ * scalefactor[0] = 1
4342+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
4343+ * We apply a further scale factor of 8.
4344+ */
4345+#define CONST_BITS 14
4346+ static const INT16 aanscales[DCTSIZE2] = {
4347+ /* precomputed values scaled up by 14 bits */
4348+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
4349+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
4350+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
4351+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
4352+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
4353+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
4354+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
4355+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
4356+ };
4357+ SHIFT_TEMPS
4358+
4359+ if (fdct->divisors[qtblno] == NULL) {
4360+ fdct->divisors[qtblno] = (DCTELEM *)
4361+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4362+ (DCTSIZE2 * 4) * SIZEOF(DCTELEM));
4363+ }
4364+ dtbl = fdct->divisors[qtblno];
4365+ for (i = 0; i < DCTSIZE2; i++) {
4366+ if(!compute_reciprocal(
4367+ DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
4368+ (INT32) aanscales[i]),
4369+ CONST_BITS-3), &dtbl[i])
4370+ && fdct->quantize == jsimd_quantize)
4371+ fdct->quantize = quantize;
4372+ }
4373+ }
4374+ break;
4375+#endif
4376+#ifdef DCT_FLOAT_SUPPORTED
4377+ case JDCT_FLOAT:
4378+ {
4379+ /* For float AA&N IDCT method, divisors are equal to quantization
4380+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
4381+ * scalefactor[0] = 1
4382+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
4383+ * We apply a further scale factor of 8.
4384+ * What's actually stored is 1/divisor so that the inner loop can
4385+ * use a multiplication rather than a division.
4386+ */
4387+ FAST_FLOAT * fdtbl;
4388+ int row, col;
4389+ static const double aanscalefactor[DCTSIZE] = {
4390+ 1.0, 1.387039845, 1.306562965, 1.175875602,
4391+ 1.0, 0.785694958, 0.541196100, 0.275899379
4392+ };
4393+
4394+ if (fdct->float_divisors[qtblno] == NULL) {
4395+ fdct->float_divisors[qtblno] = (FAST_FLOAT *)
4396+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4397+ DCTSIZE2 * SIZEOF(FAST_FLOAT));
4398+ }
4399+ fdtbl = fdct->float_divisors[qtblno];
4400+ i = 0;
4401+ for (row = 0; row < DCTSIZE; row++) {
4402+ for (col = 0; col < DCTSIZE; col++) {
4403+ fdtbl[i] = (FAST_FLOAT)
4404+ (1.0 / (((double) qtbl->quantval[i] *
4405+ aanscalefactor[row] * aanscalefactor[col] * 8.0)));
4406+ i++;
4407+ }
4408+ }
4409+ }
4410+ break;
4411+#endif
4412+ default:
4413+ ERREXIT(cinfo, JERR_NOT_COMPILED);
4414+ break;
4415+ }
4416+ }
4417+}
4418+
4419+
4420+/*
4421+ * Load data into workspace, applying unsigned->signed conversion.
4422+ */
4423+
4424+METHODDEF(void)
4425+convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)
4426+{
4427+ register DCTELEM *workspaceptr;
4428+ register JSAMPROW elemptr;
4429+ register int elemr;
4430+
4431+ workspaceptr = workspace;
4432+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
4433+ elemptr = sample_data[elemr] + start_col;
4434+
4435+#if DCTSIZE == 8 /* unroll the inner loop */
4436+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4437+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4438+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4439+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4440+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4441+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4442+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4443+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4444+#else
4445+ {
4446+ register int elemc;
4447+ for (elemc = DCTSIZE; elemc > 0; elemc--)
4448+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
4449+ }
4450+#endif
4451+ }
4452+}
4453+
4454+
4455+/*
4456+ * Quantize/descale the coefficients, and store into coef_blocks[].
4457+ */
4458+
4459+METHODDEF(void)
4460+quantize (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)
4461+{
4462+ int i;
4463+ DCTELEM temp;
4464+ UDCTELEM recip, corr, shift;
4465+ UDCTELEM2 product;
4466+ JCOEFPTR output_ptr = coef_block;
4467+
4468+ for (i = 0; i < DCTSIZE2; i++) {
4469+ temp = workspace[i];
4470+ recip = divisors[i + DCTSIZE2 * 0];
4471+ corr = divisors[i + DCTSIZE2 * 1];
4472+ shift = divisors[i + DCTSIZE2 * 3];
4473+
4474+ if (temp < 0) {
4475+ temp = -temp;
4476+ product = (UDCTELEM2)(temp + corr) * recip;
4477+ product >>= shift + sizeof(DCTELEM)*8;
4478+ temp = product;
4479+ temp = -temp;
4480+ } else {
4481+ product = (UDCTELEM2)(temp + corr) * recip;
4482+ product >>= shift + sizeof(DCTELEM)*8;
4483+ temp = product;
4484+ }
4485+
4486+ output_ptr[i] = (JCOEF) temp;
4487+ }
4488+}
4489+
4490+
4491+/*
4492+ * Perform forward DCT on one or more blocks of a component.
4493+ *
4494+ * The input samples are taken from the sample_data[] array starting at
4495+ * position start_row/start_col, and moving to the right for any additional
4496+ * blocks. The quantized coefficients are returned in coef_blocks[].
4497+ */
4498+
4499+METHODDEF(void)
4500+forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
4501+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
4502+ JDIMENSION start_row, JDIMENSION start_col,
4503+ JDIMENSION num_blocks)
4504+/* This version is used for integer DCT implementations. */
4505+{
4506+ /* This routine is heavily used, so it's worth coding it tightly. */
4507+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
4508+ DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
4509+ DCTELEM * workspace;
4510+ JDIMENSION bi;
4511+
4512+ /* Make sure the compiler doesn't look up these every pass */
4513+ forward_DCT_method_ptr do_dct = fdct->dct;
4514+ convsamp_method_ptr do_convsamp = fdct->convsamp;
4515+ quantize_method_ptr do_quantize = fdct->quantize;
4516+ workspace = fdct->workspace;
4517+
4518+ sample_data += start_row; /* fold in the vertical offset once */
4519+
4520+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
4521+ /* Load data into workspace, applying unsigned->signed conversion */
4522+ (*do_convsamp) (sample_data, start_col, workspace);
4523+
4524+ /* Perform the DCT */
4525+ (*do_dct) (workspace);
4526+
4527+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
4528+ (*do_quantize) (coef_blocks[bi], divisors, workspace);
4529+ }
4530+}
4531+
4532+
4533+#ifdef DCT_FLOAT_SUPPORTED
4534+
4535+
4536+METHODDEF(void)
4537+convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)
4538+{
4539+ register FAST_FLOAT *workspaceptr;
4540+ register JSAMPROW elemptr;
4541+ register int elemr;
4542+
4543+ workspaceptr = workspace;
4544+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
4545+ elemptr = sample_data[elemr] + start_col;
4546+#if DCTSIZE == 8 /* unroll the inner loop */
4547+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4548+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4549+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4550+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4551+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4552+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4553+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4554+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4555+#else
4556+ {
4557+ register int elemc;
4558+ for (elemc = DCTSIZE; elemc > 0; elemc--)
4559+ *workspaceptr++ = (FAST_FLOAT)
4560+ (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
4561+ }
4562+#endif
4563+ }
4564+}
4565+
4566+
4567+METHODDEF(void)
4568+quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)
4569+{
4570+ register FAST_FLOAT temp;
4571+ register int i;
4572+ register JCOEFPTR output_ptr = coef_block;
4573+
4574+ for (i = 0; i < DCTSIZE2; i++) {
4575+ /* Apply the quantization and scaling factor */
4576+ temp = workspace[i] * divisors[i];
4577+
4578+ /* Round to nearest integer.
4579+ * Since C does not specify the direction of rounding for negative
4580+ * quotients, we have to force the dividend positive for portability.
4581+ * The maximum coefficient size is +-16K (for 12-bit data), so this
4582+ * code should work for either 16-bit or 32-bit ints.
4583+ */
4584+ output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
4585+ }
4586+}
4587+
4588+
4589+METHODDEF(void)
4590+forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
4591+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
4592+ JDIMENSION start_row, JDIMENSION start_col,
4593+ JDIMENSION num_blocks)
4594+/* This version is used for floating-point DCT implementations. */
4595+{
4596+ /* This routine is heavily used, so it's worth coding it tightly. */
4597+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
4598+ FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
4599+ FAST_FLOAT * workspace;
4600+ JDIMENSION bi;
4601+
4602+
4603+ /* Make sure the compiler doesn't look up these every pass */
4604+ float_DCT_method_ptr do_dct = fdct->float_dct;
4605+ float_convsamp_method_ptr do_convsamp = fdct->float_convsamp;
4606+ float_quantize_method_ptr do_quantize = fdct->float_quantize;
4607+ workspace = fdct->float_workspace;
4608+
4609+ sample_data += start_row; /* fold in the vertical offset once */
4610+
4611+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
4612+ /* Load data into workspace, applying unsigned->signed conversion */
4613+ (*do_convsamp) (sample_data, start_col, workspace);
4614+
4615+ /* Perform the DCT */
4616+ (*do_dct) (workspace);
4617+
4618+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
4619+ (*do_quantize) (coef_blocks[bi], divisors, workspace);
4620+ }
4621+}
4622+
4623+#endif /* DCT_FLOAT_SUPPORTED */
4624+
4625+
4626+/*
4627+ * Initialize FDCT manager.
4628+ */
4629+
4630+GLOBAL(void)
4631+jinit_forward_dct (j_compress_ptr cinfo)
4632+{
4633+ my_fdct_ptr fdct;
4634+ int i;
4635+
4636+ fdct = (my_fdct_ptr)
4637+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4638+ SIZEOF(my_fdct_controller));
4639+ cinfo->fdct = (struct jpeg_forward_dct *) fdct;
4640+ fdct->pub.start_pass = start_pass_fdctmgr;
4641+
4642+ /* First determine the DCT... */
4643+ switch (cinfo->dct_method) {
4644+#ifdef DCT_ISLOW_SUPPORTED
4645+ case JDCT_ISLOW:
4646+ fdct->pub.forward_DCT = forward_DCT;
4647+ if (jsimd_can_fdct_islow())
4648+ fdct->dct = jsimd_fdct_islow;
4649+ else
4650+ fdct->dct = jpeg_fdct_islow;
4651+ break;
4652+#endif
4653+#ifdef DCT_IFAST_SUPPORTED
4654+ case JDCT_IFAST:
4655+ fdct->pub.forward_DCT = forward_DCT;
4656+ if (jsimd_can_fdct_ifast())
4657+ fdct->dct = jsimd_fdct_ifast;
4658+ else
4659+ fdct->dct = jpeg_fdct_ifast;
4660+ break;
4661+#endif
4662+#ifdef DCT_FLOAT_SUPPORTED
4663+ case JDCT_FLOAT:
4664+ fdct->pub.forward_DCT = forward_DCT_float;
4665+ if (jsimd_can_fdct_float())
4666+ fdct->float_dct = jsimd_fdct_float;
4667+ else
4668+ fdct->float_dct = jpeg_fdct_float;
4669+ break;
4670+#endif
4671+ default:
4672+ ERREXIT(cinfo, JERR_NOT_COMPILED);
4673+ break;
4674+ }
4675+
4676+ /* ...then the supporting stages. */
4677+ switch (cinfo->dct_method) {
4678+#ifdef DCT_ISLOW_SUPPORTED
4679+ case JDCT_ISLOW:
4680+#endif
4681+#ifdef DCT_IFAST_SUPPORTED
4682+ case JDCT_IFAST:
4683+#endif
4684+#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED)
4685+ if (jsimd_can_convsamp())
4686+ fdct->convsamp = jsimd_convsamp;
4687+ else
4688+ fdct->convsamp = convsamp;
4689+ if (jsimd_can_quantize())
4690+ fdct->quantize = jsimd_quantize;
4691+ else
4692+ fdct->quantize = quantize;
4693+ break;
4694+#endif
4695+#ifdef DCT_FLOAT_SUPPORTED
4696+ case JDCT_FLOAT:
4697+ if (jsimd_can_convsamp_float())
4698+ fdct->float_convsamp = jsimd_convsamp_float;
4699+ else
4700+ fdct->float_convsamp = convsamp_float;
4701+ if (jsimd_can_quantize_float())
4702+ fdct->float_quantize = jsimd_quantize_float;
4703+ else
4704+ fdct->float_quantize = quantize_float;
4705+ break;
4706+#endif
4707+ default:
4708+ ERREXIT(cinfo, JERR_NOT_COMPILED);
4709+ break;
4710+ }
4711+
4712+ /* Allocate workspace memory */
4713+#ifdef DCT_FLOAT_SUPPORTED
4714+ if (cinfo->dct_method == JDCT_FLOAT)
4715+ fdct->float_workspace = (FAST_FLOAT *)
4716+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4717+ SIZEOF(FAST_FLOAT) * DCTSIZE2);
4718+ else
4719+#endif
4720+ fdct->workspace = (DCTELEM *)
4721+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4722+ SIZEOF(DCTELEM) * DCTSIZE2);
4723+
4724+ /* Mark divisor tables unallocated */
4725+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
4726+ fdct->divisors[i] = NULL;
4727+#ifdef DCT_FLOAT_SUPPORTED
4728+ fdct->float_divisors[i] = NULL;
4729+#endif
4730+ }
4731+}
4732
4733=== added file 'src/libjpeg-turbo/jchuff.c'
4734--- src/libjpeg-turbo/jchuff.c 1970-01-01 00:00:00 +0000
4735+++ src/libjpeg-turbo/jchuff.c 2012-06-27 16:20:24 +0000
4736@@ -0,0 +1,1026 @@
4737+/*
4738+ * jchuff.c
4739+ *
4740+ * Copyright (C) 1991-1997, Thomas G. Lane.
4741+ * Copyright (C) 2009-2011, D. R. Commander.
4742+ * This file is part of the Independent JPEG Group's software.
4743+ * For conditions of distribution and use, see the accompanying README file.
4744+ *
4745+ * This file contains Huffman entropy encoding routines.
4746+ *
4747+ * Much of the complexity here has to do with supporting output suspension.
4748+ * If the data destination module demands suspension, we want to be able to
4749+ * back up to the start of the current MCU. To do this, we copy state
4750+ * variables into local working storage, and update them back to the
4751+ * permanent JPEG objects only upon successful completion of an MCU.
4752+ */
4753+
4754+#define JPEG_INTERNALS
4755+#include "jinclude.h"
4756+#include "jpeglib.h"
4757+#include "jchuff.h" /* Declarations shared with jcphuff.c */
4758+#include <limits.h>
4759+
4760+static unsigned char jpeg_nbits_table[65536];
4761+static int jpeg_nbits_table_init = 0;
4762+
4763+#ifndef min
4764+ #define min(a,b) ((a)<(b)?(a):(b))
4765+#endif
4766+
4767+
4768+/* Expanded entropy encoder object for Huffman encoding.
4769+ *
4770+ * The savable_state subrecord contains fields that change within an MCU,
4771+ * but must not be updated permanently until we complete the MCU.
4772+ */
4773+
4774+typedef struct {
4775+ size_t put_buffer; /* current bit-accumulation buffer */
4776+ int put_bits; /* # of bits now in it */
4777+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
4778+} savable_state;
4779+
4780+/* This macro is to work around compilers with missing or broken
4781+ * structure assignment. You'll need to fix this code if you have
4782+ * such a compiler and you change MAX_COMPS_IN_SCAN.
4783+ */
4784+
4785+#ifndef NO_STRUCT_ASSIGN
4786+#define ASSIGN_STATE(dest,src) ((dest) = (src))
4787+#else
4788+#if MAX_COMPS_IN_SCAN == 4
4789+#define ASSIGN_STATE(dest,src) \
4790+ ((dest).put_buffer = (src).put_buffer, \
4791+ (dest).put_bits = (src).put_bits, \
4792+ (dest).last_dc_val[0] = (src).last_dc_val[0], \
4793+ (dest).last_dc_val[1] = (src).last_dc_val[1], \
4794+ (dest).last_dc_val[2] = (src).last_dc_val[2], \
4795+ (dest).last_dc_val[3] = (src).last_dc_val[3])
4796+#endif
4797+#endif
4798+
4799+
4800+typedef struct {
4801+ struct jpeg_entropy_encoder pub; /* public fields */
4802+
4803+ savable_state saved; /* Bit buffer & DC state at start of MCU */
4804+
4805+ /* These fields are NOT loaded into local working state. */
4806+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
4807+ int next_restart_num; /* next restart number to write (0-7) */
4808+
4809+ /* Pointers to derived tables (these workspaces have image lifespan) */
4810+ c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
4811+ c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
4812+
4813+#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
4814+ long * dc_count_ptrs[NUM_HUFF_TBLS];
4815+ long * ac_count_ptrs[NUM_HUFF_TBLS];
4816+#endif
4817+} huff_entropy_encoder;
4818+
4819+typedef huff_entropy_encoder * huff_entropy_ptr;
4820+
4821+/* Working state while writing an MCU.
4822+ * This struct contains all the fields that are needed by subroutines.
4823+ */
4824+
4825+typedef struct {
4826+ JOCTET * next_output_byte; /* => next byte to write in buffer */
4827+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
4828+ savable_state cur; /* Current bit buffer & DC state */
4829+ j_compress_ptr cinfo; /* dump_buffer needs access to this */
4830+} working_state;
4831+
4832+
4833+/* Forward declarations */
4834+METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
4835+ JBLOCKROW *MCU_data));
4836+METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
4837+#ifdef ENTROPY_OPT_SUPPORTED
4838+METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
4839+ JBLOCKROW *MCU_data));
4840+METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
4841+#endif
4842+
4843+
4844+/*
4845+ * Initialize for a Huffman-compressed scan.
4846+ * If gather_statistics is TRUE, we do not output anything during the scan,
4847+ * just count the Huffman symbols used and generate Huffman code tables.
4848+ */
4849+
4850+METHODDEF(void)
4851+start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
4852+{
4853+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
4854+ int ci, dctbl, actbl;
4855+ jpeg_component_info * compptr;
4856+
4857+ if (gather_statistics) {
4858+#ifdef ENTROPY_OPT_SUPPORTED
4859+ entropy->pub.encode_mcu = encode_mcu_gather;
4860+ entropy->pub.finish_pass = finish_pass_gather;
4861+#else
4862+ ERREXIT(cinfo, JERR_NOT_COMPILED);
4863+#endif
4864+ } else {
4865+ entropy->pub.encode_mcu = encode_mcu_huff;
4866+ entropy->pub.finish_pass = finish_pass_huff;
4867+ }
4868+
4869+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
4870+ compptr = cinfo->cur_comp_info[ci];
4871+ dctbl = compptr->dc_tbl_no;
4872+ actbl = compptr->ac_tbl_no;
4873+ if (gather_statistics) {
4874+#ifdef ENTROPY_OPT_SUPPORTED
4875+ /* Check for invalid table indexes */
4876+ /* (make_c_derived_tbl does this in the other path) */
4877+ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
4878+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
4879+ if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
4880+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
4881+ /* Allocate and zero the statistics tables */
4882+ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
4883+ if (entropy->dc_count_ptrs[dctbl] == NULL)
4884+ entropy->dc_count_ptrs[dctbl] = (long *)
4885+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4886+ 257 * SIZEOF(long));
4887+ MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
4888+ if (entropy->ac_count_ptrs[actbl] == NULL)
4889+ entropy->ac_count_ptrs[actbl] = (long *)
4890+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4891+ 257 * SIZEOF(long));
4892+ MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
4893+#endif
4894+ } else {
4895+ /* Compute derived values for Huffman tables */
4896+ /* We may do this more than once for a table, but it's not expensive */
4897+ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
4898+ & entropy->dc_derived_tbls[dctbl]);
4899+ jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
4900+ & entropy->ac_derived_tbls[actbl]);
4901+ }
4902+ /* Initialize DC predictions to 0 */
4903+ entropy->saved.last_dc_val[ci] = 0;
4904+ }
4905+
4906+ /* Initialize bit buffer to empty */
4907+ entropy->saved.put_buffer = 0;
4908+ entropy->saved.put_bits = 0;
4909+
4910+ /* Initialize restart stuff */
4911+ entropy->restarts_to_go = cinfo->restart_interval;
4912+ entropy->next_restart_num = 0;
4913+}
4914+
4915+
4916+/*
4917+ * Compute the derived values for a Huffman table.
4918+ * This routine also performs some validation checks on the table.
4919+ *
4920+ * Note this is also used by jcphuff.c.
4921+ */
4922+
4923+GLOBAL(void)
4924+jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
4925+ c_derived_tbl ** pdtbl)
4926+{
4927+ JHUFF_TBL *htbl;
4928+ c_derived_tbl *dtbl;
4929+ int p, i, l, lastp, si, maxsymbol;
4930+ char huffsize[257];
4931+ unsigned int huffcode[257];
4932+ unsigned int code;
4933+
4934+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
4935+ * paralleling the order of the symbols themselves in htbl->huffval[].
4936+ */
4937+
4938+ /* Find the input Huffman table */
4939+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
4940+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
4941+ htbl =
4942+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
4943+ if (htbl == NULL)
4944+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
4945+
4946+ /* Allocate a workspace if we haven't already done so. */
4947+ if (*pdtbl == NULL)
4948+ *pdtbl = (c_derived_tbl *)
4949+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
4950+ SIZEOF(c_derived_tbl));
4951+ dtbl = *pdtbl;
4952+
4953+ /* Figure C.1: make table of Huffman code length for each symbol */
4954+
4955+ p = 0;
4956+ for (l = 1; l <= 16; l++) {
4957+ i = (int) htbl->bits[l];
4958+ if (i < 0 || p + i > 256) /* protect against table overrun */
4959+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
4960+ while (i--)
4961+ huffsize[p++] = (char) l;
4962+ }
4963+ huffsize[p] = 0;
4964+ lastp = p;
4965+
4966+ /* Figure C.2: generate the codes themselves */
4967+ /* We also validate that the counts represent a legal Huffman code tree. */
4968+
4969+ code = 0;
4970+ si = huffsize[0];
4971+ p = 0;
4972+ while (huffsize[p]) {
4973+ while (((int) huffsize[p]) == si) {
4974+ huffcode[p++] = code;
4975+ code++;
4976+ }
4977+ /* code is now 1 more than the last code used for codelength si; but
4978+ * it must still fit in si bits, since no code is allowed to be all ones.
4979+ */
4980+ if (((INT32) code) >= (((INT32) 1) << si))
4981+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
4982+ code <<= 1;
4983+ si++;
4984+ }
4985+
4986+ /* Figure C.3: generate encoding tables */
4987+ /* These are code and size indexed by symbol value */
4988+
4989+ /* Set all codeless symbols to have code length 0;
4990+ * this lets us detect duplicate VAL entries here, and later
4991+ * allows emit_bits to detect any attempt to emit such symbols.
4992+ */
4993+ MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
4994+
4995+ /* This is also a convenient place to check for out-of-range
4996+ * and duplicated VAL entries. We allow 0..255 for AC symbols
4997+ * but only 0..15 for DC. (We could constrain them further
4998+ * based on data depth and mode, but this seems enough.)
4999+ */
5000+ maxsymbol = isDC ? 15 : 255;
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches