-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecodejpeg.c
More file actions
187 lines (141 loc) · 3.23 KB
/
Copy pathdecodejpeg.c
File metadata and controls
187 lines (141 loc) · 3.23 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
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include "decodejpeg.h"
#include "definitions.h"
#include "tools.h"
int jpegDecode(const jpeg_t *jpeg)
{
if (!jpeg)
return 1;
ht_t ht_0 = decodeHT(jpeg->dht[0], jpeg->dhtLength[0]);
ht_t ht_1 = decodeHT(jpeg->dht[1], jpeg->dhtLength[1]);
sos_t sos = decodeSOS(jpeg->sos);
sof0_t sof0 = decodeSof0(jpeg->sof0);
decode(jpeg->scan);
free(ht_0.symbols);
free(ht_1.symbols);
free(sos.idComp);
free(sos.idHTable);
free(sof0.idComp);
free(sof0.factor);
free(sof0.idQt);
return 0;
}
int decode(const uint8_t *data)
{
if(!data)
return 1;
for(int i = 0; i < 16; ++i)
printf("0x%02x\n", *data++);
return 0;
}
sof0_t decodeSof0(const uint8_t *data)
{
sof0_t sof0;
sof0.idComp = NULL;
sof0.factor = NULL;
sof0.idQt = NULL;
if (!data)
return sof0;
const uint8_t precision = *data++;
if (precision > 8)
{
fprintf(stderr, "ERROR: Precision %d not supported", precision);
return sof0;
}
const uint8_t height = *data++;
const uint8_t width = *data++;
const uint8_t nComp = *data++;
sof0.idComp = malloc(nComp * sizeof(uint8_t));
if (!sof0.idComp)
return sof0;
sof0.factor = malloc(nComp * sizeof(uint8_t));
if (!sof0.factor)
return sof0;
sof0.idQt = malloc(nComp * sizeof(uint8_t));
if (!sof0.idQt)
return sof0;
for (uint8_t i = 0; i < nComp; ++i)
{
sof0.idComp[i] = *data++;
sof0.factor[i] = *data++;
sof0.idQt[i] = *data++;
}
sof0.precision = precision;
sof0.height = height;
sof0.width = width;
sof0.nComp = nComp;
return sof0;
}
sos_t decodeSOS(const uint8_t *data)
{
sos_t sos;
sos.idComp = NULL;
sos.idHTable = NULL;
if (!data)
return sos;
const uint8_t nComp = *data++;
if (nComp < 1 || nComp > 4)
return sos;
sos.idComp = malloc(nComp * sizeof(uint8_t));
if (!sos.idComp)
return sos;
sos.idHTable = malloc(nComp * sizeof(uint8_t));
if (!sos.idHTable)
return sos;
for (uint8_t i = 0; i < nComp; ++i)
{
sos.idComp[i] = data[i];
sos.idHTable[i] = data[i];
}
sos.nComp = nComp;
return sos;
}
ht_t decodeHT(const uint8_t *data, const uint16_t length)
{
ht_t ht;
ht.symbols = NULL;
if (! data)
return ht;
const uint16_t dhtLength = length;
const uint8_t sizeNSymbols = 16;
const uint8_t id = (*data) & 0x0f;
const uint8_t type = (*data >> 4);
if (type > 1)
{
fprintf(stderr, "ERROR: Unknown HT type: %0x02x\n", ht.type);
}
data++;
for (uint8_t i = 0; i < sizeNSymbols; ++i)
{
ht.nSymbols[i] = *data++;
}
if (dhtLength < (sizeNSymbols - 1))
return ht;
const uint16_t sizeHTTable = dhtLength - sizeNSymbols - 1;
ht.symbols = malloc(sizeHTTable * sizeof(uint8_t));
if (!ht.symbols)
return ht;
for(uint16_t i = 0; i < sizeHTTable; ++i)
{
ht.symbols[i] = *data++;
}
printf("\n");
printf("HTTable:\n");
uint16_t ctr = 0;
for (uint8_t i = 0; i < sizeNSymbols; ++i)
{
printf("%d ", 1+i);
for(int j = 0; j < ht.nSymbols[i]; ++j)
{
printf("0x%02x ", ht.symbols[ctr]);
ctr++;
}
printf("\n");
}
ht.id = id;
ht.type = type;
return ht;
}