Files
v4l2_libs/libv4lconvert/tinyjpeg.c
svcmobrel-release 1f9ef4be87 Updating prebuilts and/or headers
f07563fa1628a75c578152387638b1f217f730be - libv4lconvert/Makefile
6ac3c887c56c7d4345499cb5b7bfac9579a57d16 - libv4lconvert/cpia1.c
2d385802ec3690e78a961ce95431e98eb3f5fc6a - libv4lconvert/rgbyuv.c
43843cbdb81f3efee5873f5ed25ae1eb7745a9e5 - libv4lconvert/libv4lconvert-priv.h
002783c808dc565478c18bd27373792028264bb6 - libv4lconvert/ov518-decomp.c
4d49e6e897046e66e41c3563fec2eb2562abf813 - libv4lconvert/jpeg_memsrcdest.c
a38fdb4c79518e972678745c93cd62881d72972b - libv4lconvert/Makefile.dGPU
e9f783fb933606224252dfd3093afa36490f6f4c - libv4lconvert/Makefile.in
52900601a24ee9ffea87ca7fe6651e912e20235e - libv4lconvert/libv4lsyscall-priv.h
d37f854500cbd759ad93ae89c6aa75c00b000931 - libv4lconvert/bayer.c
2cd1b994bb7999d0c08c19b1a2efd11707250852 - libv4lconvert/helper.c
adac14b8826d05ddab33d595a803e92eaf84b797 - libv4lconvert/Makefile.am
6e806290f6e426c881322d362f189fa7b9e2f917 - libv4lconvert/sn9c10x.c
ba44b6ac00a643e6df484f3172022e1b2628c0cc - libv4lconvert/jpgl.c
fa06343de31dcf6d3d5862cc278396272fa838c4 - libv4lconvert/se401.c
4bd081bf25b63f5463d582ef36f77ba5e3711d0d - libv4lconvert/sq905c.c
940250beb5898bf254e7cd1ef9d7950a8db10af6 - libv4lconvert/mr97310a.c
5e177c3a7c157c25c4afcae031e79830237510f6 - libv4lconvert/libv4lconvert.c
1f362080c319355070cf409cfccf28d8c68d024e - libv4lconvert/sn9c2028-decomp.c
703ce22dc98d834490c7aa23ae73b9113e386221 - libv4lconvert/sn9c20x.c
018e503969b2e06a0ea639fc08f03a7476cc15b5 - libv4lconvert/jpeg_memsrcdest.h
fb3344cfa8df97688332ee4fd3b17968437e8ad5 - libv4lconvert/helper-funcs.h
d378cbdd377027e7528af47362a94ccb1ae15da3 - libv4lconvert/libv4lconvert.export
61710972d4e9bfcd00490c563d1bb5bde5480c9e - libv4lconvert/tinyjpeg.c
231571db9f8caa6fdc68138102029127a6b93812 - libv4lconvert/crop.c
d62448a06539ecf6e3daae7faa299b6da8878054 - libv4lconvert/pac207.c
72b65c9f0f3c87dcae86a2afbea65a76f2c6bafd - libv4lconvert/tinyjpeg-internal.h
a016f896e0e9f0cd8af7447b52651ed14c5a5b29 - libv4lconvert/jpeg.c
608a4c341a5df9daea647fbddc415cdaf27b0482 - libv4lconvert/spca561-decompress.c
b03b32eae024bf59b6ceaf6b6d342383cd0b0673 - libv4lconvert/jl2005bcd.c
803c4d0b9364050eda163452b8792e62e221ab6d - libv4lconvert/tinyjpeg.h
2df34af8f9d747fb0e5c7c71954c788aff1c483f - libv4lconvert/stv0680.c
3e8e6c1fb85e3c4b58c4e9b2b0a223ddc793edcb - libv4lconvert/libv4lconvert.pc.in
02aa2e7d12b72fe4ce79cdf4009da61f35091e94 - libv4lconvert/flip.c
05038ef9efab175322a12d3d6620dba6298aa2a9 - libv4lconvert/spca501.c
b2c19c2eac71d39d3fb883cdc159a69c2afa8fd6 - libv4lconvert/ov511-decomp.c
1d9c446cd8a232da87bd79acebc93e018ec72499 - libv4lconvert/jidctflt.c
ed215eca170d07a6838be3796aad933adf89ece2 - libv4lconvert/hm12.c
193e9d2c997e21ce8ca02f25050fb8d2d62b6bfd - libv4lconvert/processing/autogain.c
c894d4a9b9e0f95be9e7bade4f7f734ae0c6996c - libv4lconvert/processing/libv4lprocessing.c
4158e9c3d21cf0a064b99bdf8571df4f3ba6d5bb - libv4lconvert/processing/whitebalance.c
be34baf0d2ce1374c841494ba27ccc352c444e98 - libv4lconvert/processing/gamma.c
b48edcb4036ee5f4e77e9cb49dd1b52b1e7f2427 - libv4lconvert/processing/libv4lprocessing.h
967d27c0e09849338a69dc4b5647d1824f2cd2fb - libv4lconvert/processing/libv4lprocessing-priv.h
9f382ff1f2895b926f3596e0a8ae7637b5e6a2ac - libv4lconvert/control/libv4lcontrol.c
8c523bd3838004a4fa00430a38cbc6dbaa47286f - libv4lconvert/control/libv4lcontrol.h
4ce5e891af7857a3504d1350ef13015997dde62a - libv4lconvert/control/libv4lcontrol-priv.h
bda0c1296aad2b3b178a76f38e26c20ce3b81233 - include/libv4l1.h
a95818658d95c1602f2422e5f1fb54b9ca70536e - include/libv4l2rds.h
2d1932763a064ef1176450b1bb4dd242aa3c77bb - include/libv4lconvert.h
3517bc15e30852ad01ad9966ec7111a16a2263df - include/libv4l2.h
944cd9bc32420e73da3a7d3cb1d0ce707263c9f0 - include/libv4l1-videodev.h
7d9d199f4e6f4ba6f0d669ee5decd74355507137 - include/libv4l-plugin.h
cfeaf0c16571e0c64bce50bcb8a6cd8d8c976187 - include/config.h
d7d4fd219e36edadc17837bff78eb8323e2a115e - include/libdvbv5/libdvb-version.h
33071176eda60242f39927e572ac6b1c1bab32e2 - include/libdvbv5/desc_event_extended.h
29c2cd2554e4f4285ab8709e6eace38046d8ecdc - include/libdvbv5/desc_t2_delivery.h
b42819db457bc6a23324faf259c0db1104741d99 - include/libdvbv5/dvb-scan.h
bae10c2fa28bb522e9fc0bb5e4b0128ab6f90e65 - include/libdvbv5/desc_partial_reception.h
f797ddac8e1adc6b29750f076df0f3a23cf630eb - include/libdvbv5/dvb-v5-std.h
48079dcae511070a8eeb55b004bd8ac06735ecc7 - include/libdvbv5/desc_ca.h
f4eb8b615b7c5a245c76e3f448e6cd848d8cf765 - include/libdvbv5/desc_cable_delivery.h
170a50b570691975e0ff22faffeb0131fc47c7c8 - include/libdvbv5/desc_logical_channel.h
c8f11e89456fc078c607d424e887babe72cd3684 - include/libdvbv5/nit.h
d39a24d0d0a0fbe732881dd2734b10d842d88de9 - include/libdvbv5/header.h
badc03677ddbcfa1d1188ddaaa4c272fccfe4a21 - include/libdvbv5/desc_atsc_service_location.h
d3bad046037cad4a95778555021a1a1a6b3040d9 - include/libdvbv5/cat.h
a614739513f62affb1059df81c064203d9fe2fcd - include/libdvbv5/desc_ca_identifier.h
6d70f699eb912c974dc06a19f9bdcb7dd0c27932 - include/libdvbv5/desc_service.h
0eda1161b34ca39b8528bc53d05fb0fc7d457f80 - include/libdvbv5/desc_frequency_list.h
22f6b663d50de467539201ecfdb363e9252c88bd - include/libdvbv5/atsc_eit.h
1c1292f060664abf6c12273a41b2bf6f704dd19b - include/libdvbv5/desc_sat.h
22de6de5e65530aa1ab38a2373db128e81983a4c - include/libdvbv5/desc_network_name.h
91178a5945e63c81051b57b7b2842139947a3b39 - include/libdvbv5/desc_extension.h
9ca776ad159cf84b2f68101bf279472741829047 - include/libdvbv5/descriptors.h
fa7e8d5e996310e1dd47d5467780d533c8a93ad8 - include/libdvbv5/dvb-dev.h
fed6115825c5b23e1e31ff80aab997f0a7b6c3fc - include/libdvbv5/sdt.h
4b892acd2ce77c060b50cf67f5faf9dc9b76edbb - include/libdvbv5/dvb-frontend.h
8b9f9e00f7bcdce58aef1d8d7234567af82e49b8 - include/libdvbv5/dvb-file.h
b8a077b8e3c6e337223fc37685d217a81df112f1 - include/libdvbv5/dvb-demux.h
01a0afbe1a4f41cf58f9ce47d65b31e3df7c8680 - include/libdvbv5/pat.h
4a49c8b46947be16356d6355aa36d72d7baa946c - include/libdvbv5/vct.h
c83200debc98ff8fa82030d36371a2caa230281a - include/libdvbv5/desc_language.h
21cea15c5d4faff2726f18bdf5b83e2210d472c4 - include/libdvbv5/mpeg_pes.h
825f75350da9fb148acc7d36af513da977105169 - include/libdvbv5/desc_isdbt_delivery.h
8f60bf43907b1eac3c1c2f031cf68a9bdebb3662 - include/libdvbv5/desc_ts_info.h
89cbb75f3e0952e4dfb145fdea776b45e05b7e76 - include/libdvbv5/desc_hierarchy.h
5c80a7ee23d1da2c91f60e79f7292248694b4f65 - include/libdvbv5/crc32.h
93b7b27ccc89e1ad4279eb12560e1dfc3d2ff1eb - include/libdvbv5/dvb-fe.h
aeeb75989de4b2ecafa1b2fc5ed30787999eec1b - include/libdvbv5/mpeg_es.h
dc684983365ffef353993faad0b105bcb87f4218 - include/libdvbv5/desc_event_short.h
5e3be50d1292109d59c80f920ba1b7a4bcbbdf56 - include/libdvbv5/eit.h
f95a0206952db4cce561455accad66a37f9a4104 - include/libdvbv5/mgt.h
88f8c78bbaf22b345e2ccb68e9b787e649dac2c1 - include/libdvbv5/libdvb-version.h.in
74a66644980a66adeba21ecc40ebefa2b4480e25 - include/libdvbv5/pmt.h
7671f125c6f61eef85f55eb314097435b3772431 - include/libdvbv5/dvb-sat.h
26fd40a10548cdb9bd4aafdda4486f9d39fd5c9e - include/libdvbv5/countries.h
65db3beaf2ec19a43a870b2759e6aecb15a95c7b - include/libdvbv5/atsc_header.h
149f7eb3c71b8f65127d3c4f2143080b115697e5 - include/libdvbv5/desc_terrestrial_delivery.h
708af2b77b2c94ee9cf593b31e20471c80ea556b - include/libdvbv5/mpeg_ts.h
ba205468e43ca16edf1ebec59dce9822044c1064 - include/libdvbv5/desc_registration_id.h
e70b24d8d1f3a248735c2d724d3b92c49b7aaba5 - include/libdvbv5/dvb-log.h
2d0557eadd4cff02ab52e26778d35b6c34f09158 - libv4l2/libv4l2-priv.h
487af43db163be6d3604371b9a504c1df2a9bf6c - libv4l2/v4l2-plugin-android.c
0ac38b46fa1659db4c05660205f212389295e7ff - libv4l2/Makefile
8ac3789333a6cd18e7f35d3b66a8a4d0293feb78 - libv4l2/Makefile.dGPU
fdd6e5e27aa8b41d5da77cb3b9be3fc3b8a44763 - libv4l2/libv4l2.export
1d37e5ea9231e7e0eea7d1a02938c6d1ae8ba791 - libv4l2/Makefile.in
51892a60925c37c865fe6eb9c923a552df180ebf - libv4l2/v4l2-plugin.c
d4c7daee644a35244ac8a5e1009a3aeb1d4fd3da - libv4l2/Makefile.am
87856c7113d150dceb254b5d548f942e7fcaf8f1 - libv4l2/libv4l2.c
d11ec5b8ce8390a72fd61457d7b8667378311191 - libv4l2/log.c
cbcee4426c19c168c6f49d04af3a0b2e30c0b681 - libv4l2/libv4l2.pc.in
55bb16d05817a3ecf076174175b2ca5b56e729d8 - libv4l2/v4l2convert.c

Change-Id: I4dd528ac6ac1e89b1e1ebadc7fdaf45cea9dcc98
2025-08-25 10:24:17 -07:00

2671 lines
66 KiB
C

/*
* Small jpeg decoder library
*
* Copyright (c) 2006, Luc Saillard <luc@saillard.org>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include "tinyjpeg.h"
#include "tinyjpeg-internal.h"
#include "libv4lconvert-priv.h"
enum std_markers {
DQT = 0xDB, /* Define Quantization Table */
SOF = 0xC0, /* Start of Frame (size information) */
DHT = 0xC4, /* Huffman Table */
SOI = 0xD8, /* Start of Image */
SOS = 0xDA, /* Start of Scan */
RST = 0xD0, /* Reset Marker d0 -> .. */
RST7 = 0xD7, /* Reset Marker .. -> d7 */
EOI = 0xD9, /* End of Image */
DRI = 0xDD, /* Define Restart Interval */
APP0 = 0xE0,
};
#define cY 0
#define cCb 1
#define cCr 2
#define BLACK_Y 0
#define BLACK_U 127
#define BLACK_V 127
#if DEBUG
#if LOG2FILE
#define trace(fmt, args...) do { \
FILE *f = fopen("/tmp/jpeg.log", "a"); \
fprintf(f, fmt, ## args); \
fflush(f); \
fclose(f); \
} while (0)
#else
#define trace(fmt, args...) do { \
fprintf(stderr, fmt, ## args); \
fflush(stderr); \
} while (0)
#endif
#else
#define trace(fmt, args...) do { } while (0)
#endif
#define error(fmt, args...) do { \
snprintf(priv->error_string, sizeof(priv->error_string), fmt, ## args); \
return -1; \
} while (0)
#if 0
static char *print_bits(unsigned int value, char *bitstr)
{
int i, j;
i = 31;
while (i > 0) {
if (value & (1UL << i))
break;
i--;
}
j = 0;
while (i >= 0) {
bitstr[j++] = (value & (1UL << i)) ? '1' : '0';
i--;
}
bitstr[j] = 0;
return bitstr;
}
static void print_next_16bytes(int offset, const unsigned char *stream)
{
trace("%4.4x: %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
offset,
stream[0], stream[1], stream[2], stream[3],
stream[4], stream[5], stream[6], stream[7],
stream[8], stream[9], stream[10], stream[11],
stream[12], stream[13], stream[14], stream[15]);
}
#endif
static const unsigned char zigzag[64] = {
0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63
};
/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
/* IMPORTANT: these are only valid for 8-bit data precision! */
static const unsigned char bits_dc_luminance[17] = {
0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
};
static const unsigned char val_dc_luminance[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
static const unsigned char bits_dc_chrominance[17] = {
0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
};
static const unsigned char val_dc_chrominance[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
static const unsigned char bits_ac_luminance[17] = {
0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
};
static const unsigned char val_ac_luminance[] = {
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa
};
static const unsigned char bits_ac_chrominance[17] = {
0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
};
static const unsigned char val_ac_chrominance[] = {
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa
};
#if 0 /* unused */
/* Standard JPEG quantization tables from Annex K of the JPEG standard.
Note unlike in Annex K the entries here are in zigzag order! */
const unsigned char standard_quantization[][64] = { {
0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
},
{
0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
},
};
#endif
/*
* 4 functions to manage the stream
*
* fill_nbits: put at least nbits in the reservoir of bits.
* But convert any 0xff,0x00 into 0xff
* get_nbits: read nbits from the stream, and put it in result,
* bits is removed from the stream and the reservoir is filled
* automaticaly. The result is signed according to the number of
* bits.
* look_nbits: read nbits from the stream without marking as read.
* skip_nbits: read nbits from the stream but do not return the result.
*
* stream: current pointer in the jpeg data (read bytes per bytes)
* nbits_in_reservoir: number of bits filled into the reservoir
* reservoir: register that contains bits information. Only nbits_in_reservoir
* is valid.
* nbits_in_reservoir
* <-- 17 bits -->
* Ex: 0000 0000 1010 0000 1111 0000 <== reservoir
* ^
* bit 1
* To get two bits from this example
* result = (reservoir >> 15) & 3
*
*/
#define fill_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
while (nbits_in_reservoir < nbits_wanted) { \
unsigned char c; \
if (stream >= priv->stream_end) { \
snprintf(priv->error_string, sizeof(priv->error_string), \
"fill_nbits error: need %u more bits\n", \
nbits_wanted - nbits_in_reservoir); \
longjmp(priv->jump_state, -EIO); \
} \
c = *stream++; \
reservoir <<= 8; \
if (c == 0xff && *stream == 0x00) \
stream++; \
reservoir |= c; \
nbits_in_reservoir += 8; \
} \
} while (0);
/* Signed version !!!! */
#define get_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted, result) do { \
fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
result = ((reservoir) >> (nbits_in_reservoir - (nbits_wanted))); \
nbits_in_reservoir -= (nbits_wanted); \
reservoir &= ((1U << nbits_in_reservoir) - 1); \
if ((unsigned int)result < (1UL << ((nbits_wanted) - 1))) \
result += (0xFFFFFFFFUL << (nbits_wanted)) + 1; \
} while (0);
#define look_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted, result) do { \
fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
result = ((reservoir) >> (nbits_in_reservoir - (nbits_wanted))); \
} while (0);
/* To speed up the decoding, we assume that the reservoir have enough bit
* slow version:
* #define skip_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
* fill_nbits(reservoir, nbits_in_reservoir, stream, (nbits_wanted)); \
* nbits_in_reservoir -= (nbits_wanted); \
* reservoir &= ((1U << nbits_in_reservoir) - 1); \
* } while(0);
*/
#define skip_nbits(reservoir, nbits_in_reservoir, stream, nbits_wanted) do { \
nbits_in_reservoir -= (nbits_wanted); \
reservoir &= ((1U << nbits_in_reservoir) - 1); \
} while (0);
#define be16_to_cpu(x) (((x)[0] << 8) | (x)[1])
static void resync(struct jdec_private *priv);
/**
* Get the next (valid) huffman code in the stream.
*
* To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
* lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
* and the value by using two lookup table.
* Else if the value is not found, just search (linear) into an array for each
* bits is the code is present.
*
* If the code is not present for any reason, -1 is return.
*/
static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table)
{
int value, hcode;
unsigned int extra_nbits, nbits;
uint16_t *slowtable;
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode);
value = huffman_table->lookup[hcode];
if (value >= 0) {
unsigned int code_size = huffman_table->code_size[value];
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size);
return value;
}
/* Decode more bits each time ... */
for (extra_nbits = 0; extra_nbits < 16 - HUFFMAN_HASH_NBITS; extra_nbits++) {
nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits;
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode);
slowtable = huffman_table->slowtable[extra_nbits];
/* Search if the code is in this array */
while (slowtable[0]) {
if (slowtable[0] == hcode) {
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits);
return slowtable[1];
}
slowtable += 2;
}
}
snprintf(priv->error_string, sizeof(priv->error_string),
"unknown huffman code: %08x\n", (unsigned int)hcode);
longjmp(priv->jump_state, -EIO);
return 0;
}
/**
*
* Decode a single block that contains the DCT coefficients.
* The table coefficients is already dezigzaged at the end of the operation.
*
*/
static void process_Huffman_data_unit(struct jdec_private *priv, int component)
{
unsigned char j;
unsigned int huff_code;
unsigned char size_val, count_0;
struct component *c = &priv->component_infos[component];
short int DCT[64];
/* Initialize the DCT coef table */
memset(DCT, 0, sizeof(DCT));
/* DC coefficient decoding */
huff_code = get_next_huffman_code(priv, c->DC_table);
if (huff_code) {
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]);
DCT[0] += c->previous_DC;
c->previous_DC = DCT[0];
} else {
DCT[0] = c->previous_DC;
}
/* AC coefficient decoding */
j = 1;
while (j < 64) {
huff_code = get_next_huffman_code(priv, c->AC_table);
size_val = huff_code & 0xF;
count_0 = huff_code >> 4;
if (size_val == 0) { /* RLE */
if (count_0 == 0)
break; /* EOB found, go out */
if (count_0 == 0xF)
j += 16; /* skip 16 zeros */
} else {
j += count_0; /* skip count_0 zeroes */
if (j < 64) {
get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]);
j++;
}
}
}
if (j > 64) {
snprintf(priv->error_string, sizeof(priv->error_string),
"error: more than 63 AC components (%d) in huffman unit\n", (int)j);
longjmp(priv->jump_state, -EIO);
}
for (j = 0; j < 64; j++)
c->DCT[j] = DCT[zigzag[j]];
}
/*
* Takes two array of bits, and build the huffman table for size, and code
*
* lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
* code_size will be used to known how many bits this symbol is encoded.
* slowtable will be used when the first lookup didn't give the result.
*/
static int build_huffman_table(struct jdec_private *priv, const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
{
unsigned int i, j, code, code_size, val, nbits;
unsigned char huffsize[257], *hz;
unsigned int huffcode[257], *hc;
int slowtable_used[16 - HUFFMAN_HASH_NBITS];
/*
* Build a temp array
* huffsize[X] => numbers of bits to write vals[X]
*/
hz = huffsize;
for (i = 1; i <= 16; i++) {
for (j = 1; j <= bits[i]; j++)
*hz++ = i;
}
*hz = 0;
memset(table->lookup, 0xff, sizeof(table->lookup));
for (i = 0; i < (16 - HUFFMAN_HASH_NBITS); i++)
slowtable_used[i] = 0;
/* Build a temp array
* huffcode[X] => code used to write vals[X]
*/
code = 0;
hc = huffcode;
hz = huffsize;
nbits = *hz;
while (*hz) {
while (*hz == nbits) {
*hc++ = code++;
hz++;
}
code <<= 1;
nbits++;
}
/*
* Build the lookup table, and the slowtable if needed.
*/
for (i = 0; huffsize[i]; i++) {
val = vals[i];
code = huffcode[i];
code_size = huffsize[i];
trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i, code, code_size);
table->code_size[val] = code_size;
if (code_size <= HUFFMAN_HASH_NBITS) {
/*
* Good: val can be put in the lookup table, so fill all value of this
* column with value val
*/
int repeat = 1UL << (HUFFMAN_HASH_NBITS - code_size);
code <<= HUFFMAN_HASH_NBITS - code_size;
while (repeat--)
table->lookup[code++] = val;
} else {
/* Perhaps sorting the array will be an optimization */
int slowtable_index = code_size - HUFFMAN_HASH_NBITS - 1;
if (slowtable_used[slowtable_index] == 254)
error("slow Huffman table overflow\n");
table->slowtable[slowtable_index][slowtable_used[slowtable_index]]
= code;
table->slowtable[slowtable_index][slowtable_used[slowtable_index] + 1]
= val;
slowtable_used[slowtable_index] += 2;
}
}
for (i = 0; i < (16 - HUFFMAN_HASH_NBITS); i++)
table->slowtable[i][slowtable_used[i]] = 0;
return 0;
}
static int build_default_huffman_tables(struct jdec_private *priv)
{
if ((priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
&& priv->default_huffman_table_initialized)
return 0;
if (build_huffman_table(priv, bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]))
return -1;
if (build_huffman_table(priv, bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]))
return -1;
if (build_huffman_table(priv, bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]))
return -1;
if (build_huffman_table(priv, bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]))
return -1;
priv->default_huffman_table_initialized = 1;
return 0;
}
/*******************************************************************************
*
* Colorspace conversion routine
*
*
* Note:
* YCbCr is defined per CCIR 601-1, except that Cb and Cr are
* normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
* The conversion equations to be implemented are therefore
* R = Y + 1.40200 * Cr
* G = Y - 0.34414 * Cb - 0.71414 * Cr
* B = Y + 1.77200 * Cb
*
******************************************************************************/
static unsigned char clamp(int i)
{
if (i < 0)
return 0;
if (i > 255)
return 255;
return i;
}
/**
* YCrCb -> YUV420P (1x1)
* .---.
* | 1 |
* `---'
*/
static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
{
const unsigned char *s, *y;
unsigned char *p;
int i, j;
p = priv->plane[0];
y = priv->Y;
for (i = 0; i < 8; i++) {
memcpy(p, y, 8);
p += priv->width;
y += 8;
}
p = priv->plane[1];
s = priv->Cb;
for (i = 0; i < 8; i += 2) {
for (j = 0; j < 8; j += 2, s += 2)
*p++ = *s;
s += 8; /* Skip one line */
p += priv->width / 2 - 4;
}
p = priv->plane[2];
s = priv->Cr;
for (i = 0; i < 8; i += 2) {
for (j = 0; j < 8; j += 2, s += 2)
*p++ = *s;
s += 8; /* Skip one line */
p += priv->width / 2 - 4;
}
}
/**
* YCrCb -> YUV420P (2x1)
* .-------.
* | 1 | 2 |
* `-------'
*/
static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
{
unsigned char *p;
const unsigned char *s, *y1;
unsigned int i;
p = priv->plane[0];
y1 = priv->Y;
for (i = 0; i < 8; i++) {
memcpy(p, y1, 16);
p += priv->width;
y1 += 16;
}
p = priv->plane[1];
s = priv->Cb;
for (i = 0; i < 8; i += 2) {
memcpy(p, s, 8);
s += 16; /* Skip one line */
p += priv->width / 2;
}
p = priv->plane[2];
s = priv->Cr;
for (i = 0; i < 8; i += 2) {
memcpy(p, s, 8);
s += 16; /* Skip one line */
p += priv->width/2;
}
}
/**
* YCrCb -> YUV420P (1x2)
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
{
const unsigned char *s, *y;
unsigned char *p;
int i, j;
p = priv->plane[0];
y = priv->Y;
for (i = 0; i < 16; i++) {
memcpy(p, y, 8);
p += priv->width;
y += 8;
}
p = priv->plane[1];
s = priv->Cb;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j += 2, s += 2)
*p++ = *s;
p += priv->width / 2 - 4;
}
p = priv->plane[2];
s = priv->Cr;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j += 2, s += 2)
*p++ = *s;
p += priv->width / 2 - 4;
}
}
/**
* YCrCb -> YUV420P (2x2)
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
{
unsigned char *p;
const unsigned char *s, *y1;
unsigned int i;
p = priv->plane[0];
y1 = priv->Y;
for (i = 0; i < 16; i++) {
memcpy(p, y1, 16);
p += priv->width;
y1 += 16;
}
p = priv->plane[1];
s = priv->Cb;
for (i = 0; i < 8; i++) {
memcpy(p, s, 8);
s += 8;
p += priv->width / 2;
}
p = priv->plane[2];
s = priv->Cr;
for (i = 0; i < 8; i++) {
memcpy(p, s, 8);
s += 8;
p += priv->width / 2;
}
}
/**
* YCrCb -> RGB24 (1x1)
* .---.
* | 1 |
* `---'
*/
static void YCrCB_to_RGB24_1x1(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = priv->width * 3 - 8 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
y = (*Y++) << SCALEBITS;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
}
p += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/**
* YCrCb -> BGR24 (1x1)
* .---.
* | 1 |
* `---'
*/
static void YCrCB_to_BGR24_1x1(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = priv->width * 3 - 8 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
y = (*Y++) << SCALEBITS;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
}
p += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/**
* YCrCb -> RGB24 (2x1)
* .-------.
* | 1 | 2 |
* `-------'
*/
static void YCrCB_to_RGB24_2x1(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = priv->width * 3 - 16 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
y = (*Y++) << SCALEBITS;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
y = (*Y++) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
}
p += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/*
* YCrCb -> BGR24 (2x1)
* .-------.
* | 1 | 2 |
* `-------'
*/
static void YCrCB_to_BGR24_2x1(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = priv->width * 3 - 16 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
y = (*Y++) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
y = (*Y++) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
}
p += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/**
* YCrCb -> RGB24 (1x2)
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void YCrCB_to_RGB24_1x2(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p, *p2;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
p2 = priv->plane[0] + priv->width * 3;
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = 2 * priv->width * 3 - 8 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
y = (*Y++) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
y = (Y[8-1]) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
}
Y += 8;
p += offset_to_next_row;
p2 += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/*
* YCrCb -> BGR24 (1x2)
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p, *p2;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
p2 = priv->plane[0] + priv->width * 3;
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = 2 * priv->width * 3 - 8 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
y = (*Y++) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
y = (Y[8-1]) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
}
Y += 8;
p += offset_to_next_row;
p2 += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/**
* YCrCb -> RGB24 (2x2)
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void YCrCB_to_RGB24_2x2(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p, *p2;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
p2 = priv->plane[0] + priv->width * 3;
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = (priv->width * 3 * 2) - 16 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
y = (*Y++) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
y = (*Y++) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
y = (Y[16-2]) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
y = (Y[16-1]) << SCALEBITS;
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
}
Y += 16;
p += offset_to_next_row;
p2 += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/*
* YCrCb -> BGR24 (2x2)
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
{
const unsigned char *Y, *Cb, *Cr;
unsigned char *p, *p2;
int i, j;
int offset_to_next_row;
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
p = priv->plane[0];
p2 = priv->plane[0] + priv->width * 3;
Y = priv->Y;
Cb = priv->Cb;
Cr = priv->Cr;
offset_to_next_row = (priv->width * 3 * 2) - 16 * 3;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
int y, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *Cb++ - 128;
cr = *Cr++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
y = (*Y++) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
y = (*Y++) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p++ = clamp(r);
y = (Y[16-2]) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
y = (Y[16-1]) << SCALEBITS;
b = (y + add_b) >> SCALEBITS;
*p2++ = clamp(b);
g = (y + add_g) >> SCALEBITS;
*p2++ = clamp(g);
r = (y + add_r) >> SCALEBITS;
*p2++ = clamp(r);
}
Y += 16;
p += offset_to_next_row;
p2 += offset_to_next_row;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
}
/**
* YCrCb -> Grey (1x1)
* .---.
* | 1 |
* `---'
*/
static void YCrCB_to_Grey_1x1(struct jdec_private *priv)
{
const unsigned char *y;
unsigned char *p;
unsigned int i;
int offset_to_next_row;
p = priv->plane[0];
y = priv->Y;
offset_to_next_row = priv->width;
for (i = 0; i < 8; i++) {
memcpy(p, y, 8);
y += 8;
p += offset_to_next_row;
}
}
/**
* YCrCb -> Grey (2x1)
* .-------.
* | 1 | 2 |
* `-------'
*/
static void YCrCB_to_Grey_2x1(struct jdec_private *priv)
{
const unsigned char *y;
unsigned char *p;
unsigned int i;
p = priv->plane[0];
y = priv->Y;
for (i = 0; i < 8; i++) {
memcpy(p, y, 16);
y += 16;
p += priv->width;
}
}
/**
* YCrCb -> Grey (1x2)
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void YCrCB_to_Grey_1x2(struct jdec_private *priv)
{
const unsigned char *y;
unsigned char *p;
unsigned int i;
p = priv->plane[0];
y = priv->Y;
for (i = 0; i < 16; i++) {
memcpy(p, y, 8);
y += 8;
p += priv->width;
}
}
/**
* YCrCb -> Grey (2x2)
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void YCrCB_to_Grey_2x2(struct jdec_private *priv)
{
const unsigned char *y;
unsigned char *p;
unsigned int i;
p = priv->plane[0];
y = priv->Y;
for (i = 0; i < 16; i++) {
memcpy(p, y, 16);
y += 16;
p += priv->width;
}
}
/*
* Decode all the 3 components for 1x1
*/
static void decode_MCU_1x1_3planes(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 8);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
/*
* Decode a 1x1 directly in 1 color
*/
static void decode_MCU_1x1_1plane(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 8);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
/*
* Decode a 2x1
* .-------.
* | 1 | 2 |
* `-------'
*/
static void decode_MCU_2x1_3planes(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
static void build_quantization_table(float *qtable, const unsigned char *ref_table);
static void pixart_decode_MCU_2x1_3planes(struct jdec_private *priv)
{
unsigned char marker;
look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream,
8, marker);
/* Sometimes the pac7302 switches chrominance setting halfway though a
frame, with a quite ugly looking result, so we drop such frames. */
if (priv->first_marker == 0)
priv->first_marker = marker;
else if ((marker & 0x80) != (priv->first_marker & 0x80)) {
snprintf(priv->error_string, sizeof(priv->error_string),
"Pixart JPEG error: chrominance changed halfway\n");
longjmp(priv->jump_state, -EIO);
}
/* Pixart JPEG MCU-s are preceded by a marker indicating the quality
setting with which the MCU is compressed, IOW the MCU-s may have a
different quantization table per MCU. So if the marker changes we
need to rebuild the quantization tables. */
if (marker != priv->marker) {
int i, j, comp, lumi;
unsigned char qt[64];
/* These values have been found by trial and error and seem to
work reasonably. Markers with index 0 - 7 are never
generated by the hardware, so they are likely wrong. */
const int qfactor[32] = {
25, 30, 35, 40, 45, 50, 55, 60,
65, 70, 75, 80, 85, 90, 95, 100,
100, 100, 120, 140, 160, 180, 210, 240,
270, 300, 330, 360, 390, 420, 450, 480
};
/* These tables were found in SPC230NC.SYS */
const unsigned char pixart_q[][64] = { {
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
}, {
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
}, {
0x08, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
}, {
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
} };
i = (marker & 0x7c) >> 2; /* Bits 0 and 1 are always 0 */
comp = qfactor[i];
lumi = (marker & 0x40) ? 1 : 0;
/* printf("marker %02x comp %d lumi %d\n", marker, comp, lumi); */
/* Note the DC quantization factor is fixed! */
qt[0] = pixart_q[lumi][0];
for (i = 1; i < 64; i++) {
j = (pixart_q[lumi][i] * comp + 50) / 100;
qt[i] = (j < 255) ? j : 255;
}
build_quantization_table(priv->Q_tables[0], qt);
/* If bit 7 of the marker is set chrominance uses the
luminance quantization table */
if (!(marker & 0x80)) {
qt[0] = pixart_q[3][0];
for (i = 1; i < 64; i++) {
j = (pixart_q[3][i] * comp + 50) / 100;
qt[i] = (j < 255) ? j : 255;
}
}
build_quantization_table(priv->Q_tables[1], qt);
priv->marker = marker;
}
skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, 8);
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
/*
* Decode a 2x1
* .-------.
* | 1 | 2 |
* `-------'
*/
static void decode_MCU_2x1_1plane(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
// Cb
process_Huffman_data_unit(priv, cCb);
// Cr
process_Huffman_data_unit(priv, cCr);
}
/*
* Decode a 2x2
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void decode_MCU_2x2_3planes(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64 * 2, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64 * 2 + 8, 16);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
/*
* Decode a 2x2 directly in GREY format (8bits)
* .-------.
* | 1 | 2 |
* |---+---|
* | 3 | 4 |
* `-------'
*/
static void decode_MCU_2x2_1plane(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 8, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64 * 2, 16);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64 * 2 + 8, 16);
// Cb
process_Huffman_data_unit(priv, cCb);
// Cr
process_Huffman_data_unit(priv, cCr);
}
/*
* Decode a 1x2 mcu
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void decode_MCU_1x2_3planes(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 8);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64, 8);
// Cb
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], priv->Cb, 8);
// Cr
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], priv->Cr, 8);
}
/*
* Decode a 1x2 mcu
* .---.
* | 1 |
* |---|
* | 2 |
* `---'
*/
static void decode_MCU_1x2_1plane(struct jdec_private *priv)
{
// Y
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y, 8);
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], priv->Y + 64, 8);
// Cb
process_Huffman_data_unit(priv, cCb);
// Cr
process_Huffman_data_unit(priv, cCr);
}
static void print_SOF(const unsigned char *stream)
{
#if DEBUG
int width, height, nr_components, precision;
const char *nr_components_to_string[] = {
"????",
"Grayscale",
"????",
"YCbCr",
"CYMK"
};
precision = stream[2];
height = be16_to_cpu(stream + 3);
width = be16_to_cpu(stream + 5);
nr_components = stream[7];
trace("> SOF marker\n");
trace("Size:%dx%d nr_components:%d (%s) precision:%d\n",
width, height,
nr_components, nr_components_to_string[nr_components],
precision);
#endif
}
/*******************************************************************************
*
* JPEG/JFIF Parsing functions
*
* Note: only a small subset of the jpeg file format is supported. No markers,
* nor progressive stream is supported.
*
******************************************************************************/
static void build_quantization_table(float *qtable, const unsigned char *ref_table)
{
/* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
* For float AA&N IDCT method, divisors are equal to quantization
* coefficients scaled by scalefactor[row]*scalefactor[col], where
* scalefactor[0] = 1
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
* We apply a further scale factor of 8.
* What's actually stored is 1/divisor so that the inner loop can
* use a multiplication rather than a division.
*/
int i, j;
static const double aanscalefactor[8] = {
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
};
const unsigned char *zz = zigzag;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
*qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
}
static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
{
int qi;
float *table;
const unsigned char *dqt_block_end;
trace("> DQT marker\n");
dqt_block_end = stream + be16_to_cpu(stream);
stream += 2; /* Skip length */
while (stream < dqt_block_end) {
qi = *stream++;
#if SANITY_CHECK
if (qi >> 4)
error("16 bits quantization table is not supported\n");
if (qi >= COMPONENTS)
error("No more than %d quantization tables supported (got %d)\n",
COMPONENTS, qi + 1);
#endif
table = priv->Q_tables[qi];
build_quantization_table(table, stream);
stream += 64;
}
trace("< DQT marker\n");
return 0;
}
static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
{
int i, width, height, nr_components, cid, sampling_factor;
int Q_table;
struct component *c;
trace("> SOF marker\n");
print_SOF(stream);
height = be16_to_cpu(stream+3);
width = be16_to_cpu(stream+5);
nr_components = stream[7];
#if SANITY_CHECK
if (stream[2] != 8)
error("Precision other than 8 is not supported\n");
if (width > JPEG_MAX_WIDTH || height > JPEG_MAX_HEIGHT)
error("Width and Height (%dx%d) seems suspicious\n", width, height);
if (nr_components != 3)
error("We only support YUV images\n");
if (height % 8)
error("Height need to be a multiple of 8 (current height is %d)\n", height);
if (width % 16)
error("Width need to be a multiple of 16 (current Width is %d)\n", width);
#endif
stream += 8;
for (i = 0; i < nr_components; i++) {
cid = *stream++;
sampling_factor = *stream++;
Q_table = *stream++;
c = &priv->component_infos[i];
#if SANITY_CHECK
c->cid = cid;
#endif
c->Vfactor = sampling_factor & 0xf;
c->Hfactor = sampling_factor >> 4;
c->Q_table = priv->Q_tables[Q_table];
trace("Component:%d factor:%dx%d Quantization table:%d\n",
cid, c->Hfactor, c->Hfactor, Q_table);
}
priv->width = width;
priv->height = height;
trace("< SOF marker\n");
return 0;
}
static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int i, cid, table;
unsigned int nr_components = stream[2];
trace("> SOS marker\n");
#if SANITY_CHECK
if (nr_components != 3 && nr_components != 1)
error("We only support YCbCr image\n");
#endif
if (nr_components == 1)
priv->flags |= TINYJPEG_FLAGS_PLANAR_JPEG;
#if SANITY_CHECK
else if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
error("SOS with more than 1 component while decoding planar JPEG\n");
#endif
stream += 3;
for (i = 0; i < nr_components; i++) {
cid = *stream++;
table = *stream++;
if (nr_components == 1) {
#if SANITY_CHECK
/* Find matching cid so we store the tables in the right component */
for (i = 0; i < COMPONENTS; i++)
if (priv->component_infos[i].cid == cid)
break;
if (i == COMPONENTS)
error("Unknown cid in SOS: %u\n", cid);
priv->current_cid = cid;
#else
i = cid - 1;
#endif
trace("SOS cid: %u, using component_info: %u\n", cid, i);
}
#if SANITY_CHECK
if ((table & 0xf) >= HUFFMAN_TABLES)
error("We do not support more than %d AC Huffman table\n",
HUFFMAN_TABLES);
if ((table >> 4) >= HUFFMAN_TABLES)
error("We do not support more than %d DC Huffman table\n",
HUFFMAN_TABLES);
if (cid != priv->component_infos[i].cid)
error("SOS cid order (%u:%u) isn't compatible with the SOF marker (%u:%u)\n",
i, cid, i, priv->component_infos[i].cid);
trace("ComponentId:%u tableAC:%d tableDC:%d\n", cid, table & 0xf, table >> 4);
#endif
priv->component_infos[i].AC_table = &priv->HTAC[table & 0xf];
priv->component_infos[i].DC_table = &priv->HTDC[table >> 4];
}
priv->stream = stream + 3;
/* ITU-T T.81 (9/92) chapter E.1.3 clearly states that RSTm is to be set to 0 at the beginning of each scan */
priv->last_rst_marker_seen = 0;
trace("< SOS marker\n");
return 0;
}
static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int count, i;
unsigned char huff_bits[17];
int length, index;
length = be16_to_cpu(stream) - 2;
stream += 2; /* Skip length */
trace("> DHT marker (length=%d)\n", length);
while (length > 0) {
index = *stream++;
/* We need to calculate the number of bytes 'vals' will takes */
huff_bits[0] = 0;
count = 0;
for (i = 1; i < 17; i++) {
huff_bits[i] = *stream++;
count += huff_bits[i];
}
#if SANITY_CHECK
if (count > 1024)
error("No more than 1024 bytes is allowed to describe a huffman table\n");
if ((index & 0xf) >= HUFFMAN_TABLES)
error("No mode than %d Huffman tables is supported\n", HUFFMAN_TABLES);
trace("Huffman table %s n%d\n", (index & 0xf0) ? "AC" : "DC", index & 0xf);
trace("Length of the table: %d\n", count);
#endif
if (index & 0xf0) {
if (build_huffman_table(priv, huff_bits, stream, &priv->HTAC[index & 0xf]))
return -1;
} else {
if (build_huffman_table(priv, huff_bits, stream, &priv->HTDC[index & 0xf]))
return -1;
}
length -= 1;
length -= 16;
length -= count;
stream += count;
}
trace("< DHT marker\n");
return 0;
}
static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
{
unsigned int length;
trace("> DRI marker\n");
length = be16_to_cpu(stream);
#if SANITY_CHECK
if (length != 4)
error("Length of DRI marker need to be 4\n");
#endif
priv->restart_interval = be16_to_cpu(stream + 2);
#if DEBUG
trace("Restart interval = %d\n", priv->restart_interval);
#endif
trace("< DRI marker\n");
return 0;
}
static void resync(struct jdec_private *priv)
{
int i;
/* Init DC coefficients */
for (i = 0; i < COMPONENTS; i++)
priv->component_infos[i].previous_DC = 0;
priv->reservoir = 0;
priv->nbits_in_reservoir = 0;
if (priv->restart_interval > 0)
priv->restarts_to_go = priv->restart_interval;
else
priv->restarts_to_go = -1;
}
static int find_next_rst_marker(struct jdec_private *priv)
{
int rst_marker_found = 0;
int marker;
const unsigned char *stream = priv->stream;
/* Parse marker */
while (!rst_marker_found) {
while (*stream++ != 0xff) {
if (stream >= priv->stream_end)
error("EOF while search for a RST marker.\n");
}
/* Skip any padding ff byte (this is normal) */
while (*stream == 0xff) {
stream++;
if (stream >= priv->stream_end)
error("EOF while search for a RST marker.\n");
}
marker = *stream++;
if ((RST + priv->last_rst_marker_seen) == marker)
rst_marker_found = 1;
else if (marker >= RST && marker <= RST7)
error("Wrong Reset marker found, abording\n");
else if (marker == EOI)
return 0;
}
priv->stream = stream;
priv->last_rst_marker_seen++;
priv->last_rst_marker_seen &= 7;
return 0;
}
static int find_next_sos_marker(struct jdec_private *priv)
{
const unsigned char *stream = priv->stream;
/* Parse marker */
while (1) {
while (*stream++ != 0xff) {
if (stream >= priv->stream_end)
error("EOF while search for a SOS marker.\n");
}
/* Skip any padding ff byte (this is normal) */
while (*stream == 0xff) {
stream++;
if (stream >= priv->stream_end)
error("EOF while search for a SOS marker.\n");
}
if (*stream++ == SOS)
break; /* Found it ! */
}
priv->stream = stream;
return 0;
}
static int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
{
int chuck_len;
int marker;
int sof_marker_found = 0;
int dqt_marker_found = 0;
int sos_marker_found = 0;
int dht_marker_found = 0;
const unsigned char *next_chunck;
/* Parse marker */
while (!sos_marker_found) {
if (*stream++ != 0xff)
goto bogus_jpeg_format;
/* Skip any padding ff byte (this is normal) */
while (*stream == 0xff)
stream++;
marker = *stream++;
chuck_len = be16_to_cpu(stream);
next_chunck = stream + chuck_len;
switch (marker) {
case SOF:
if (parse_SOF(priv, stream) < 0)
return -1;
sof_marker_found = 1;
break;
case DQT:
if (parse_DQT(priv, stream) < 0)
return -1;
dqt_marker_found = 1;
break;
case SOS:
if (parse_SOS(priv, stream) < 0)
return -1;
sos_marker_found = 1;
break;
case DHT:
if (parse_DHT(priv, stream) < 0)
return -1;
dht_marker_found = 1;
break;
case DRI:
if (parse_DRI(priv, stream) < 0)
return -1;
break;
default:
trace("> Unknown marker %2.2x\n", marker);
break;
}
stream = next_chunck;
}
if (!sof_marker_found ||
(!dqt_marker_found && !(priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)))
goto bogus_jpeg_format;
if (!dht_marker_found) {
trace("No Huffman table loaded, using the default one\n");
if (build_default_huffman_tables(priv))
return -1;
}
#ifdef SANITY_CHECK
if ((priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
|| (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
if ((priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
|| (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
if ((priv->component_infos[cCb].Hfactor != 1)
|| (priv->component_infos[cCr].Hfactor != 1)
|| (priv->component_infos[cCb].Vfactor != 1)
|| (priv->component_infos[cCr].Vfactor != 1))
error("Sampling other than 1x1 for Cr and Cb is not supported\n");
if ((priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG) &&
((priv->component_infos[cY].Hfactor != 2)
|| (priv->component_infos[cY].Vfactor != 2)))
error("Sampling other than 2x2 for Y is not supported with planar JPEG\n");
#endif
return 0;
bogus_jpeg_format:
error("Bogus jpeg format\n");
return -1;
}
/*******************************************************************************
*
* Functions exported of the library.
*
* Note: Some applications can access directly to internal pointer of the
* structure. It's is not recommended, but if you have many images to
* uncompress with the same parameters, some functions can be called to speedup
* the decoding.
*
******************************************************************************/
/**
* Allocate a new tinyjpeg decoder object.
*
* Before calling any other functions, an object need to be called.
*/
struct jdec_private *tinyjpeg_init(void)
{
struct jdec_private *priv;
priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private));
if (priv == NULL)
return NULL;
return priv;
}
/**
* Free a tinyjpeg object.
*
* No others function can be called after this one.
*/
void tinyjpeg_free(struct jdec_private *priv)
{
int i;
for (i = 0; i < COMPONENTS; i++) {
free(priv->components[i]);
free(priv->tmp_buf[i]);
priv->components[i] = NULL;
priv->tmp_buf[i] = NULL;
}
priv->tmp_buf_y_size = 0;
free(priv->stream_filtered);
free(priv);
}
/**
* Initialize the tinyjpeg object and prepare the decoding of the stream.
*
* Check if the jpeg can be decoded with this jpeg decoder.
* Fill some table used for preprocessing.
*/
int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size)
{
/* Identify the file */
if ((buf[0] != 0xFF) || (buf[1] != SOI))
error("Not a JPG file ?\n");
priv->stream_end = buf + size;
return parse_JFIF(priv, buf + 2);
}
static const decode_MCU_fct decode_mcu_3comp_table[4] = {
decode_MCU_1x1_3planes,
decode_MCU_1x2_3planes,
decode_MCU_2x1_3planes,
decode_MCU_2x2_3planes,
};
static const decode_MCU_fct pixart_decode_mcu_3comp_table[4] = {
NULL,
NULL,
pixart_decode_MCU_2x1_3planes,
NULL,
};
static const decode_MCU_fct decode_mcu_1comp_table[4] = {
decode_MCU_1x1_1plane,
decode_MCU_1x2_1plane,
decode_MCU_2x1_1plane,
decode_MCU_2x2_1plane,
};
static const convert_colorspace_fct convert_colorspace_yuv420p[4] = {
YCrCB_to_YUV420P_1x1,
YCrCB_to_YUV420P_1x2,
YCrCB_to_YUV420P_2x1,
YCrCB_to_YUV420P_2x2,
};
static const convert_colorspace_fct convert_colorspace_rgb24[4] = {
YCrCB_to_RGB24_1x1,
YCrCB_to_RGB24_1x2,
YCrCB_to_RGB24_2x1,
YCrCB_to_RGB24_2x2,
};
static const convert_colorspace_fct convert_colorspace_bgr24[4] = {
YCrCB_to_BGR24_1x1,
YCrCB_to_BGR24_1x2,
YCrCB_to_BGR24_2x1,
YCrCB_to_BGR24_2x2,
};
static const convert_colorspace_fct convert_colorspace_grey[4] = {
YCrCB_to_Grey_1x1,
YCrCB_to_Grey_1x2,
YCrCB_to_Grey_2x1,
YCrCB_to_Grey_2x2,
};
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt);
/* This function parses and removes the special Pixart JPEG chunk headers */
static int pixart_filter(struct jdec_private *priv, unsigned char *dest,
const unsigned char *src, int n)
{
int chunksize, copied = 0;
/* The first data bytes encodes the image size:
0x60: 160x120
0x61: 320x240
0x62: 640x480
160x120 images are not chunked due to their small size!
*/
if (src[0] == 0x60) {
memcpy(dest, src + 1, n - 1);
return n - 1;
}
src++;
n--;
/* The first chunk is always 1024 bytes, 5 bytes are dropped in the
kernel: 0xff 0xff 0x00 0xff 0x96, and we skip one unknown byte */
chunksize = 1024 - 6;
while (1) {
if (n < chunksize)
break; /* Short frame */
memcpy(dest, src, chunksize);
dest += chunksize;
src += chunksize;
copied += chunksize;
n -= chunksize;
if (n < 4)
break; /* Short frame */
if (src[0] != 0xff || src[1] != 0xff || src[2] != 0xff)
error("Missing Pixart ff ff ff xx header, "
"got: %02x %02x %02x %02x, copied sofar: %d\n",
src[0], src[1], src[2], src[3], copied);
if (src[3] > 6)
error("Unexpected Pixart chunk size: %d\n", src[3]);
chunksize = src[3];
src += 4;
n -= 4;
if (chunksize == 0) {
/* 0 indicates we are done, copy whatever remains */
memcpy(dest, src, n);
return copied + n;
}
chunksize = 2048 >> chunksize;
}
error("Short Pixart JPEG frame\n");
}
/**
* Decode and convert the jpeg image into @pixfmt@ image
*
* Note: components will be automaticaly allocated if no memory is attached.
*/
int tinyjpeg_decode(struct jdec_private *priv, int pixfmt)
{
unsigned int x, y, xstride_by_mcu, ystride_by_mcu;
unsigned int bytes_per_blocklines[3], bytes_per_mcu[3];
decode_MCU_fct decode_MCU;
const decode_MCU_fct *decode_mcu_table;
const convert_colorspace_fct *colorspace_array_conv;
convert_colorspace_fct convert_to_pixfmt;
if (setjmp(priv->jump_state))
return -1;
if (priv->flags & TINYJPEG_FLAGS_PLANAR_JPEG)
return tinyjpeg_decode_planar(priv, pixfmt);
/* To keep gcc happy initialize some array */
bytes_per_mcu[1] = 0;
bytes_per_mcu[2] = 0;
bytes_per_blocklines[1] = 0;
bytes_per_blocklines[2] = 0;
decode_mcu_table = decode_mcu_3comp_table;
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
int length;
priv->stream_filtered =
v4lconvert_alloc_buffer(priv->stream_end - priv->stream,
&priv->stream_filtered,
&priv->stream_filtered_bufsize);
if (!priv->stream_filtered)
error("Out of memory!\n");
length = pixart_filter(priv, priv->stream_filtered,
priv->stream, priv->stream_end - priv->stream);
if (length < 0)
return length;
priv->stream = priv->stream_filtered;
priv->stream_end = priv->stream + length;
priv->first_marker = 0;
decode_mcu_table = pixart_decode_mcu_3comp_table;
}
switch (pixfmt) {
case TINYJPEG_FMT_YUV420P:
colorspace_array_conv = convert_colorspace_yuv420p;
if (priv->components[0] == NULL)
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
if (priv->components[1] == NULL)
priv->components[1] = (uint8_t *)malloc(priv->width * priv->height/4);
if (priv->components[2] == NULL)
priv->components[2] = (uint8_t *)malloc(priv->width * priv->height/4);
bytes_per_blocklines[0] = priv->width;
bytes_per_blocklines[1] = priv->width/4;
bytes_per_blocklines[2] = priv->width/4;
bytes_per_mcu[0] = 8;
bytes_per_mcu[1] = 4;
bytes_per_mcu[2] = 4;
break;
case TINYJPEG_FMT_RGB24:
colorspace_array_conv = convert_colorspace_rgb24;
if (priv->components[0] == NULL)
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
bytes_per_blocklines[0] = priv->width * 3;
bytes_per_mcu[0] = 3*8;
break;
case TINYJPEG_FMT_BGR24:
colorspace_array_conv = convert_colorspace_bgr24;
if (priv->components[0] == NULL)
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
bytes_per_blocklines[0] = priv->width * 3;
bytes_per_mcu[0] = 3*8;
break;
case TINYJPEG_FMT_GREY:
decode_mcu_table = decode_mcu_1comp_table;
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG)
error("Greyscale output not support for PIXART JPEG's\n");
colorspace_array_conv = convert_colorspace_grey;
if (priv->components[0] == NULL)
priv->components[0] = (uint8_t *)malloc(priv->width * priv->height);
bytes_per_blocklines[0] = priv->width;
bytes_per_mcu[0] = 8;
break;
default:
error("Bad pixel format\n");
}
xstride_by_mcu = ystride_by_mcu = 8;
if ((priv->component_infos[cY].Hfactor | priv->component_infos[cY].Vfactor) == 1) {
decode_MCU = decode_mcu_table[0];
convert_to_pixfmt = colorspace_array_conv[0];
trace("Use decode 1x1 sampling\n");
} else if (priv->component_infos[cY].Hfactor == 1) {
decode_MCU = decode_mcu_table[1];
convert_to_pixfmt = colorspace_array_conv[1];
ystride_by_mcu = 16;
trace("Use decode 1x2 sampling (not supported)\n");
} else if (priv->component_infos[cY].Vfactor == 2) {
decode_MCU = decode_mcu_table[3];
convert_to_pixfmt = colorspace_array_conv[3];
xstride_by_mcu = 16;
ystride_by_mcu = 16;
trace("Use decode 2x2 sampling\n");
} else {
decode_MCU = decode_mcu_table[2];
convert_to_pixfmt = colorspace_array_conv[2];
xstride_by_mcu = 16;
trace("Use decode 2x1 sampling\n");
}
if (decode_MCU == NULL)
error("no decode MCU function for this JPEG format (PIXART?)\n");
resync(priv);
/* Don't forget to that block can be either 8 or 16 lines */
bytes_per_blocklines[0] *= ystride_by_mcu;
bytes_per_blocklines[1] *= ystride_by_mcu;
bytes_per_blocklines[2] *= ystride_by_mcu;
bytes_per_mcu[0] *= xstride_by_mcu / 8;
bytes_per_mcu[1] *= xstride_by_mcu / 8;
bytes_per_mcu[2] *= xstride_by_mcu / 8;
/* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
for (y = 0; y < priv->height / ystride_by_mcu; y++) {
//trace("Decoding row %d\n", y);
priv->plane[0] = priv->components[0] + (y * bytes_per_blocklines[0]);
priv->plane[1] = priv->components[1] + (y * bytes_per_blocklines[1]);
priv->plane[2] = priv->components[2] + (y * bytes_per_blocklines[2]);
for (x = 0; x < priv->width; x += xstride_by_mcu) {
decode_MCU(priv);
convert_to_pixfmt(priv);
priv->plane[0] += bytes_per_mcu[0];
priv->plane[1] += bytes_per_mcu[1];
priv->plane[2] += bytes_per_mcu[2];
if (priv->restarts_to_go > 0) {
priv->restarts_to_go--;
if (priv->restarts_to_go == 0) {
priv->stream -= (priv->nbits_in_reservoir / 8);
resync(priv);
if (find_next_rst_marker(priv) < 0)
return -1;
}
}
}
}
if (priv->flags & TINYJPEG_FLAGS_PIXART_JPEG) {
/* Additional sanity check for funky Pixart format */
if ((priv->stream_end - priv->stream) > 5)
error("Pixart JPEG error, stream does not end with EOF marker\n");
}
return 0;
}
int tinyjpeg_decode_planar(struct jdec_private *priv, int pixfmt)
{
unsigned int i, x, y;
uint8_t *y_buf, *u_buf, *v_buf, *p, *p2;
switch (pixfmt) {
case TINYJPEG_FMT_GREY:
error("Greyscale output not supported with planar JPEG input\n");
break;
case TINYJPEG_FMT_RGB24:
case TINYJPEG_FMT_BGR24:
if (priv->tmp_buf_y_size < (priv->width * priv->height)) {
for (i = 0; i < COMPONENTS; i++) {
free(priv->tmp_buf[i]);
priv->tmp_buf[i] = malloc(priv->width * priv->height / (i ? 4 : 1));
if (!priv->tmp_buf[i])
error("Could not allocate memory for temporary buffers\n");
}
priv->tmp_buf_y_size = priv->width * priv->height;
}
y_buf = priv->tmp_buf[cY];
u_buf = priv->tmp_buf[cCb];
v_buf = priv->tmp_buf[cCr];
break;
case TINYJPEG_FMT_YUV420P:
y_buf = priv->components[cY];
u_buf = priv->components[cCb];
v_buf = priv->components[cCr];
break;
default:
error("Bad pixel format\n");
}
#if SANITY_CHECK
if (priv->current_cid != priv->component_infos[cY].cid)
error("Planar jpeg first SOS cid does not match Y cid (%u:%u)\n",
priv->current_cid, priv->component_infos[cY].cid);
#endif
resync(priv);
for (y = 0; y < priv->height / 8; y++) {
for (x = 0; x < priv->width / 8; x++) {
process_Huffman_data_unit(priv, cY);
IDCT(&priv->component_infos[cY], y_buf, priv->width);
y_buf += 8;
}
y_buf += 7 * priv->width;
}
priv->stream -= (priv->nbits_in_reservoir/8);
resync(priv);
if (find_next_sos_marker(priv) < 0)
return -1;
if (parse_SOS(priv, priv->stream) < 0)
return -1;
#if SANITY_CHECK
if (priv->current_cid != priv->component_infos[cCb].cid)
error("Planar jpeg second SOS cid does not match Cn cid (%u:%u)\n",
priv->current_cid, priv->component_infos[cCb].cid);
#endif
for (y = 0; y < priv->height / 16; y++) {
for (x = 0; x < priv->width / 16; x++) {
process_Huffman_data_unit(priv, cCb);
IDCT(&priv->component_infos[cCb], u_buf, priv->width / 2);
u_buf += 8;
}
u_buf += 7 * (priv->width / 2);
}
priv->stream -= (priv->nbits_in_reservoir / 8);
resync(priv);
if (find_next_sos_marker(priv) < 0)
return -1;
if (parse_SOS(priv, priv->stream) < 0)
return -1;
#if SANITY_CHECK
if (priv->current_cid != priv->component_infos[cCr].cid)
error("Planar jpeg third SOS cid does not match Cr cid (%u:%u)\n",
priv->current_cid, priv->component_infos[cCr].cid);
#endif
for (y = 0; y < priv->height / 16; y++) {
for (x = 0; x < priv->width / 16; x++) {
process_Huffman_data_unit(priv, cCr);
IDCT(&priv->component_infos[cCr], v_buf, priv->width / 2);
v_buf += 8;
}
v_buf += 7 * (priv->width / 2);
}
#define SCALEBITS 10
#define ONE_HALF (1UL << (SCALEBITS - 1))
#define FIX(x) ((int)((x) * (1UL << SCALEBITS) + 0.5))
switch (pixfmt) {
case TINYJPEG_FMT_RGB24:
y_buf = priv->tmp_buf[cY];
u_buf = priv->tmp_buf[cCb];
v_buf = priv->tmp_buf[cCr];
p = priv->components[0];
p2 = priv->components[0] + priv->width * 3;
for (y = 0; y < priv->height / 2; y++) {
for (x = 0; x < priv->width / 2; x++) {
int l, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *u_buf++ - 128;
cr = *v_buf++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
l = (*y_buf) << SCALEBITS;
r = (l + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (l + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (l + add_b) >> SCALEBITS;
*p++ = clamp(b);
l = (y_buf[priv->width]) << SCALEBITS;
r = (l + add_r) >> SCALEBITS;
*p2++ = clamp(r);
g = (l + add_g) >> SCALEBITS;
*p2++ = clamp(g);
b = (l + add_b) >> SCALEBITS;
*p2++ = clamp(b);
y_buf++;
l = (*y_buf) << SCALEBITS;
r = (l + add_r) >> SCALEBITS;
*p++ = clamp(r);
g = (l + add_g) >> SCALEBITS;
*p++ = clamp(g);
b = (l + add_b) >> SCALEBITS;
*p++ = clamp(b);
l = (y_buf[priv->width]) << SCALEBITS;
r = (l + add_r) >> SCALEBITS;
*p2++ = clamp(r);
g = (l + add_g) >> SCALEBITS;
*p2++ = clamp(g);
b = (l + add_b) >> SCALEBITS;
*p2++ = clamp(b);
y_buf++;
}
y_buf += priv->width;
p += priv->width * 3;
p2 += priv->width * 3;
}
break;
case TINYJPEG_FMT_BGR24:
y_buf = priv->tmp_buf[cY];
u_buf = priv->tmp_buf[cCb];
v_buf = priv->tmp_buf[cCr];
p = priv->components[0];
p2 = priv->components[0] + priv->width * 3;
for (y = 0; y < priv->height / 2; y++) {
for (x = 0; x < priv->width / 2; x++) {
int l, cb, cr;
int add_r, add_g, add_b;
int r, g , b;
cb = *u_buf++ - 128;
cr = *v_buf++ - 128;
add_r = FIX(1.40200) * cr + ONE_HALF;
add_g = -FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
add_b = FIX(1.77200) * cb + ONE_HALF;
l = (*y_buf) << SCALEBITS;
b = (l + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (l + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (l + add_r) >> SCALEBITS;
*p++ = clamp(r);
l = (y_buf[priv->width]) << SCALEBITS;
b = (l + add_b) >> SCALEBITS;
*p2++ = clamp(b);
g = (l + add_g) >> SCALEBITS;
*p2++ = clamp(g);
r = (l + add_r) >> SCALEBITS;
*p2++ = clamp(r);
y_buf++;
l = (*y_buf) << SCALEBITS;
b = (l + add_b) >> SCALEBITS;
*p++ = clamp(b);
g = (l + add_g) >> SCALEBITS;
*p++ = clamp(g);
r = (l + add_r) >> SCALEBITS;
*p++ = clamp(r);
l = (y_buf[priv->width]) << SCALEBITS;
b = (l + add_b) >> SCALEBITS;
*p2++ = clamp(b);
g = (l + add_g) >> SCALEBITS;
*p2++ = clamp(g);
r = (l + add_r) >> SCALEBITS;
*p2++ = clamp(r);
y_buf++;
}
y_buf += priv->width;
p += priv->width * 3;
p2 += priv->width * 3;
}
break;
}
#undef SCALEBITS
#undef ONE_HALF
#undef FIX
return 0;
}
const char *tinyjpeg_get_errorstring(struct jdec_private *priv)
{
return priv->error_string;
}
void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height)
{
*width = priv->width;
*height = priv->height;
}
int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components)
{
int i;
for (i = 0; i < COMPONENTS && priv->components[i]; i++)
components[i] = priv->components[i];
return 0;
}
int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents)
{
unsigned int i;
if (ncomponents > COMPONENTS)
ncomponents = COMPONENTS;
for (i = 0; i < ncomponents; i++)
priv->components[i] = components[i];
return 0;
}
int tinyjpeg_set_flags(struct jdec_private *priv, int flags)
{
int oldflags = priv->flags;
priv->flags = flags;
return oldflags;
}