Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__jpeg_decoder__Proj
changeset 0:a8af8b3fc99d
initial commit
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 05 Jul 2012 11:35:03 +0200 |
| parents | |
| children | 7e13c9ecc89c |
| files | .hgeol .hgignore VSs_tinyjpeg/jidctflt.c VSs_tinyjpeg/loadjpeg.c VSs_tinyjpeg/tinyjpeg-internal.h VSs_tinyjpeg/tinyjpeg-parse.c VSs_tinyjpeg/tinyjpeg.c VSs_tinyjpeg/tinyjpeg.h __brch__default test_images/scc_markers.jpg test_images/scc_markers2.jpg test_images/scc_markers2_lowq.jpg test_images/scc_markers_lowq.jpg test_images/scc_nomarkers.jpg test_images/scc_nomarkers_lowq.jpg |
| diffstat | 15 files changed, 1867 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgeol Thu Jul 05 11:35:03 2012 +0200 1.3 @@ -0,0 +1,14 @@ 1.4 + 1.5 +[patterns] 1.6 +**.py = native 1.7 +**.txt = native 1.8 +**.c = native 1.9 +**.h = native 1.10 +**.cpp = native 1.11 +**.java = native 1.12 +**.class = bin 1.13 +**.jar = bin 1.14 +**.sh = native 1.15 +**.pl = native 1.16 +**.jpg = bin 1.17 +**.gif = bin
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/.hgignore Thu Jul 05 11:35:03 2012 +0200 2.3 @@ -0,0 +1,12 @@ 2.4 +nbproject 2.5 +Makefile 2.6 +build 2.7 +dist 2.8 +src/Default 2.9 +src/.settings 2.10 +src/.cproject 2.11 +src/.project 2.12 +.dep.inc 2.13 +glob:.cproject 2.14 +glob:.project 2.15 +glob:Debug
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/VSs_tinyjpeg/jidctflt.c Thu Jul 05 11:35:03 2012 +0200 3.3 @@ -0,0 +1,265 @@ 3.4 +/* 3.5 + * jidctflt.c 3.6 + * 3.7 + * Copyright (C) 1994-1998, Thomas G. Lane. 3.8 + * This file is part of the Independent JPEG Group's software. 3.9 + * 3.10 + * The authors make NO WARRANTY or representation, either express or implied, 3.11 + * with respect to this software, its quality, accuracy, merchantability, or 3.12 + * fitness for a particular purpose. This software is provided "AS IS", and you, 3.13 + * its user, assume the entire risk as to its quality and accuracy. 3.14 + * 3.15 + * This software is copyright (C) 1991-1998, Thomas G. Lane. 3.16 + * All Rights Reserved except as specified below. 3.17 + * 3.18 + * Permission is hereby granted to use, copy, modify, and distribute this 3.19 + * software (or portions thereof) for any purpose, without fee, subject to these 3.20 + * conditions: 3.21 + * (1) If any part of the source code for this software is distributed, then this 3.22 + * README file must be included, with this copyright and no-warranty notice 3.23 + * unaltered; and any additions, deletions, or changes to the original files 3.24 + * must be clearly indicated in accompanying documentation. 3.25 + * (2) If only executable code is distributed, then the accompanying 3.26 + * documentation must state that "this software is based in part on the work of 3.27 + * the Independent JPEG Group". 3.28 + * (3) Permission for use of this software is granted only if the user accepts 3.29 + * full responsibility for any undesirable consequences; the authors accept 3.30 + * NO LIABILITY for damages of any kind. 3.31 + * 3.32 + * These conditions apply to any software derived from or based on the IJG code, 3.33 + * not just to the unmodified library. If you use our work, you ought to 3.34 + * acknowledge us. 3.35 + * 3.36 + * Permission is NOT granted for the use of any IJG author's name or company name 3.37 + * in advertising or publicity relating to this software or products derived from 3.38 + * it. This software may be referred to only as "the Independent JPEG Group's 3.39 + * software". 3.40 + * 3.41 + * We specifically permit and encourage the use of this software as the basis of 3.42 + * commercial products, provided that all warranty or liability claims are 3.43 + * assumed by the product vendor. 3.44 + * 3.45 + * 3.46 + * This file contains a floating-point implementation of the 3.47 + * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine 3.48 + * must also perform dequantization of the input coefficients. 3.49 + * 3.50 + * This implementation should be more accurate than either of the integer 3.51 + * IDCT implementations. However, it may not give the same results on all 3.52 + * machines because of differences in roundoff behavior. Speed will depend 3.53 + * on the hardware's floating point capacity. 3.54 + * 3.55 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT 3.56 + * on each row (or vice versa, but it's more convenient to emit a row at 3.57 + * a time). Direct algorithms are also available, but they are much more 3.58 + * complex and seem not to be any faster when reduced to code. 3.59 + * 3.60 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for 3.61 + * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in 3.62 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell 3.63 + * JPEG textbook (see REFERENCES section in file README). The following code 3.64 + * is based directly on figure 4-8 in P&M. 3.65 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is 3.66 + * possible to arrange the computation so that many of the multiplies are 3.67 + * simple scalings of the final outputs. These multiplies can then be 3.68 + * folded into the multiplications or divisions by the JPEG quantization 3.69 + * table entries. The AA&N method leaves only 5 multiplies and 29 adds 3.70 + * to be done in the DCT itself. 3.71 + * The primary disadvantage of this method is that with a fixed-point 3.72 + * implementation, accuracy is lost due to imprecise representation of the 3.73 + * scaled quantization values. However, that problem does not arise if 3.74 + * we use floating point arithmetic. 3.75 + */ 3.76 + 3.77 +#include <stdint.h> 3.78 +#include "tinyjpeg-internal.h" 3.79 + 3.80 +#define FAST_FLOAT float 3.81 +#define DCTSIZE 8 3.82 +#define DCTSIZE2 (DCTSIZE*DCTSIZE) 3.83 + 3.84 +#define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) 3.85 + 3.86 +static inline unsigned char descale_and_clamp(int x, int shift) 3.87 +{ 3.88 + x += (1UL<<(shift-1)); 3.89 + if (x<0) 3.90 + x = (x >> shift) | ((~(0UL)) << (32-(shift))); 3.91 + else 3.92 + x >>= shift; 3.93 + x += 128; 3.94 + if (x>255) 3.95 + return 255; 3.96 + else if (x<0) 3.97 + return 0; 3.98 + else 3.99 + return x; 3.100 +} 3.101 + 3.102 +/* 3.103 + * Perform dequantization and inverse DCT on one block of coefficients. 3.104 + */ 3.105 +void tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride) 3.106 +{ 3.107 + FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; 3.108 + FAST_FLOAT tmp10, tmp11, tmp12, tmp13; 3.109 + FAST_FLOAT z5, z10, z11, z12, z13; 3.110 + int16_t *inptr; 3.111 + FAST_FLOAT *quantptr; 3.112 + FAST_FLOAT *wsptr; 3.113 + uint8_t *outptr; 3.114 + int ctr; 3.115 + FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ 3.116 + 3.117 + /* Pass 1: process columns from input, store into work array. */ 3.118 + 3.119 + inptr = compptr->DCT; 3.120 + quantptr = compptr->Q_table; 3.121 + wsptr = workspace; 3.122 + for (ctr = DCTSIZE; ctr > 0; ctr--) { 3.123 + /* Due to quantization, we will usually find that many of the input 3.124 + * coefficients are zero, especially the AC terms. We can exploit this 3.125 + * by short-circuiting the IDCT calculation for any column in which all 3.126 + * the AC terms are zero. In that case each output is equal to the 3.127 + * DC coefficient (with scale factor as needed). 3.128 + * With typical images and quantization tables, half or more of the 3.129 + * column DCT calculations can be simplified this way. 3.130 + */ 3.131 + 3.132 + if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && 3.133 + inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && 3.134 + inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && 3.135 + inptr[DCTSIZE*7] == 0) { 3.136 + /* AC terms all zero */ 3.137 + FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 3.138 + 3.139 + wsptr[DCTSIZE*0] = dcval; 3.140 + wsptr[DCTSIZE*1] = dcval; 3.141 + wsptr[DCTSIZE*2] = dcval; 3.142 + wsptr[DCTSIZE*3] = dcval; 3.143 + wsptr[DCTSIZE*4] = dcval; 3.144 + wsptr[DCTSIZE*5] = dcval; 3.145 + wsptr[DCTSIZE*6] = dcval; 3.146 + wsptr[DCTSIZE*7] = dcval; 3.147 + 3.148 + inptr++; /* advance pointers to next column */ 3.149 + quantptr++; 3.150 + wsptr++; 3.151 + continue; 3.152 + } 3.153 + 3.154 + /* Even part */ 3.155 + 3.156 + tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 3.157 + tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); 3.158 + tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); 3.159 + tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); 3.160 + 3.161 + tmp10 = tmp0 + tmp2; /* phase 3 */ 3.162 + tmp11 = tmp0 - tmp2; 3.163 + 3.164 + tmp13 = tmp1 + tmp3; /* phases 5-3 */ 3.165 + tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ 3.166 + 3.167 + tmp0 = tmp10 + tmp13; /* phase 2 */ 3.168 + tmp3 = tmp10 - tmp13; 3.169 + tmp1 = tmp11 + tmp12; 3.170 + tmp2 = tmp11 - tmp12; 3.171 + 3.172 + /* Odd part */ 3.173 + 3.174 + tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); 3.175 + tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); 3.176 + tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); 3.177 + tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); 3.178 + 3.179 + z13 = tmp6 + tmp5; /* phase 6 */ 3.180 + z10 = tmp6 - tmp5; 3.181 + z11 = tmp4 + tmp7; 3.182 + z12 = tmp4 - tmp7; 3.183 + 3.184 + tmp7 = z11 + z13; /* phase 5 */ 3.185 + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ 3.186 + 3.187 + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ 3.188 + tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ 3.189 + tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ 3.190 + 3.191 + tmp6 = tmp12 - tmp7; /* phase 2 */ 3.192 + tmp5 = tmp11 - tmp6; 3.193 + tmp4 = tmp10 + tmp5; 3.194 + 3.195 + wsptr[DCTSIZE*0] = tmp0 + tmp7; 3.196 + wsptr[DCTSIZE*7] = tmp0 - tmp7; 3.197 + wsptr[DCTSIZE*1] = tmp1 + tmp6; 3.198 + wsptr[DCTSIZE*6] = tmp1 - tmp6; 3.199 + wsptr[DCTSIZE*2] = tmp2 + tmp5; 3.200 + wsptr[DCTSIZE*5] = tmp2 - tmp5; 3.201 + wsptr[DCTSIZE*4] = tmp3 + tmp4; 3.202 + wsptr[DCTSIZE*3] = tmp3 - tmp4; 3.203 + 3.204 + inptr++; /* advance pointers to next column */ 3.205 + quantptr++; 3.206 + wsptr++; 3.207 + } 3.208 + 3.209 + /* Pass 2: process rows from work array, store into output array. */ 3.210 + /* Note that we must descale the results by a factor of 8 == 2**3. */ 3.211 + 3.212 + wsptr = workspace; 3.213 + outptr = output_buf; 3.214 + for (ctr = 0; ctr < DCTSIZE; ctr++) { 3.215 + /* Rows of zeroes can be exploited in the same way as we did with columns. 3.216 + * However, the column calculation has created many nonzero AC terms, so 3.217 + * the simplification applies less often (typically 5% to 10% of the time). 3.218 + * And testing floats for zero is relatively expensive, so we don't bother. 3.219 + */ 3.220 + 3.221 + /* Even part */ 3.222 + 3.223 + tmp10 = wsptr[0] + wsptr[4]; 3.224 + tmp11 = wsptr[0] - wsptr[4]; 3.225 + 3.226 + tmp13 = wsptr[2] + wsptr[6]; 3.227 + tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; 3.228 + 3.229 + tmp0 = tmp10 + tmp13; 3.230 + tmp3 = tmp10 - tmp13; 3.231 + tmp1 = tmp11 + tmp12; 3.232 + tmp2 = tmp11 - tmp12; 3.233 + 3.234 + /* Odd part */ 3.235 + 3.236 + z13 = wsptr[5] + wsptr[3]; 3.237 + z10 = wsptr[5] - wsptr[3]; 3.238 + z11 = wsptr[1] + wsptr[7]; 3.239 + z12 = wsptr[1] - wsptr[7]; 3.240 + 3.241 + tmp7 = z11 + z13; 3.242 + tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); 3.243 + 3.244 + z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ 3.245 + tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ 3.246 + tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ 3.247 + 3.248 + tmp6 = tmp12 - tmp7; 3.249 + tmp5 = tmp11 - tmp6; 3.250 + tmp4 = tmp10 + tmp5; 3.251 + 3.252 + /* Final output stage: scale down by a factor of 8 and range-limit */ 3.253 + 3.254 + outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3); 3.255 + outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3); 3.256 + outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3); 3.257 + outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3); 3.258 + outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3); 3.259 + outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3); 3.260 + outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3); 3.261 + outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3); 3.262 + 3.263 + 3.264 + wsptr += DCTSIZE; /* advance pointer to next row */ 3.265 + outptr += stride; 3.266 + } 3.267 +} 3.268 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/VSs_tinyjpeg/loadjpeg.c Thu Jul 05 11:35:03 2012 +0200 4.3 @@ -0,0 +1,251 @@ 4.4 +/* 4.5 + * Small jpeg decoder library - testing application 4.6 + * 4.7 + * Copyright (c) 2006, Luc Saillard <luc@saillard.org> 4.8 + * All rights reserved. 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * - Redistributions of source code must retain the above copyright notice, 4.13 + * this list of conditions and the following disclaimer. 4.14 + * 4.15 + * - Redistributions in binary form must reproduce the above copyright notice, 4.16 + * this list of conditions and the following disclaimer in the documentation 4.17 + * and/or other materials provided with the distribution. 4.18 + * 4.19 + * - Neither the name of the author nor the names of its contributors may be 4.20 + * used to endorse or promote products derived from this software without 4.21 + * specific prior written permission. 4.22 + * 4.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 4.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.33 + * POSSIBILITY OF SUCH DAMAGE. 4.34 + * 4.35 + */ 4.36 + 4.37 + 4.38 +#include <stdio.h> 4.39 +#include <stdint.h> 4.40 +#include <stdlib.h> 4.41 +#include <string.h> 4.42 +#include <sys/time.h> 4.43 + 4.44 +#include "tinyjpeg.h" 4.45 +#include "VSs_impl/VSs.h" 4.46 + 4.47 +typedef struct timeval timer; 4.48 +#define TIME(x) gettimeofday(&x, NULL); 4.49 + 4.50 +long timevaldiff(timer *start, timer *finish); 4.51 + 4.52 +static void exitmessage(const char *message) 4.53 +{ 4.54 + printf("%s\n", message); 4.55 + exit(0); 4.56 +} 4.57 + 4.58 +static int filesize(FILE *fp) 4.59 +{ 4.60 + long pos; 4.61 + fseek(fp, 0, SEEK_END); 4.62 + pos = ftell(fp); 4.63 + fseek(fp, 0, SEEK_SET); 4.64 + return pos; 4.65 +} 4.66 + 4.67 +/** 4.68 + * Save a buffer in 24bits Targa format 4.69 + * (BGR byte order) 4.70 + */ 4.71 +FILE* write_tga_header(const char* filename, int width, int height) { 4.72 + unsigned char targaheader[18]; 4.73 + FILE *F; 4.74 + char temp[1024]; 4.75 + 4.76 + snprintf(temp, sizeof(temp), "%s", filename); 4.77 + 4.78 + memset(targaheader,0,sizeof(targaheader)); 4.79 + 4.80 + targaheader[12] = (unsigned char) (width & 0xFF); 4.81 + targaheader[13] = (unsigned char) (width >> 8); 4.82 + targaheader[14] = (unsigned char) (height & 0xFF); 4.83 + targaheader[15] = (unsigned char) (height >> 8); 4.84 + targaheader[17] = 0x20; /* Top-down, non-interlaced */ 4.85 + targaheader[2] = 2; /* image type = uncompressed RGB */ 4.86 + targaheader[16] = 24; 4.87 + 4.88 + 4.89 + F = fopen(temp, "wb"); 4.90 + fwrite(targaheader, sizeof(targaheader), 1, F); 4.91 + return F; 4.92 +} 4.93 + 4.94 +//todo 4.95 +//#pragma omp task input(*rgb_data) output(*d) inout(*d) 4.96 +void write_tga_task(FILE* fp, int bufferlen, unsigned char* rgb_data, char* d) { 4.97 + 4.98 + // To disable ompss warnings 4.99 + d = d; 4.100 + unsigned char *data = rgb_data + bufferlen - RGB_DEPTH; 4.101 + do 4.102 + { 4.103 + unsigned char c = data[0]; 4.104 + data[0] = data[2]; 4.105 + data[2] = c; 4.106 + data-=RGB_DEPTH; 4.107 + } while (data >= rgb_data); 4.108 + 4.109 + fwrite(rgb_data, 1, bufferlen, fp); 4.110 +} 4.111 + 4.112 + 4.113 +int32 tinyjpegArgTypes[2] = {IN, OUT}; 4.114 +int32 tinyjpegArgSizes[2] = {sizeof(struct jdec_private), sizeof(uint8_t)}; 4.115 + 4.116 +/** 4.117 + * Load one jpeg image, and decompress it, and save the result. 4.118 + */ 4.119 +int convert_one_image(const char *infilename, const char *outfilename) 4.120 +{ 4.121 + FILE *fp; 4.122 + unsigned int length_of_file; 4.123 + unsigned int width, height; 4.124 + unsigned char *buf; 4.125 + struct jdec_private *jdec; //for parsing header 4.126 + struct jdec_private **jdec_task; //for decoding mcus 4.127 + uint8_t *rgb_data; 4.128 + int i; 4.129 + int ntasks; 4.130 + 4.131 + /* Load the Jpeg into memory */ 4.132 + fp = fopen(infilename, "rb"); 4.133 + if (fp == NULL) 4.134 + perror("Cannot open image");//exitmessage("Cannot open filename\n"); 4.135 + length_of_file = filesize(fp); 4.136 + buf = (unsigned char *)malloc(length_of_file + 4); 4.137 + if (buf == NULL) 4.138 + exitmessage("Not enough memory for loading file\n"); 4.139 + fread(buf, length_of_file, 1, fp); 4.140 + fclose(fp); 4.141 + 4.142 + /* Decompress it */ 4.143 + jdec = tinyjpeg_init(); 4.144 + if (jdec == NULL) 4.145 + exitmessage("Not enough memory to alloc the structure need for decompressing\n"); 4.146 + 4.147 + if (tinyjpeg_parse_header(jdec, buf, length_of_file)<0) 4.148 + exitmessage(tinyjpeg_get_errorstring()); 4.149 + 4.150 + /* Get the size of the image */ 4.151 + tinyjpeg_get_size(jdec, &width, &height); 4.152 + 4.153 + // RGB stuff 4.154 + rgb_data = (uint8_t *)malloc(width * height * RGB_DEPTH); 4.155 + jdec->components[0] = rgb_data; 4.156 + 4.157 + // this jpeg decoder only supports full MCUs for simplicity 4.158 + ntasks = (jdec->mcus_in_width * jdec->mcus_in_height)/ jdec->restart_interval; 4.159 + jdec_task = (struct jdec_private **) malloc ( ntasks * sizeof(struct jdec_private*)); 4.160 + 4.161 + 4.162 + //VSs setup 4.163 + tinyjpegTaskType = VMS_App__malloc( sizeof(VSsTaskType) ); 4.164 + tinyjpegTaskType->fn = &tinyjpeg_decode_task; 4.165 + tinyjpegTaskType->numCtldArgs = 2; 4.166 + tinyjpegTaskType->numTotalArgs = 2; 4.167 + tinyjpegTaskType->sizeOfArgs = sizeof(tinyjpeg_decode_task_args); 4.168 + tinyjpegTaskType->argTypes = tinyjpegArgTypes; 4.169 + tinyjpegTaskType->argSizes = tinyjpegArgSizes; 4.170 + 4.171 + tinyjpeg_decode_task_args args; 4.172 + 4.173 + fp = write_tga_header(outfilename, width, height); 4.174 + printf("Decoding JPEG image...\n"); 4.175 + for (i=0; i<ntasks; i++){ 4.176 + jdec_task[i] = create_jdec_priv_task(jdec, i); 4.177 + 4.178 + args.priv = jdec_task[i]; 4.179 + args.context = rgb_data+i*width*RGB_DEPTH*MCU_Y_STRIDE; 4.180 + VSs__submit_task(tinyjpegTaskType, &args, master); 4.181 + 4.182 + } 4.183 + 4.184 + char dummy; 4.185 + for(i=0; i<ntasks;i++) { 4.186 + write_tga_task(fp, width*RGB_DEPTH*MCU_Y_STRIDE, rgb_data+i*RGB_DEPTH*width*MCU_Y_STRIDE, &dummy); 4.187 + } 4.188 + 4.189 + //VSs__wait_for_all_tasks_to_complete(); 4.190 + //#pragma omp barrier 4.191 + 4.192 + tinyjpeg_free(jdec); 4.193 + for(i=0; i < ntasks; i++) { 4.194 + tinyjpeg_free(jdec_task[i]); 4.195 + } 4.196 + fclose(fp); 4.197 + free(buf); 4.198 + free(rgb_data); 4.199 + free(jdec_task); 4.200 + return 0; 4.201 +} 4.202 + 4.203 +/* 4.204 + * Usage information. 4.205 + */ 4.206 +static void usage(void) 4.207 +{ 4.208 + fprintf(stderr, "Usage: loadjpeg <input_filename.jpeg> <output_filename>\n"); 4.209 + exit(1); 4.210 +} 4.211 + 4.212 +/* 4.213 + * Calculates the time difference between start and finish in msecs. 4.214 + */ 4.215 +long timevaldiff(timer *start, timer *finish){ 4.216 + long msec; 4.217 + msec = (finish->tv_sec - start->tv_sec)*1000; 4.218 + msec += (finish->tv_usec - start->tv_usec)/1000; 4.219 + return msec; 4.220 +} 4.221 + 4.222 + 4.223 + char *output_filename, *input_filename; 4.224 +/** 4.225 + * Benchmark MAIN 4.226 + */ 4.227 +int main(int argc, char *argv[]) 4.228 +{ 4.229 + 4.230 + if (argc < 3) 4.231 + usage(); 4.232 + 4.233 + 4.234 + input_filename = argv[1]; 4.235 + output_filename = argv[2]; 4.236 + 4.237 + VSs__create_seed_slave_and_do_work( &convert_one_image_wrapper, 4.238 + NULL ); 4.239 + 4.240 + 4.241 + 4.242 + return 0; 4.243 +} 4.244 + 4.245 + 4.246 + 4.247 +void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ){ 4.248 + master = animSlv; 4.249 + 4.250 + printf("Input file: %s\nOutput file: %s\n",input_filename,output_filename); 4.251 + 4.252 + convert_one_image(input_filename, output_filename); 4.253 +} 4.254 +
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/VSs_tinyjpeg/tinyjpeg-internal.h Thu Jul 05 11:35:03 2012 +0200 5.3 @@ -0,0 +1,159 @@ 5.4 +/* 5.5 + * Small jpeg decoder library (Internal header) 5.6 + * 5.7 + * Copyright (c) 2006, Luc Saillard <luc@saillard.org> 5.8 + * All rights reserved. 5.9 + * Redistribution and use in source and binary forms, with or without 5.10 + * modification, are permitted provided that the following conditions are met: 5.11 + * 5.12 + * - Redistributions of source code must retain the above copyright notice, 5.13 + * this list of conditions and the following disclaimer. 5.14 + * 5.15 + * - Redistributions in binary form must reproduce the above copyright notice, 5.16 + * this list of conditions and the following disclaimer in the documentation 5.17 + * and/or other materials provided with the distribution. 5.18 + * 5.19 + * - Neither the name of the author nor the names of its contributors may be 5.20 + * used to endorse or promote products derived from this software without 5.21 + * specific prior written permission. 5.22 + * 5.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 5.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5.33 + * POSSIBILITY OF SUCH DAMAGE. 5.34 + * 5.35 + */ 5.36 + 5.37 +#ifndef __TINYJPEG_INTERNAL_H_ 5.38 +#define __TINYJPEG_INTERNAL_H_ 5.39 + 5.40 +#define SANITY_CHECK 1 5.41 + 5.42 +struct jdec_private; 5.43 + 5.44 +#define HUFFMAN_BITS_SIZE 256 5.45 +#define HUFFMAN_HASH_NBITS 9 5.46 +#define HUFFMAN_HASH_SIZE (1UL<<HUFFMAN_HASH_NBITS) 5.47 +#define HUFFMAN_HASH_MASK (HUFFMAN_HASH_SIZE-1) 5.48 + 5.49 +#define HUFFMAN_TABLES 4 5.50 +#define COMPONENTS 3 5.51 + 5.52 +#define cY 0 5.53 +#define cCb 1 5.54 +#define cCr 2 5.55 + 5.56 +#define BLACK_Y 0 5.57 +#define BLACK_U 127 5.58 +#define BLACK_V 127 5.59 + 5.60 +#if DEBUG 5.61 +#define trace(fmt, args...) do { \ 5.62 + fprintf(stderr, fmt, ## args); \ 5.63 + fflush(stderr); \ 5.64 +} while(0) 5.65 +#else 5.66 +#define trace(fmt, args...) do { } while (0) 5.67 +#endif 5.68 +#define error(fmt, args...) do { \ 5.69 + snprintf(error_string, sizeof(error_string), fmt, ## args); \ 5.70 + return -1; \ 5.71 +} while(0) 5.72 + 5.73 +#define be16_to_cpu(x) (((x)[0]<<8)|(x)[1]) 5.74 + 5.75 +enum std_markers { 5.76 + DQT = 0xDB, /* Define Quantization Table */ 5.77 + SOF = 0xC0, /* Start of Frame (size information) */ 5.78 + DHT = 0xC4, /* Huffman Table */ 5.79 + SOI = 0xD8, /* Start of Image */ 5.80 + SOS = 0xDA, /* Start of Scan */ 5.81 + RST = 0xD0, /* Reset Marker d0 -> .. */ 5.82 + RST7 = 0xD7, /* Reset Marker .. -> d7 */ 5.83 + EOI = 0xD9, /* End of Image */ 5.84 + DRI = 0xDD, /* Define Restart Interval */ 5.85 + APP0 = 0xE0, 5.86 +}; 5.87 + 5.88 +struct huffman_table 5.89 +{ 5.90 + /* Fast look up table, using HUFFMAN_HASH_NBITS bits we can have directly the symbol, 5.91 + * if the symbol is <0, then we need to look into the tree table */ 5.92 + short int lookup[HUFFMAN_HASH_SIZE]; 5.93 + /* code size: give the number of bits of a symbol is encoded */ 5.94 + unsigned char code_size[HUFFMAN_HASH_SIZE]; 5.95 + /* some place to store value that is not encoded in the lookup table */ 5.96 + uint16_t slowtable[16-HUFFMAN_HASH_NBITS][256]; 5.97 +}; 5.98 + 5.99 +struct component 5.100 +{ 5.101 + unsigned int Hfactor; 5.102 + unsigned int Vfactor; 5.103 + float *Q_table; /* Pointer to the quantisation table to use */ 5.104 + struct huffman_table *AC_table; 5.105 + struct huffman_table *DC_table; 5.106 + short int previous_DC; /* Previous DC coefficient */ 5.107 + short int DCT[64]; /* DCT coef */ 5.108 +#if SANITY_CHECK 5.109 + unsigned int cid; 5.110 +#endif 5.111 +}; 5.112 + 5.113 +typedef int (*decode_MCU_fct) (struct jdec_private *priv); 5.114 +typedef void (*convert_colorspace_fct) (struct jdec_private *priv); 5.115 + 5.116 +struct jdec_private 5.117 +{ 5.118 + /* Public variables */ 5.119 + uint8_t *components[COMPONENTS]; 5.120 + unsigned int width, height; /* Size of the image */ 5.121 + unsigned int mcus_in_width, mcus_in_height; 5.122 + unsigned int mcus_posx, mcus_posy; 5.123 + unsigned int flags; 5.124 + 5.125 + /* Private variables */ 5.126 + const unsigned char *stream_begin, *stream_end; 5.127 + unsigned int stream_length; 5.128 + 5.129 + const unsigned char *stream; /* Pointer to the current stream */ 5.130 + unsigned int reservoir, nbits_in_reservoir; 5.131 + 5.132 + struct component component_infos[COMPONENTS]; 5.133 + float Q_tables[COMPONENTS][64]; /* quantization tables */ 5.134 + struct huffman_table HTDC[HUFFMAN_TABLES]; /* DC huffman tables */ 5.135 + struct huffman_table HTAC[HUFFMAN_TABLES]; /* AC huffman tables */ 5.136 + int default_huffman_table_initialized; 5.137 + unsigned int restart_interval; 5.138 +// int restarts_to_go; /* MCUs left in this restart interval */ 5.139 + int last_rst_marker_seen; /* Rst marker is incremented each time */ 5.140 + 5.141 + /* Temp space used after the IDCT to store each components */ 5.142 + uint8_t Y[64*4], Cr[64], Cb[64]; 5.143 + 5.144 + /* Internal Pointer use for colorspace conversion, do not modify it !!! */ 5.145 + uint8_t *plane; 5.146 + 5.147 +}; 5.148 + 5.149 +#if defined(__GNUC__) && (__GNUC__ > 3) && defined(__OPTIMIZE__) 5.150 +#define __likely(x) __builtin_expect(!!(x), 1) 5.151 +#define __unlikely(x) __builtin_expect(!!(x), 0) 5.152 +#else 5.153 +#define __likely(x) (x) 5.154 +#define __unlikely(x) (x) 5.155 +#endif 5.156 + 5.157 +#define IDCT tinyjpeg_idct_float 5.158 +void tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride); 5.159 +int parse_JFIF(struct jdec_private *priv, const unsigned char *stream); 5.160 + 5.161 +#endif 5.162 +
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/VSs_tinyjpeg/tinyjpeg-parse.c Thu Jul 05 11:35:03 2012 +0200 6.3 @@ -0,0 +1,511 @@ 6.4 +#include <stdio.h> 6.5 +#include <stdlib.h> 6.6 +#include <string.h> 6.7 +#include <stdint.h> 6.8 +#include <errno.h> 6.9 + 6.10 +#include "tinyjpeg.h" 6.11 +#include "tinyjpeg-internal.h" 6.12 + 6.13 +extern char error_string[256]; 6.14 + 6.15 +static const unsigned char zigzag[64] = 6.16 +{ 6.17 + 0, 1, 5, 6, 14, 15, 27, 28, 6.18 + 2, 4, 7, 13, 16, 26, 29, 42, 6.19 + 3, 8, 12, 17, 25, 30, 41, 43, 6.20 + 9, 11, 18, 24, 31, 40, 44, 53, 6.21 + 10, 19, 23, 32, 39, 45, 52, 54, 6.22 + 20, 22, 33, 38, 46, 51, 55, 60, 6.23 + 21, 34, 37, 47, 50, 56, 59, 61, 6.24 + 35, 36, 48, 49, 57, 58, 62, 63 6.25 +}; 6.26 + 6.27 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ 6.28 +/* IMPORTANT: these are only valid for 8-bit data precision! */ 6.29 +static const unsigned char bits_dc_luminance[17] = 6.30 +{ 6.31 + 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 6.32 +}; 6.33 +static const unsigned char val_dc_luminance[] = 6.34 +{ 6.35 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 6.36 +}; 6.37 + 6.38 +static const unsigned char bits_dc_chrominance[17] = 6.39 +{ 6.40 + 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 6.41 +}; 6.42 +static const unsigned char val_dc_chrominance[] = 6.43 +{ 6.44 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 6.45 +}; 6.46 + 6.47 +static const unsigned char bits_ac_luminance[17] = 6.48 +{ 6.49 + 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d 6.50 +}; 6.51 +static const unsigned char val_ac_luminance[] = 6.52 +{ 6.53 + 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 6.54 + 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 6.55 + 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 6.56 + 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 6.57 + 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 6.58 + 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 6.59 + 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 6.60 + 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 6.61 + 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 6.62 + 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 6.63 + 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 6.64 + 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 6.65 + 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 6.66 + 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 6.67 + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 6.68 + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 6.69 + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 6.70 + 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 6.71 + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 6.72 + 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 6.73 + 0xf9, 0xfa 6.74 +}; 6.75 + 6.76 +static const unsigned char bits_ac_chrominance[17] = 6.77 +{ 6.78 + 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 6.79 +}; 6.80 + 6.81 +static const unsigned char val_ac_chrominance[] = 6.82 +{ 6.83 + 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 6.84 + 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 6.85 + 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 6.86 + 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 6.87 + 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 6.88 + 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 6.89 + 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 6.90 + 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 6.91 + 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 6.92 + 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 6.93 + 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 6.94 + 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 6.95 + 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 6.96 + 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 6.97 + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 6.98 + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 6.99 + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 6.100 + 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 6.101 + 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 6.102 + 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 6.103 + 0xf9, 0xfa 6.104 +}; 6.105 + 6.106 +static void print_SOF(const unsigned char *stream) 6.107 +{ 6.108 + int width, height, nr_components, precision; 6.109 + 6.110 + precision = stream[2]; 6.111 + height = be16_to_cpu(stream+3); 6.112 + width = be16_to_cpu(stream+5); 6.113 + nr_components = stream[7]; 6.114 + 6.115 + trace("> SOF marker\n"); 6.116 + trace("Size:%dx%d nr_components:%d precision:%d\n", 6.117 + width, height, 6.118 + nr_components, 6.119 + precision); 6.120 +} 6.121 + 6.122 + 6.123 +/* 6.124 + * Takes two array of bits, and build the huffman table for size, and code 6.125 + * 6.126 + * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS. 6.127 + * code_size will be used to known how many bits this symbol is encoded. 6.128 + * slowtable will be used when the first lookup didn't give the result. 6.129 + */ 6.130 +static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table) 6.131 +{ 6.132 + unsigned int i, j, code, code_size, val, nbits; 6.133 + unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz; 6.134 + unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc; 6.135 + int next_free_entry; 6.136 + 6.137 + /* 6.138 + * Build a temp array 6.139 + * huffsize[X] => numbers of bits to write vals[X] 6.140 + */ 6.141 + hz = huffsize; 6.142 + for (i=1; i<=16; i++) 6.143 + { 6.144 + for (j=1; j<=bits[i]; j++) 6.145 + *hz++ = i; 6.146 + } 6.147 + *hz = 0; 6.148 + 6.149 + memset(table->lookup, 0xff, sizeof(table->lookup)); 6.150 + for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++) 6.151 + table->slowtable[i][0] = 0; 6.152 + 6.153 + /* Build a temp array 6.154 + * huffcode[X] => code used to write vals[X] 6.155 + */ 6.156 + code = 0; 6.157 + hc = huffcode; 6.158 + hz = huffsize; 6.159 + nbits = *hz; 6.160 + while (*hz) 6.161 + { 6.162 + while (*hz == nbits) 6.163 + { 6.164 + *hc++ = code++; 6.165 + hz++; 6.166 + } 6.167 + code <<= 1; 6.168 + nbits++; 6.169 + } 6.170 + 6.171 + /* 6.172 + * Build the lookup table, and the slowtable if needed. 6.173 + */ 6.174 + next_free_entry = -1; 6.175 + for (i=0; huffsize[i]; i++) 6.176 + { 6.177 + val = vals[i]; 6.178 + code = huffcode[i]; 6.179 + code_size = huffsize[i]; 6.180 + 6.181 + trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size); 6.182 + 6.183 + table->code_size[val] = code_size; 6.184 + if (code_size <= HUFFMAN_HASH_NBITS) 6.185 + { 6.186 + /* 6.187 + * Good: val can be put in the lookup table, so fill all value of this 6.188 + * column with value val 6.189 + */ 6.190 + int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size); 6.191 + code <<= HUFFMAN_HASH_NBITS - code_size; 6.192 + while ( repeat-- ) 6.193 + table->lookup[code++] = val; 6.194 + 6.195 + } 6.196 + else 6.197 + { 6.198 + /* Perhaps sorting the array will be an optimization */ 6.199 + uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1]; 6.200 + while(slowtable[0]) 6.201 + slowtable+=2; 6.202 + slowtable[0] = code; 6.203 + slowtable[1] = val; 6.204 + slowtable[2] = 0; 6.205 + } 6.206 + 6.207 + } 6.208 +} 6.209 + 6.210 +static void build_default_huffman_tables(struct jdec_private *priv) 6.211 +{ 6.212 + if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE) 6.213 + && priv->default_huffman_table_initialized) 6.214 + return; 6.215 + 6.216 + build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]); 6.217 + build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]); 6.218 + 6.219 + build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]); 6.220 + build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]); 6.221 + 6.222 + priv->default_huffman_table_initialized = 1; 6.223 +} 6.224 + 6.225 +/******************************************************************************* 6.226 + * 6.227 + * JPEG/JFIF Parsing functions 6.228 + * 6.229 + * Note: only a small subset of the jpeg file format is supported. No markers, 6.230 + * nor progressive stream is supported. 6.231 + * 6.232 + ******************************************************************************/ 6.233 + 6.234 +static void build_quantization_table(float *qtable, const unsigned char *ref_table) 6.235 +{ 6.236 + /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct. 6.237 + * For float AA&N IDCT method, divisors are equal to quantization 6.238 + * coefficients scaled by scalefactor[row]*scalefactor[col], where 6.239 + * scalefactor[0] = 1 6.240 + * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 6.241 + * We apply a further scale factor of 8. 6.242 + * What's actually stored is 1/divisor so that the inner loop can 6.243 + * use a multiplication rather than a division. 6.244 + */ 6.245 + int i, j; 6.246 + static const double aanscalefactor[8] = { 6.247 + 1.0, 1.387039845, 1.306562965, 1.175875602, 6.248 + 1.0, 0.785694958, 0.541196100, 0.275899379 6.249 + }; 6.250 + const unsigned char *zz = zigzag; 6.251 + 6.252 + for (i=0; i<8; i++) { 6.253 + for (j=0; j<8; j++) { 6.254 + *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j]; 6.255 + } 6.256 + } 6.257 + 6.258 +} 6.259 + 6.260 +static int parse_DQT(struct jdec_private *priv, const unsigned char *stream) 6.261 +{ 6.262 + int qi; 6.263 + float *table; 6.264 + const unsigned char *dqt_block_end; 6.265 + 6.266 + trace("> DQT marker\n"); 6.267 + dqt_block_end = stream + be16_to_cpu(stream); 6.268 + stream += 2; /* Skip length */ 6.269 + 6.270 + while (stream < dqt_block_end) 6.271 + { 6.272 + qi = *stream++; 6.273 +#if SANITY_CHECK 6.274 + if (qi>>4) 6.275 + error("16 bits quantization table is not supported\n"); 6.276 + if (qi>4) 6.277 + error("No more 4 quantization table is supported (got %d)\n", qi); 6.278 +#endif 6.279 + table = priv->Q_tables[qi]; 6.280 + build_quantization_table(table, stream); 6.281 + stream += 64; 6.282 + } 6.283 + trace("< DQT marker\n"); 6.284 + return 0; 6.285 +} 6.286 + 6.287 +static int parse_SOF(struct jdec_private *priv, const unsigned char *stream) 6.288 +{ 6.289 + int i, width, height, nr_components, cid, sampling_factor; 6.290 + int Q_table; 6.291 + struct component *c; 6.292 + 6.293 + trace("> SOF marker\n"); 6.294 + print_SOF(stream); 6.295 + 6.296 + height = be16_to_cpu(stream+3); 6.297 + width = be16_to_cpu(stream+5); 6.298 + nr_components = stream[7]; 6.299 +#if SANITY_CHECK 6.300 + if (stream[2] != 8) 6.301 + error("Precision other than 8 is not supported\n"); 6.302 + if (nr_components != 3) 6.303 + error("We only support YUV images\n"); 6.304 + if (height%16) 6.305 + error("Height need to be a multiple of 16 (current height is %d)\n", height); 6.306 + if (width%16) 6.307 + error("Width need to be a multiple of 16 (current Width is %d)\n", width); 6.308 +#endif 6.309 + stream += 8; 6.310 + for (i=0; i<nr_components; i++) { 6.311 + cid = *stream++; 6.312 + sampling_factor = *stream++; 6.313 + Q_table = *stream++; 6.314 + c = &priv->component_infos[i]; 6.315 +#if SANITY_CHECK 6.316 + c->cid = cid; 6.317 + if (Q_table >= COMPONENTS) 6.318 + error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1); 6.319 +#endif 6.320 + c->Vfactor = sampling_factor&0xf; 6.321 + c->Hfactor = sampling_factor>>4; 6.322 + c->Q_table = priv->Q_tables[Q_table]; 6.323 + trace("Component:%d factor:%dx%d Quantization table:%d\n", cid, c->Hfactor, c->Hfactor, Q_table ); 6.324 + 6.325 + } 6.326 + priv->width = width; 6.327 + priv->height = height; 6.328 + priv->mcus_in_width = width/16; 6.329 + priv->mcus_in_height = height/16; 6.330 + priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height; 6.331 + 6.332 + trace("< SOF marker\n"); 6.333 + 6.334 + return 0; 6.335 +} 6.336 + 6.337 +static int parse_SOS(struct jdec_private *priv, const unsigned char *stream) 6.338 +{ 6.339 + unsigned int i, cid, table; 6.340 + unsigned int nr_components = stream[2]; 6.341 + 6.342 + trace("> SOS marker\n"); 6.343 + 6.344 +#if SANITY_CHECK 6.345 + if (nr_components != 3) 6.346 + error("We only support YCbCr image\n"); 6.347 +#endif 6.348 + 6.349 + stream += 3; 6.350 + for (i=0;i<nr_components;i++) { 6.351 + cid = *stream++; 6.352 + table = *stream++; 6.353 +#if SANITY_CHECK 6.354 + if ((table&0xf)>=4) 6.355 + error("We do not support more than 2 AC Huffman table\n"); 6.356 + if ((table>>4)>=4) 6.357 + error("We do not support more than 2 DC Huffman table\n"); 6.358 + if (cid != priv->component_infos[i].cid) 6.359 + error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n", 6.360 + i, cid, i, priv->component_infos[i].cid); 6.361 + trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4); 6.362 +#endif 6.363 + priv->component_infos[i].AC_table = &priv->HTAC[table&0xf]; 6.364 + priv->component_infos[i].DC_table = &priv->HTDC[table>>4]; 6.365 + } 6.366 + priv->stream = stream+3; 6.367 + trace("< SOS marker\n"); 6.368 + return 0; 6.369 +} 6.370 + 6.371 +static int parse_DHT(struct jdec_private *priv, const unsigned char *stream) 6.372 +{ 6.373 + unsigned int count, i; 6.374 + unsigned char huff_bits[17]; 6.375 + int length, index; 6.376 + 6.377 + length = be16_to_cpu(stream) - 2; 6.378 + stream += 2; /* Skip length */ 6.379 + 6.380 + trace("> DHT marker (length=%d)\n", length); 6.381 + 6.382 + while (length>0) { 6.383 + index = *stream++; 6.384 + 6.385 + /* We need to calculate the number of bytes 'vals' will takes */ 6.386 + huff_bits[0] = 0; 6.387 + count = 0; 6.388 + for (i=1; i<17; i++) { 6.389 + huff_bits[i] = *stream++; 6.390 + count += huff_bits[i]; 6.391 + } 6.392 +#if SANITY_CHECK 6.393 + if (count >= HUFFMAN_BITS_SIZE) 6.394 + error("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE); 6.395 + if ( (index &0xf) >= HUFFMAN_TABLES) 6.396 + error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf); 6.397 + trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count); 6.398 +#endif 6.399 + 6.400 + if (index & 0xf0 ) 6.401 + build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]); 6.402 + else 6.403 + build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]); 6.404 + 6.405 + length -= 1; 6.406 + length -= 16; 6.407 + length -= count; 6.408 + stream += count; 6.409 + } 6.410 + trace("< DHT marker\n"); 6.411 + return 0; 6.412 +} 6.413 + 6.414 +static int parse_DRI(struct jdec_private *priv, const unsigned char *stream) 6.415 +{ 6.416 + unsigned int length; 6.417 + 6.418 + trace("> DRI marker\n"); 6.419 + 6.420 + length = be16_to_cpu(stream); 6.421 + 6.422 +#if SANITY_CHECK 6.423 + if (length != 4) 6.424 + error("Length of DRI marker need to be 4\n"); 6.425 +#endif 6.426 + 6.427 + priv->restart_interval = be16_to_cpu(stream+2); 6.428 + if (priv->restart_interval == 0) 6.429 + priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height; 6.430 + 6.431 +#if DEBUG 6.432 + trace("Restart interval = %d\n", priv->restart_interval); 6.433 +#endif 6.434 + 6.435 + trace("< DRI marker\n"); 6.436 + 6.437 + return 0; 6.438 +} 6.439 + 6.440 +int parse_JFIF(struct jdec_private *priv, const unsigned char *stream) 6.441 +{ 6.442 + int chuck_len; 6.443 + int marker; 6.444 + int sos_marker_found = 0; 6.445 + int dht_marker_found = 0; 6.446 + const unsigned char *next_chunck; 6.447 + 6.448 + /* Parse marker */ 6.449 + while (!sos_marker_found) 6.450 + { 6.451 + if (*stream++ != 0xff) { 6.452 + trace("Bogus jpeg format\n"); 6.453 + return -1; 6.454 + } 6.455 + /* Skip any padding ff byte (this is normal) */ 6.456 + while (*stream == 0xff) 6.457 + stream++; 6.458 + 6.459 + marker = *stream++; 6.460 + chuck_len = be16_to_cpu(stream); 6.461 + next_chunck = stream + chuck_len; 6.462 + switch (marker) 6.463 + { 6.464 + case SOF: 6.465 + if (parse_SOF(priv, stream) < 0) 6.466 + return -1; 6.467 + break; 6.468 + case DQT: 6.469 + if (parse_DQT(priv, stream) < 0) 6.470 + return -1; 6.471 + break; 6.472 + case SOS: 6.473 + if (parse_SOS(priv, stream) < 0) 6.474 + return -1; 6.475 + sos_marker_found = 1; 6.476 + break; 6.477 + case DHT: 6.478 + if (parse_DHT(priv, stream) < 0) 6.479 + return -1; 6.480 + dht_marker_found = 1; 6.481 + break; 6.482 + case DRI: 6.483 + if (parse_DRI(priv, stream) < 0) 6.484 + return -1; 6.485 + break; 6.486 + default: 6.487 + trace("> Unknown marker %2.2x\n", marker); 6.488 + break; 6.489 + } 6.490 + 6.491 + stream = next_chunck; 6.492 + } 6.493 + 6.494 + if (!dht_marker_found) { 6.495 + trace("No Huffman table loaded, using the default one\n"); 6.496 + build_default_huffman_tables(priv); 6.497 + } 6.498 + 6.499 + #ifdef SANITY_CHECK 6.500 + if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor) 6.501 + || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor)) 6.502 + error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n"); 6.503 + if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor) 6.504 + || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor)) 6.505 + error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n"); 6.506 + if ( (priv->component_infos[cCb].Hfactor!=1) 6.507 + || (priv->component_infos[cCr].Hfactor!=1) 6.508 + || (priv->component_infos[cCb].Vfactor!=1) 6.509 + || (priv->component_infos[cCr].Vfactor!=1)) 6.510 + error("Sampling other than 1x1 for Cr and Cb is not supported"); 6.511 + #endif 6.512 + 6.513 + return 0; 6.514 +} 6.515 \ No newline at end of file
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/VSs_tinyjpeg/tinyjpeg.c Thu Jul 05 11:35:03 2012 +0200 7.3 @@ -0,0 +1,575 @@ 7.4 +/* 7.5 + * Small jpeg decoder library 7.6 + * 7.7 + * Copyright (c) 2006, Luc Saillard <luc@saillard.org> 7.8 + * All rights reserved. 7.9 + * Redistribution and use in source and binary forms, with or without 7.10 + * modification, are permitted provided that the following conditions are met: 7.11 + * 7.12 + * - Redistributions of source code must retain the above copyright notice, 7.13 + * this list of conditions and the following disclaimer. 7.14 + * 7.15 + * - Redistributions in binary form must reproduce the above copyright notice, 7.16 + * this list of conditions and the following disclaimer in the documentation 7.17 + * and/or other materials provided with the distribution. 7.18 + * 7.19 + * - Neither the name of the author nor the names of its contributors may be 7.20 + * used to endorse or promote products derived from this software without 7.21 + * specific prior written permission. 7.22 + * 7.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 7.24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 7.25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 7.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 7.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 7.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 7.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 7.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 7.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 7.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 7.33 + * POSSIBILITY OF SUCH DAMAGE. 7.34 + * 7.35 + */ 7.36 + 7.37 +#include <stdio.h> 7.38 +#include <stdlib.h> 7.39 +#include <string.h> 7.40 +#include <stdint.h> 7.41 +#include <errno.h> 7.42 + 7.43 +#include "tinyjpeg.h" 7.44 +#include "tinyjpeg-internal.h" 7.45 + 7.46 +/* Global variable to return the last error found while deconding */ 7.47 +char error_string[256]; 7.48 + 7.49 +static const unsigned char zigzag[64] = 7.50 +{ 7.51 + 0, 1, 5, 6, 14, 15, 27, 28, 7.52 + 2, 4, 7, 13, 16, 26, 29, 42, 7.53 + 3, 8, 12, 17, 25, 30, 41, 43, 7.54 + 9, 11, 18, 24, 31, 40, 44, 53, 7.55 + 10, 19, 23, 32, 39, 45, 52, 54, 7.56 + 20, 22, 33, 38, 46, 51, 55, 60, 7.57 + 21, 34, 37, 47, 50, 56, 59, 61, 7.58 + 35, 36, 48, 49, 57, 58, 62, 63 7.59 +}; 7.60 + 7.61 +/* 7.62 + * 4 functions to manage the stream 7.63 + * 7.64 + * fill_nbits: put at least nbits in the reservoir of bits. 7.65 + * But convert any 0xff,0x00 into 0xff 7.66 + * get_nbits: read nbits from the stream, and put it in result, 7.67 + * bits is removed from the stream and the reservoir is filled 7.68 + * automaticaly. The result is signed according to the number of 7.69 + * bits. 7.70 + * look_nbits: read nbits from the stream without marking as read. 7.71 + * skip_nbits: read nbits from the stream but do not return the result. 7.72 + * 7.73 + * stream: current pointer in the jpeg data (read bytes per bytes) 7.74 + * nbits_in_reservoir: number of bits filled into the reservoir 7.75 + * reservoir: register that contains bits information. Only nbits_in_reservoir 7.76 + * is valid. 7.77 + * nbits_in_reservoir 7.78 + * <-- 17 bits --> 7.79 + * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir 7.80 + * ^ 7.81 + * bit 1 7.82 + * To get two bits from this example 7.83 + * result = (reservoir >> 15) & 3 7.84 + * 7.85 + */ 7.86 + 7.87 +#define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ 7.88 + while (nbits_in_reservoir<nbits_wanted) \ 7.89 + { \ 7.90 + unsigned char c; \ 7.91 + if (stream >= priv->stream_end) \ 7.92 + return -1; \ 7.93 + c = *stream++; \ 7.94 + reservoir <<= 8; \ 7.95 + if (c == 0xff && *stream == 0x00) \ 7.96 + stream++; \ 7.97 + reservoir |= c; \ 7.98 + nbits_in_reservoir+=8; \ 7.99 + } \ 7.100 +} while(0); 7.101 + 7.102 + 7.103 +/* Signed version !!!! */ 7.104 +#define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \ 7.105 + fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ 7.106 + result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \ 7.107 + nbits_in_reservoir -= (nbits_wanted); \ 7.108 + reservoir &= ((1U<<nbits_in_reservoir)-1); \ 7.109 + if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \ 7.110 + result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \ 7.111 +} while(0); 7.112 + 7.113 +#define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \ 7.114 + fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ 7.115 + result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \ 7.116 +} while(0); 7.117 + 7.118 +/* To speed up the decoding, we assume that the reservoir has enough bits */ 7.119 +#define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ 7.120 + nbits_in_reservoir -= (nbits_wanted); \ 7.121 + reservoir &= ((1U<<nbits_in_reservoir)-1); \ 7.122 +} while(0); 7.123 + 7.124 +static void resync(struct jdec_private *priv); 7.125 + 7.126 +/** 7.127 + * Get the next (valid) huffman code in the stream. 7.128 + * 7.129 + * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is 7.130 + * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code 7.131 + * and the value by using two lookup table. 7.132 + * Else if the value is not found, just search (linear) into an array for each 7.133 + * bits is the code is present. 7.134 + * 7.135 + * If the code is not present for any reason, -1 is return. 7.136 + */ 7.137 +static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table) 7.138 +{ 7.139 + int value, hcode; 7.140 + unsigned int extra_nbits, nbits; 7.141 + uint16_t *slowtable; 7.142 + 7.143 + look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode); 7.144 + value = huffman_table->lookup[hcode]; 7.145 + if (__likely(value >= 0)) 7.146 + { 7.147 + unsigned int code_size = huffman_table->code_size[value]; 7.148 + skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size); 7.149 + return value; 7.150 + } 7.151 + 7.152 + /* Decode more bits each time ... */ 7.153 + for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++) 7.154 + { 7.155 + nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits; 7.156 + 7.157 + look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode); 7.158 + slowtable = huffman_table->slowtable[extra_nbits]; 7.159 + /* Search if the code is in this array */ 7.160 + while (slowtable[0]) { 7.161 + if (slowtable[0] == hcode) { 7.162 + skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits); 7.163 + return slowtable[1]; 7.164 + } 7.165 + slowtable+=2; 7.166 + } 7.167 + } 7.168 + return 0; 7.169 +} 7.170 + 7.171 +/** 7.172 + * 7.173 + * Decode a single block that contains the DCT coefficients. 7.174 + * The table coefficients is already dezigzaged at the end of the operation. 7.175 + * 7.176 + */ 7.177 +static int process_Huffman_data_unit(struct jdec_private *priv, int component) 7.178 +{ 7.179 + unsigned char j; 7.180 + unsigned int huff_code; 7.181 + int retcode; 7.182 + unsigned char size_val, count_0; 7.183 + 7.184 + struct component *c = &priv->component_infos[component]; 7.185 + short int DCT[64]; 7.186 + 7.187 + /* Initialize the DCT coef table */ 7.188 + memset(DCT, 0, sizeof(DCT)); 7.189 + 7.190 + /* DC coefficient decoding */ 7.191 + retcode = get_next_huffman_code(priv, c->DC_table); 7.192 + // End of stream 7.193 + if(retcode == -1) 7.194 + return -1; 7.195 + else 7.196 + huff_code = (unsigned int)retcode; 7.197 + //trace("+ %x\n", huff_code); 7.198 + if (huff_code) { 7.199 + get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, huff_code, DCT[0]); 7.200 + DCT[0] += c->previous_DC; 7.201 + c->previous_DC = DCT[0]; 7.202 + } else { 7.203 + DCT[0] = c->previous_DC; 7.204 + } 7.205 + 7.206 + /* AC coefficient decoding */ 7.207 + j = 1; 7.208 + while (j<64) 7.209 + { 7.210 + huff_code = get_next_huffman_code(priv, c->AC_table); 7.211 + //trace("- %x\n", huff_code); 7.212 + 7.213 + size_val = huff_code & 0xF; 7.214 + count_0 = huff_code >> 4; 7.215 + 7.216 + if (size_val == 0) 7.217 + { /* RLE */ 7.218 + if (count_0 == 0) 7.219 + break; /* EOB found, go out */ 7.220 + else if (count_0 == 0xF) 7.221 + j += 16; /* skip 16 zeros */ 7.222 + } 7.223 + else 7.224 + { 7.225 + j += count_0; /* skip count_0 zeroes */ 7.226 + if (__unlikely(j >= 64)) 7.227 + { 7.228 + snprintf(error_string, sizeof(error_string), "Bad huffman data (buffer overflow)"); 7.229 + break; 7.230 + } 7.231 + get_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, size_val, DCT[j]); 7.232 + j++; 7.233 + } 7.234 + } 7.235 + 7.236 + for (j = 0; j < 64; j++) 7.237 + c->DCT[j] = DCT[zigzag[j]]; 7.238 + return 0; 7.239 +} 7.240 + 7.241 +/******************************************************************************* 7.242 + * 7.243 + * Colorspace conversion routine 7.244 + * 7.245 + * Note: 7.246 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are 7.247 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. 7.248 + * The conversion equations to be implemented are therefore 7.249 + * R = Y + 1.40200 * Cr 7.250 + * G = Y - 0.34414 * Cb - 0.71414 * Cr 7.251 + * B = Y + 1.77200 * Cb 7.252 + * 7.253 + ******************************************************************************/ 7.254 + 7.255 +static unsigned char clamp(int i) 7.256 +{ 7.257 + if (i<0) 7.258 + return 0; 7.259 + else if (i>255) 7.260 + return 255; 7.261 + else 7.262 + return i; 7.263 +} 7.264 + 7.265 +#define SCALEBITS 10 7.266 +#define ONE_HALF (1UL << (SCALEBITS-1)) 7.267 +#define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5)) 7.268 + 7.269 +/** 7.270 + * YCrCb -> RGB24 (2x2) 7.271 + * .-------. 7.272 + * | 1 | 2 | 7.273 + * |---+---| 7.274 + * | 3 | 4 | 7.275 + * `-------' 7.276 + */ 7.277 +static void YCrCB_to_RGB24_2x2(struct jdec_private *priv) 7.278 +{ 7.279 + const unsigned char *Y, *Cb, *Cr; 7.280 + unsigned char *p, *p2; 7.281 + int i,j; 7.282 + int offset_to_next_row; 7.283 + 7.284 + p = priv->plane; 7.285 + p2 = priv->plane + priv->width*3; 7.286 + Y = priv->Y; 7.287 + Cb = priv->Cb; 7.288 + Cr = priv->Cr; 7.289 + offset_to_next_row = (priv->width*3*2) - 16*3; 7.290 + for (i=0; i<8; i++) { 7.291 + 7.292 + for (j=0; j<8; j++) { 7.293 + 7.294 + int y, cb, cr; 7.295 + int add_r, add_g, add_b; 7.296 + int r, g , b; 7.297 + 7.298 + cb = *Cb++ - 128; 7.299 + cr = *Cr++ - 128; 7.300 + add_r = FIX(1.40200) * cr + ONE_HALF; 7.301 + add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF; 7.302 + add_b = FIX(1.77200) * cb + ONE_HALF; 7.303 + 7.304 + y = (*Y++) << SCALEBITS; 7.305 + r = (y + add_r) >> SCALEBITS; 7.306 + *p++ = clamp(r); 7.307 + g = (y + add_g) >> SCALEBITS; 7.308 + *p++ = clamp(g); 7.309 + b = (y + add_b) >> SCALEBITS; 7.310 + *p++ = clamp(b); 7.311 + 7.312 + y = (*Y++) << SCALEBITS; 7.313 + r = (y + add_r) >> SCALEBITS; 7.314 + *p++ = clamp(r); 7.315 + g = (y + add_g) >> SCALEBITS; 7.316 + *p++ = clamp(g); 7.317 + b = (y + add_b) >> SCALEBITS; 7.318 + *p++ = clamp(b); 7.319 + 7.320 + y = (Y[16-2]) << SCALEBITS; 7.321 + r = (y + add_r) >> SCALEBITS; 7.322 + *p2++ = clamp(r); 7.323 + g = (y + add_g) >> SCALEBITS; 7.324 + *p2++ = clamp(g); 7.325 + b = (y + add_b) >> SCALEBITS; 7.326 + *p2++ = clamp(b); 7.327 + 7.328 + y = (Y[16-1]) << SCALEBITS; 7.329 + r = (y + add_r) >> SCALEBITS; 7.330 + *p2++ = clamp(r); 7.331 + g = (y + add_g) >> SCALEBITS; 7.332 + *p2++ = clamp(g); 7.333 + b = (y + add_b) >> SCALEBITS; 7.334 + *p2++ = clamp(b); 7.335 + } 7.336 + Y += 16; 7.337 + p += offset_to_next_row; 7.338 + p2 += offset_to_next_row; 7.339 + } 7.340 +} 7.341 + 7.342 +/* 7.343 + * Decode a 2x2 7.344 + * .-------. 7.345 + * | 1 | 2 | 7.346 + * |---+---| 7.347 + * | 3 | 4 | 7.348 + * `-------' 7.349 + */ 7.350 +static int decode_MCU_2x2_3planes(struct jdec_private *priv) 7.351 +{ 7.352 + // Y 7.353 + if(process_Huffman_data_unit(priv, cY)) 7.354 + return -1; 7.355 + IDCT(&priv->component_infos[cY], priv->Y, 16); 7.356 + if(process_Huffman_data_unit(priv, cY)) 7.357 + return -1; 7.358 + IDCT(&priv->component_infos[cY], priv->Y+8, 16); 7.359 + if(process_Huffman_data_unit(priv, cY)) 7.360 + return -1; 7.361 + IDCT(&priv->component_infos[cY], priv->Y+64*2, 16); 7.362 + if(process_Huffman_data_unit(priv, cY)) 7.363 + return -1; 7.364 + IDCT(&priv->component_infos[cY], priv->Y+64*2+8, 16); 7.365 + 7.366 + // Cb 7.367 + if(process_Huffman_data_unit(priv, cCb)) 7.368 + return -1; 7.369 + IDCT(&priv->component_infos[cCb], priv->Cb, 8); 7.370 + 7.371 + // Cr 7.372 + if(process_Huffman_data_unit(priv, cCr)) 7.373 + return -1; 7.374 + IDCT(&priv->component_infos[cCr], priv->Cr, 8); 7.375 + 7.376 + return 0; 7.377 +} 7.378 + 7.379 +static void resync(struct jdec_private *priv) 7.380 +{ 7.381 + int i; 7.382 + 7.383 + /* Init DC coefficients */ 7.384 + for (i=0; i<COMPONENTS; i++) 7.385 + priv->component_infos[i].previous_DC = 0; 7.386 + 7.387 + priv->reservoir = 0; 7.388 + priv->nbits_in_reservoir = 0; 7.389 +} 7.390 + 7.391 +static int find_next_rst_marker(struct jdec_private *priv) 7.392 +{ 7.393 + int rst_marker_found = 0; 7.394 + int marker; 7.395 + const unsigned char *stream = priv->stream; 7.396 + 7.397 + /* Parse marker */ 7.398 + while (!rst_marker_found) 7.399 + { 7.400 + while (*stream++ != 0xff) 7.401 + { 7.402 + if (stream >= priv->stream_end) 7.403 + error("EOF while search for a RST marker."); 7.404 + } 7.405 + /* Skip any padding ff byte (this is normal) */ 7.406 + while (*stream == 0xff) 7.407 + stream++; 7.408 + 7.409 + marker = *stream++; 7.410 + if ((RST+priv->last_rst_marker_seen) == marker) 7.411 + rst_marker_found = 1; 7.412 + else if (marker >= RST && marker <= RST7) 7.413 + error("Wrong Reset marker found, abording"); 7.414 + else if (marker == EOI) 7.415 + return 0; 7.416 + } 7.417 + trace("RST Marker %d found at offset %ld\n", priv->last_rst_marker_seen, stream - priv->stream_begin); 7.418 + 7.419 + priv->stream = stream; 7.420 + priv->last_rst_marker_seen++; 7.421 + priv->last_rst_marker_seen &= 7; 7.422 + 7.423 + return 0; 7.424 +} 7.425 + 7.426 +/******************************************************************************* 7.427 + * 7.428 + * Functions exported of the library. 7.429 + * 7.430 + * Note: Some applications can access directly to internal pointer of the 7.431 + * structure. It's is not recommended, but if you have many images to 7.432 + * uncompress with the same parameters, some functions can be called to speedup 7.433 + * the decoding. 7.434 + * 7.435 + ******************************************************************************/ 7.436 + 7.437 +/** 7.438 + * Allocate a new tinyjpeg decoder object. 7.439 + * 7.440 + * Before calling any other functions, an object need to be called. 7.441 + */ 7.442 +struct jdec_private *tinyjpeg_init(void) 7.443 +{ 7.444 + struct jdec_private *priv; 7.445 + 7.446 + priv = (struct jdec_private *)calloc(1, sizeof(struct jdec_private)); 7.447 + if (priv == NULL) 7.448 + return NULL; 7.449 + return priv; 7.450 +} 7.451 + 7.452 +/** 7.453 + * Free a tinyjpeg object. 7.454 + * 7.455 + * No others function can be called after this one. 7.456 + */ 7.457 +void tinyjpeg_free(struct jdec_private *priv) 7.458 +{ 7.459 + free(priv); 7.460 +} 7.461 + 7.462 + 7.463 +/** 7.464 + * Create a new JPEG decode task 7.465 + * 7.466 + */ 7.467 +struct jdec_private *create_jdec_priv_task(struct jdec_private *priv, int tasknum) 7.468 +{ 7.469 + struct jdec_private *jdec_task; 7.470 + 7.471 + jdec_task = tinyjpeg_init(); 7.472 + resync(priv); 7.473 + if (tasknum > 0){ 7.474 + find_next_rst_marker(priv); 7.475 + } 7.476 + memcpy(jdec_task, priv, sizeof(struct jdec_private)); 7.477 + 7.478 + jdec_task->mcus_posx = (tasknum * priv->restart_interval) % priv->mcus_in_width; 7.479 + jdec_task->mcus_posy = (tasknum * priv->restart_interval) / priv->mcus_in_width; 7.480 + 7.481 + return jdec_task; 7.482 +} 7.483 + 7.484 +/** 7.485 + * Initialize the tinyjpeg object and prepare the decoding of the stream. 7.486 + * 7.487 + * Check if the jpeg can be decoded with this jpeg decoder. 7.488 + * Fill some table used for preprocessing. 7.489 + */ 7.490 +int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size) 7.491 +{ 7.492 + int ret; 7.493 + 7.494 + /* Identify the file */ 7.495 + if ((buf[0] != 0xFF) || (buf[1] != SOI)) 7.496 + error("Not a JPG file ?\n"); 7.497 + 7.498 + priv->stream_begin = buf+2; 7.499 + priv->stream_length = size-2; 7.500 + priv->stream_end = priv->stream_begin + priv->stream_length; 7.501 + 7.502 + ret = parse_JFIF(priv, priv->stream_begin); 7.503 + 7.504 + return ret; 7.505 +} 7.506 + 7.507 +/** 7.508 + * Decode and convert the jpeg image 7.509 + * 7.510 + * Note: components will be automaticaly allocated if no memory is attached. 7.511 + */ 7.512 +void tinyjpeg_decode_task(void *data, SlaveVP *animatingSlv ) 7.513 +{ 7.514 + //struct jdec_private *priv, uint8_t* context 7.515 + tinyjpeg_decode_task_args* args = (tinyjpeg_decode_task_args*) data; 7.516 + 7.517 + struct jdec_private *priv = args->priv; 7.518 + uint8_t* context = args->context; 7.519 + 7.520 + // Make OmpSs not complain while compiling 7.521 + //(void) context; 7.522 + 7.523 + unsigned int x, xstride_by_mcu, ystride_by_mcu; 7.524 + unsigned int bytes_per_blocklines, bytes_per_mcu; 7.525 + decode_MCU_fct decode_MCU; 7.526 + convert_colorspace_fct convert_to_pixfmt; 7.527 + 7.528 + bytes_per_blocklines = priv->width * RGB_DEPTH; 7.529 + bytes_per_mcu = RGB_DEPTH*8; 7.530 + 7.531 + // Only 2x2 CU sizes are supported in this simple decoder 7.532 + decode_MCU = decode_MCU_2x2_3planes; 7.533 + convert_to_pixfmt = YCrCB_to_RGB24_2x2; 7.534 + xstride_by_mcu = MCU_X_STRIDE; 7.535 + ystride_by_mcu = MCU_Y_STRIDE; 7.536 + 7.537 + bytes_per_blocklines *= ystride_by_mcu; 7.538 + bytes_per_mcu *= xstride_by_mcu/8; 7.539 + 7.540 + /* Just the decode the image by macroblock */ 7.541 + 7.542 + priv->plane = priv->components[0] + (priv->mcus_posy * bytes_per_blocklines) + (priv->mcus_posx * bytes_per_mcu); 7.543 + 7.544 + for (x=0; x < priv->restart_interval; x++) { 7.545 + if(decode_MCU(priv)) { 7.546 + fprintf(stderr, "%s\n", error_string); 7.547 + } 7.548 + convert_to_pixfmt(priv); 7.549 + 7.550 + priv->plane += bytes_per_mcu; 7.551 + priv->mcus_posx++; 7.552 + if (priv->mcus_posx >= priv->mcus_in_width){ 7.553 + priv->mcus_posy++; 7.554 + priv->mcus_posx = 0; 7.555 + priv->plane += (bytes_per_blocklines - priv->width*3); 7.556 + } 7.557 + } 7.558 + return; 7.559 +} 7.560 + 7.561 +const char *tinyjpeg_get_errorstring() 7.562 +{ 7.563 + return error_string; 7.564 +} 7.565 + 7.566 +void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height) 7.567 +{ 7.568 + *width = priv->width; 7.569 + *height = priv->height; 7.570 +} 7.571 + 7.572 +int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components) 7.573 +{ 7.574 + int i; 7.575 + for (i=0; priv->components[i] && i<COMPONENTS; i++) 7.576 + components[i] = priv->components[i]; 7.577 + return 0; 7.578 +} 7.579 \ No newline at end of file
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/VSs_tinyjpeg/tinyjpeg.h Thu Jul 05 11:35:03 2012 +0200 8.3 @@ -0,0 +1,79 @@ 8.4 +/* 8.5 + * Small jpeg decoder library (header file) 8.6 + * 8.7 + * Copyright (c) 2006, Luc Saillard <luc@saillard.org> 8.8 + * All rights reserved. 8.9 + * Redistribution and use in source and binary forms, with or without 8.10 + * modification, are permitted provided that the following conditions are met: 8.11 + * 8.12 + * - Redistributions of source code must retain the above copyright notice, 8.13 + * this list of conditions and the following disclaimer. 8.14 + * 8.15 + * - Redistributions in binary form must reproduce the above copyright notice, 8.16 + * this list of conditions and the following disclaimer in the documentation 8.17 + * and/or other materials provided with the distribution. 8.18 + * 8.19 + * - Neither the name of the author nor the names of its contributors may be 8.20 + * used to endorse or promote products derived from this software without 8.21 + * specific prior written permission. 8.22 + * 8.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 8.24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 8.25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 8.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 8.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 8.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 8.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 8.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 8.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 8.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 8.33 + * POSSIBILITY OF SUCH DAMAGE. 8.34 + * 8.35 + */ 8.36 + 8.37 +#ifndef __JPEGDEC_H__ 8.38 +#define __JPEGDEC_H__ 8.39 + 8.40 +#include "tinyjpeg-internal.h" 8.41 + 8.42 +#include "VSs_impl/VSs.h" 8.43 + 8.44 +/* Flags that can be set by any applications */ 8.45 +#define TINYJPEG_FLAGS_MJPEG_TABLE (1<<1) 8.46 + 8.47 +#define TINYJPEG_FMT_RGB24 3 8.48 + 8.49 +#define RGB_DEPTH 3 8.50 +#define MCU_X_STRIDE 16 8.51 +#define MCU_Y_STRIDE 16 8.52 + 8.53 +struct jdec_private *tinyjpeg_init(void); 8.54 +void tinyjpeg_free(struct jdec_private *priv); 8.55 +struct jdec_private *create_jdec_priv_task(struct jdec_private *priv, int tasknum); 8.56 + 8.57 +int tinyjpeg_parse_header(struct jdec_private *priv, const unsigned char *buf, unsigned int size); 8.58 + 8.59 + 8.60 +typedef struct{ 8.61 + struct jdec_private *priv; 8.62 + uint8_t* context; 8.63 +} tinyjpeg_decode_task_args; 8.64 + 8.65 +//#pragma omp task input(*priv) output(*context) 8.66 +void tinyjpeg_decode_task(void *data, SlaveVP *animatingSlv ); 8.67 + 8.68 +VSsTaskType *tinyjpegTaskType; 8.69 +SlaveVP* master; 8.70 + 8.71 +void convert_one_image_wrapper( void *_params, SlaveVP *animSlv ); 8.72 + 8.73 +const char *tinyjpeg_get_errorstring(); 8.74 +void tinyjpeg_get_size(struct jdec_private *priv, unsigned int *width, unsigned int *height); 8.75 +int tinyjpeg_get_components(struct jdec_private *priv, unsigned char **components); 8.76 +int tinyjpeg_set_components(struct jdec_private *priv, unsigned char **components, unsigned int ncomponents); 8.77 +int tinyjpeg_set_flags(struct jdec_private *priv, int flags); 8.78 + 8.79 +#endif 8.80 + 8.81 + 8.82 +
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/__brch__default Thu Jul 05 11:35:03 2012 +0200 9.3 @@ -0,0 +1,1 @@ 9.4 +Applications normally have only the default branch -- they shouldn't be affected by any choices in VMS or language..
10.1 Binary file test_images/scc_markers.jpg has changed
11.1 Binary file test_images/scc_markers2.jpg has changed
12.1 Binary file test_images/scc_markers2_lowq.jpg has changed
13.1 Binary file test_images/scc_markers_lowq.jpg has changed
14.1 Binary file test_images/scc_nomarkers.jpg has changed
15.1 Binary file test_images/scc_nomarkers_lowq.jpg has changed
