-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.c
More file actions
219 lines (204 loc) · 7.85 KB
/
convert.c
File metadata and controls
219 lines (204 loc) · 7.85 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "convert.h"
#include "jpeg.h"
#include "dct.h"
#include "jpeg_internal.h"
#include "bitmap.h"
#include "bitmap_internal.h"
#include "scan_start.h"
static float contribution(unsigned int bitmap_channel, component_id component, float value);
typedef struct scale_s {
float factor;
float offset;
} scale;
static const scale y_to_rgb[BITMAP_NUM_CHANNELS]
= {{ 1.0f, 0.0f}
,{ 1.0f, 0.0f}
,{ 1.0f, 0.0f}};
static const scale cb_to_rgb[BITMAP_NUM_CHANNELS]
= {{ 1.772f, -128.0f}
,{-0.34414f, -128.0f}
,{ 0.0f, 0.0f}};
static const scale cr_to_rgb[BITMAP_NUM_CHANNELS]
= {{ 0.0f, 0.0f}
,{-0.71414f, -128.0f}
,{ 1.402f, -128.0f}};
static const scale *ycbcr_to_rgb[NUM_COMPONENTS] = {y_to_rgb, cb_to_rgb, cr_to_rgb};
static float contribution(unsigned int bitmap_channel
,component_id component
,float value) {
scale s = ycbcr_to_rgb[component - 1][bitmap_channel];
return (value + s.offset) * s.factor;
}
static int read_data_unit(const jpeg *j
,component *c
,int chunk[JPEG_CHUNK_NUM_SAMPLES]);
static int convert_mcu(const jpeg *j, bitmap *b, int restart, int row, int col);
static void write_pixels_to_bitmap(float pixels[JPEG_CHUNK_SIDE_LENGTH][JPEG_CHUNK_SIDE_LENGTH]
,const jpeg *j
,component *component
,unsigned int row
,unsigned int col
,unsigned int h
,unsigned int v
,bitmap *b);
bitmap *jpeg_to_bitmap(const jpeg *j) {
jpeg_stream *stream;
bitmap *b = malloc(sizeof(bitmap));
int error = 0;
int done = 0;
unsigned int i;
unsigned int row = 0;
unsigned int col = 0;
size_t mcus_read = 0;
int restart = 0;
assert(j);
stream = j->scan_start->stream;
b->num_cols = j->frame->samples_per_line;
b->num_rows = j->frame->num_lines;
for (i = 0; i < BITMAP_NUM_CHANNELS; i++) {
b->samples[i] = calloc(b->num_rows * b->num_cols, sizeof(float));
}
while (!done && !error) {
int state;
if (restart) {
jpeg_stream_restart(stream);
}
error = convert_mcu(j, b, restart, row, col);
col += j->frame->highest_sampling_factor * JPEG_CHUNK_SIDE_LENGTH;
if (col >= b->num_cols) {
col = 0;
row += j->frame->highest_sampling_factor * JPEG_CHUNK_SIDE_LENGTH;
}
mcus_read += 1;
restart = 0;
if (j->has_restart_interval && mcus_read % j->restart_interval == 0) {
restart = 1;
}
state = jpeg_stream_get_state(stream);
if (state == JPEG_STREAM_STATE_OUT_OF_DATA) {
printf("Error reading image.\n");
error = 1;
/* Check for EOI marker */
} else if (state == JPEG_STREAM_STATE_EOI) {
printf("Finished reading image.\n");
done = 1;
}
}
return b;
}
static int convert_mcu(const jpeg *j, bitmap *b, int restart, int row, int col) {
unsigned int c;
int error = 0;
for (c = 0; c < j->frame->num_components && !error; c++) {
unsigned int v;
component *component = &j->frame->components[c];
if (restart) {
component->prev_dc_coeff = 0;
}
for (v = 0; v < component->sampling_factor_vertical && !error; v++) {
unsigned int h;
for (h = 0; h < component->sampling_factor_horizontal && !error; h++) {
int chunk[JPEG_CHUNK_NUM_SAMPLES];
float pixels[JPEG_CHUNK_SIDE_LENGTH][JPEG_CHUNK_SIDE_LENGTH];
error = read_data_unit(j, component, chunk);
if (!error) {
qtable_dequantise(j->qtables[component->qtable_id], chunk);
dct_inverse(chunk, pixels);
write_pixels_to_bitmap(pixels, j, component, row, col, h, v, b);
}
}
}
}
return error;
}
static void write_pixels_to_bitmap(float pixels[JPEG_CHUNK_SIDE_LENGTH][JPEG_CHUNK_SIDE_LENGTH]
,const jpeg *j
,component *component
,unsigned int row
,unsigned int col
,unsigned int h
,unsigned int v
,bitmap *b) {
unsigned int n, m;
float pixel;
for (n = 0; n < (JPEG_CHUNK_SIDE_LENGTH * j->frame->highest_sampling_factor)
/ component->sampling_factor_horizontal; n++) {
for (m = 0; m < (JPEG_CHUNK_SIDE_LENGTH * j->frame->highest_sampling_factor)
/ component->sampling_factor_vertical; m++) {
pixel = pixels[n * component->sampling_factor_horizontal / j->frame->highest_sampling_factor]
[m * component->sampling_factor_vertical / j->frame->highest_sampling_factor];
unsigned int real_col = col + h * JPEG_CHUNK_SIDE_LENGTH;
unsigned int real_row = row + v * JPEG_CHUNK_SIDE_LENGTH;
if ((real_row + n) < b->num_rows && (real_col + m) < b->num_cols) {
unsigned int channel;
for (channel = 0; channel < BITMAP_NUM_CHANNELS; channel++) {
b->samples[channel][(real_row + n) * b->num_cols + (real_col + m)]
+= contribution(channel, component->id, pixel);
}
}
}
}
}
static int read_data_unit(const jpeg *j
,component *c
,int chunk[JPEG_CHUNK_NUM_SAMPLES]) {
int status;
int error = 0;
jpeg_stream *stream;
size_t num_previous_zeros = 0;
stream = j->scan_start->stream;
/* Read DC coefficient */
htable *table = htable_get_table(j->htables
,HTABLE_TYPE_DC
,c->dc_htable_id);
int dc_delta = 0;
size_t sample = 0;
status = htable_decode(stream, table, &dc_delta, &num_previous_zeros);
if (status == HTABLE_OK || status == HTABLE_END_OF_BLOCK) {
c->prev_dc_coeff += dc_delta;
chunk[sample] = c->prev_dc_coeff;
sample += 1;
/* Read AC coefficients */
status = HTABLE_OK;
while (status == HTABLE_OK && sample < JPEG_CHUNK_NUM_SAMPLES) {
int ac_coeff;
num_previous_zeros = 0;
table = htable_get_table(j->htables
,HTABLE_TYPE_AC
,c->ac_htable_id);
status = htable_decode(stream, table, &ac_coeff, &num_previous_zeros);
if (status == HTABLE_OK) {
unsigned int i;
for (i = 0; i < num_previous_zeros; i++) {
if ((sample + i) < JPEG_CHUNK_NUM_SAMPLES) {
chunk[sample + i] = 0;
} else {
status = HTABLE_ERR_DECODE;
}
}
if (status == HTABLE_OK) {
sample += num_previous_zeros;
chunk[sample] = ac_coeff;
sample += 1;
}
}
}
/* Fill in zeros at end of block */
if (status == HTABLE_END_OF_BLOCK) {
while (sample < JPEG_CHUNK_NUM_SAMPLES) {
chunk[sample] = 0;
sample += 1;
}
} else if (sample < JPEG_CHUNK_NUM_SAMPLES) {
printf("Error during huffman decoding\n");
error = 1;
}
} else {
printf("Error during huffman decoding\n");
error = 1;
}
return error;
}