1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include "jpeg_rgb_internal.h"
#include "jpeg.h"
static void convert (JpegRGBDecoder * rgbdec);
static void imagescale2h_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2h2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void scanlinescale2_u8 (unsigned char *dest, unsigned char *src,
int len);
JpegRGBDecoder *
jpeg_rgb_decoder_new (void)
{
JpegRGBDecoder *rgbdec;
rgbdec = g_new0 (JpegRGBDecoder, 1);
rgbdec->dec = jpeg_decoder_new ();
return rgbdec;
}
void
jpeg_rgb_decoder_free (JpegRGBDecoder * rgbdec)
{
int i;
jpeg_decoder_free (rgbdec->dec);
for (i = 0; i < 3; i++) {
if (rgbdec->component[i].alloc) {
g_free (rgbdec->component[i].image);
}
}
g_free (rgbdec);
}
int
jpeg_rgb_decoder_addbits (JpegRGBDecoder * dec, unsigned char *data,
unsigned int len)
{
return jpeg_decoder_addbits (dec->dec, data, len);
}
int
jpeg_rgb_decoder_parse (JpegRGBDecoder * dec)
{
return jpeg_decoder_parse (dec->dec);
}
int
jpeg_rgb_decoder_get_image (JpegRGBDecoder * rgbdec,
unsigned char **image, int *rowstride, int *width, int *height)
{
int i;
jpeg_decoder_get_image_size (rgbdec->dec, &rgbdec->width, &rgbdec->height);
for (i = 0; i < 3; i++) {
jpeg_decoder_get_component_ptr (rgbdec->dec, i + 1,
&rgbdec->component[i].image, &rgbdec->component[i].rowstride);
jpeg_decoder_get_component_subsampling (rgbdec->dec, i + 1,
&rgbdec->component[i].h_subsample, &rgbdec->component[i].v_subsample);
rgbdec->component[i].alloc = 0;
if (rgbdec->component[i].h_subsample > 1 ||
rgbdec->component[i].v_subsample > 1) {
unsigned char *dest;
dest = g_malloc (rgbdec->width * rgbdec->height);
if (rgbdec->component[i].v_subsample > 1) {
if (rgbdec->component[i].h_subsample > 1) {
imagescale2h2v_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
} else {
imagescale2v_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
}
} else {
imagescale2h_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
}
rgbdec->component[i].alloc = 1;
rgbdec->component[i].image = dest;
rgbdec->component[i].rowstride = rgbdec->width;
rgbdec->component[i].h_subsample = 1;
rgbdec->component[i].v_subsample = 1;
}
}
rgbdec->image = g_malloc (rgbdec->width * rgbdec->height * 4);
convert (rgbdec);
if (image)
*image = rgbdec->image;
if (rowstride)
*rowstride = rgbdec->width * 4;
if (width)
*width = rgbdec->width;
if (height)
*height = rgbdec->height;
return 0;
}
/* from the JFIF spec */
#define YUV_TO_R(y,u,v) CLAMP((y) + 1.402*((v)-128),0,255)
#define YUV_TO_G(y,u,v) CLAMP((y) - 0.34414*((u)-128) - 0.71414*((v)-128),0,255)
#define YUV_TO_B(y,u,v) CLAMP((y) + 1.772*((u)-128),0,255)
static void
convert (JpegRGBDecoder * rgbdec)
{
int x, y;
unsigned char *yp;
unsigned char *up;
unsigned char *vp;
unsigned char *rgbp;
rgbp = rgbdec->image;
yp = rgbdec->component[0].image;
up = rgbdec->component[1].image;
vp = rgbdec->component[2].image;
for (y = 0; y < rgbdec->height; y++) {
for (x = 0; x < rgbdec->width; x++) {
rgbp[0] = YUV_TO_B (yp[x], up[x], vp[x]);
rgbp[1] = YUV_TO_G (yp[x], up[x], vp[x]);
rgbp[2] = YUV_TO_R (yp[x], up[x], vp[x]);
rgbp[3] = 0;
rgbp += 4;
}
yp += rgbdec->component[0].rowstride;
up += rgbdec->component[1].rowstride;
vp += rgbdec->component[2].rowstride;
}
}
static void
imagescale2h_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int s_rowstride, int width, int height)
{
int i;
for (i = 0; i < height; i++) {
scanlinescale2_u8 (dest + i * d_rowstride, src + i * s_rowstride, width);
}
}
static void
imagescale2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int s_rowstride, int width, int height)
{
int i;
for (i = 0; i < height; i++) {
memcpy (dest + i * d_rowstride, src + (i / 2) * s_rowstride, width);
}
}
static void
imagescale2h2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int s_rowstride, int width, int height)
{
int i;
for (i = 0; i < height; i++) {
scanlinescale2_u8 (dest + i * d_rowstride,
src + (i / 2) * s_rowstride, width);
}
}
static void
scanlinescale2_u8 (unsigned char *dest, unsigned char *src, int len)
{
int i;
for (i = 0; i < len; i++) {
dest[i] = src[i / 2];
}
}
|