| 1 |
/* jl2005bcd.c |
| 2 |
* |
| 3 |
* Converts raw output from Jeilin JL2005B/C/D to Bayer output. |
| 4 |
* |
| 5 |
* The code is based upon what is found in libgphoto2/camlibs/jl2005c, |
| 6 |
* Copyright (c) 2010 Theodore Kilgore <[email protected]> |
| 7 |
* |
| 8 |
* The decompression code used is |
| 9 |
* Copyright (c) 2010 Hans de Goede <[email protected]> |
| 10 |
* |
| 11 |
* This program is free software; you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU Lesser General Public License as published by |
| 13 |
* the Free Software Foundation; either version 2.1 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU Lesser General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU Lesser General Public License |
| 22 |
* along with this program; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#ifdef HAVE_JPEG |
| 30 |
#include "libv4lconvert-priv.h" |
| 31 |
#include "jpeg_memsrcdest.h" |
| 32 |
#include "libv4lsyscall-priv.h" |
| 33 |
|
| 34 |
|
| 35 |
#define JPEG_HEADER_SIZE 338 |
| 36 |
#define JPEG_HEIGHT_OFFSET 94 |
| 37 |
|
| 38 |
static int find_eoi(struct v4lconvert_data *data, |
| 39 |
const unsigned char *jpeg_data, int jpeg_data_idx, int jpeg_data_size) |
| 40 |
{ |
| 41 |
int i; |
| 42 |
|
| 43 |
for (i = jpeg_data_idx; i < (jpeg_data_size - 1); i++) |
| 44 |
if (jpeg_data[i] == 0xff && jpeg_data[i + 1] == 0xd9) |
| 45 |
break; |
| 46 |
|
| 47 |
if (i >= (jpeg_data_size - 1)) { |
| 48 |
V4LCONVERT_ERR("incomplete jl2005bcd frame\n"); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
|
| 52 |
return i + 2; /* + 2 -> Point to after EOI marker */ |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
int v4lconvert_decode_jl2005bcd(struct v4lconvert_data *data, |
| 57 |
const unsigned char *src, int src_size, |
| 58 |
unsigned char *dest, int width, int height) |
| 59 |
{ |
| 60 |
unsigned char jpeg_stripe[50000]; |
| 61 |
int q; |
| 62 |
struct jpeg_compress_struct cinfo; |
| 63 |
struct jpeg_decompress_struct dinfo; |
| 64 |
struct jpeg_error_mgr jcerr, jderr; |
| 65 |
JOCTET *jpeg_header = NULL; |
| 66 |
#if JPEG_LIB_VERSION >= 90 |
| 67 |
size_t jpeg_header_size = 0; |
| 68 |
#else |
| 69 |
unsigned long jpeg_header_size = 0; |
| 70 |
#endif |
| 71 |
int i, x, y, x1, y1, jpeg_data_size, jpeg_data_idx, eoi, size; |
| 72 |
|
| 73 |
/* src_size had better be bigger than 16 */ |
| 74 |
if (src_size < 16) |
| 75 |
return 1; |
| 76 |
|
| 77 |
/* The first 16 bytes of src are a raw frame header */ |
| 78 |
q = src[13] & 0x7f; |
| 79 |
|
| 80 |
if (height != src[4] << 3) { |
| 81 |
V4LCONVERT_ERR("Height is %d, not %d\n", src[4] << 3, height); |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
if (width != src[5] << 3) { |
| 86 |
V4LCONVERT_ERR("Width is %d, not %d\n", src[5] << 3, width); |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* And the fun begins, first of all create a dummy jpeg, which we use |
| 92 |
* to get the headers from to feed to libjpeg when decompressing the |
| 93 |
* stripes. This way we can use libjpeg's quant table handling |
| 94 |
* (and built in default huffman tables). |
| 95 |
*/ |
| 96 |
cinfo.err = jpeg_std_error (&jcerr); |
| 97 |
jpeg_create_compress (&cinfo); |
| 98 |
jpeg_mem_dest (&cinfo, &jpeg_header, &jpeg_header_size); |
| 99 |
cinfo.image_width = 16; |
| 100 |
cinfo.image_height = 16; |
| 101 |
cinfo.input_components = 3; |
| 102 |
cinfo.in_color_space = JCS_RGB; |
| 103 |
jpeg_set_defaults (&cinfo); |
| 104 |
/* Make comp[0] (which will be green) 1x2 subsampled */ |
| 105 |
cinfo.comp_info[0].h_samp_factor = 1; |
| 106 |
cinfo.comp_info[0].v_samp_factor = 2; |
| 107 |
/* Make comp[1] and [2] use huffman table and quanttable 0, as all |
| 108 |
* components use luminance settings with the jl2005c/d/e */ |
| 109 |
cinfo.comp_info[1].quant_tbl_no = 0; |
| 110 |
cinfo.comp_info[1].dc_tbl_no = 0; |
| 111 |
cinfo.comp_info[1].ac_tbl_no = 0; |
| 112 |
cinfo.comp_info[2].quant_tbl_no = 0; |
| 113 |
cinfo.comp_info[2].dc_tbl_no = 0; |
| 114 |
cinfo.comp_info[2].ac_tbl_no = 0; |
| 115 |
/* Apply the quality setting from the header */ |
| 116 |
if (q <= 0) |
| 117 |
i = 5000; |
| 118 |
else if (q <= 50) |
| 119 |
i = 5000 / q; |
| 120 |
else if (q <= 100) |
| 121 |
i = 2 * (100 - q); |
| 122 |
else |
| 123 |
i = 0; |
| 124 |
jpeg_set_linear_quality(&cinfo, i, TRUE); |
| 125 |
|
| 126 |
jpeg_start_compress (&cinfo, TRUE); |
| 127 |
while( cinfo.next_scanline < cinfo.image_height ) { |
| 128 |
JOCTET row[16 * 3]; |
| 129 |
JSAMPROW row_pointer[1] = { row }; |
| 130 |
jpeg_write_scanlines (&cinfo, row_pointer, 1); |
| 131 |
} |
| 132 |
jpeg_finish_compress (&cinfo); |
| 133 |
jpeg_destroy_compress (&cinfo); |
| 134 |
|
| 135 |
JSAMPLE green[8 * 16]; |
| 136 |
JSAMPLE red[8 * 8]; |
| 137 |
JSAMPLE blue[8 * 8]; |
| 138 |
JSAMPROW green_row_pointer[16]; |
| 139 |
JSAMPROW red_row_pointer[8]; |
| 140 |
JSAMPROW blue_row_pointer[8]; |
| 141 |
|
| 142 |
for (i = 0; i < 16; i++) |
| 143 |
green_row_pointer[i] = green + i * 8; |
| 144 |
|
| 145 |
for (i = 0; i < 8; i++) { |
| 146 |
red_row_pointer[i] = red + i * 8; |
| 147 |
blue_row_pointer[i] = blue + i * 8; |
| 148 |
} |
| 149 |
|
| 150 |
JSAMPARRAY samp_image[3] = { green_row_pointer, |
| 151 |
red_row_pointer, |
| 152 |
blue_row_pointer }; |
| 153 |
|
| 154 |
memcpy(jpeg_stripe, jpeg_header, JPEG_HEADER_SIZE); |
| 155 |
jpeg_stripe[JPEG_HEIGHT_OFFSET ] = height >> 8; |
| 156 |
jpeg_stripe[JPEG_HEIGHT_OFFSET + 1] = height; |
| 157 |
jpeg_stripe[JPEG_HEIGHT_OFFSET + 2] = 0; |
| 158 |
jpeg_stripe[JPEG_HEIGHT_OFFSET + 3] = 8; |
| 159 |
free (jpeg_header); |
| 160 |
/* Get past the raw frame header. */ |
| 161 |
src += 16; |
| 162 |
jpeg_data_size = src_size - 16; |
| 163 |
|
| 164 |
jpeg_data_idx = 0; |
| 165 |
|
| 166 |
|
| 167 |
dinfo.err = jpeg_std_error (&jderr); |
| 168 |
jpeg_create_decompress (&dinfo); |
| 169 |
for (x = 0; x < width; x += 16) { |
| 170 |
eoi = find_eoi(data, src, jpeg_data_idx, jpeg_data_size); |
| 171 |
if (eoi < 0) |
| 172 |
return eoi; |
| 173 |
|
| 174 |
size = eoi - jpeg_data_idx; |
| 175 |
if ((JPEG_HEADER_SIZE + size) > sizeof(jpeg_stripe)) { |
| 176 |
V4LCONVERT_ERR("stripe size too big %d > %zd\n", |
| 177 |
JPEG_HEADER_SIZE + size, |
| 178 |
sizeof(jpeg_stripe)); |
| 179 |
return 1; |
| 180 |
} |
| 181 |
memcpy (jpeg_stripe + JPEG_HEADER_SIZE, |
| 182 |
src + jpeg_data_idx, size); |
| 183 |
|
| 184 |
jpeg_mem_src (&dinfo, jpeg_stripe, JPEG_HEADER_SIZE + size); |
| 185 |
jpeg_read_header (&dinfo, TRUE); |
| 186 |
dinfo.raw_data_out = TRUE; |
| 187 |
#if JPEG_LIB_VERSION >= 70 |
| 188 |
dinfo.do_fancy_upsampling = FALSE; |
| 189 |
#endif |
| 190 |
jpeg_start_decompress (&dinfo); |
| 191 |
for (y = 0; y < height; y += 16) { |
| 192 |
jpeg_read_raw_data (&dinfo, samp_image, 16); |
| 193 |
for (y1 = 0; y1 < 16; y1 += 2) { |
| 194 |
for (x1 = 0; x1 < 16; x1 += 2) { |
| 195 |
dest[((y + y1 + 0) * width |
| 196 |
+ x + x1 + 0)] |
| 197 |
= red[y1 * 4 + x1 / 2]; |
| 198 |
dest[((y + y1 + 0) * width |
| 199 |
+ x + x1 + 1)] |
| 200 |
= green[y1 * 8 + x1 / 2]; |
| 201 |
dest[((y + y1 + 1) * width |
| 202 |
+ x + x1 + 0)] |
| 203 |
= green[y1 * 8 + 8 + x1 / 2]; |
| 204 |
dest[((y + y1 + 1) * width |
| 205 |
+ x + x1 + 1)] |
| 206 |
= blue[y1 * 4 + x1 / 2]; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
jpeg_finish_decompress (&dinfo); |
| 211 |
|
| 212 |
/* Set jpeg_data_idx for the next stripe */ |
| 213 |
jpeg_data_idx = (jpeg_data_idx + size + 0x0f) & ~0x0f; |
| 214 |
} |
| 215 |
jpeg_destroy_decompress(&dinfo); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
#endif /* HAVE_JPEG */ |