-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpeg2bmp.c
159 lines (134 loc) · 4.61 KB
/
jpeg2bmp.c
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
/***********************************************
Front-end for the JPEG DECODER
1. JPEGDecode() --> into coefficient buffer
2. De-quantization
3. IDCT
4. Conversion: YUV-to-RGB only
It has it's own fast integer scaled IDCT
together with de-quantization. See idct.c
* Copyright (c) 2017 A. Tarpai
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <malloc.h>
#include "bmp.h"
#include "jpeg81.h"
#include "idct.h"
extern void WriteBmp(struct BMP *bmp, char *path);
static struct JPEGD J; // a decoder object
extern void jpeg2bmp(char *path)
{
enum JPEGENUM err;
struct JPEGD *j= &J;
printf("JPEG decoding: \n");
err= JPEGDecode(j);
if ( err > 0 )
{
if ( 3 == j->Nf )
{
printf("Scaled de-quantization and IDCT.. ");
{
int c, i, n;
// Pre-scale quant-tables
int SQ[4][64];
for (c=0; c<4 && j->QT[c][0]; c++)
{
int *q= j->QT[c], *sq= SQ[c];
for (i=0; i<64; i++) sq[i]= q[i] * SCALEM[zigzag[i]];
}
// DEQUANT + IDCT
for (c=0; c<j->Nf; c++)
{
struct COMP *C= j->Components+c;
int *q= j->QT[C->Qi], *sq= SQ[C->Qi];
for (n=0; n < C->du_size; n++)
{
/*
// <--- scaled idct
int k, t[64];
TCOEF *coef= du[x];
t[0]= coef[0] * q[0] + 1024; // dequant DC and level-shift (8-bit)
for (k=1; k<64; k++) t[zigzag[k]] = coef[k] * q[k]; // dequant AC (+zigzag)
idct_s(t, coef);
*/
// <--- scaled idct with dequant
idct_sq( C->du[ (n / C->du_w) * C->du_width + n % C->du_w ], sq );
}
}
}
printf("done\n");
}
else printf("Not RGB\n");
// Primitive yuv-rgb converter for all sub-sampling types, 24-bit BMP only
if ( 3 == j->Nf )
{
enum BITPERPIXELENUM FORMAT= BITPERPIXEL24;
int bmpSize= BmpSize(FORMAT, j->X, j->Y);
struct BMP *bmp= malloc(bmpSize);
printf("Malloc for BMP... ");
if (bmp)
{
printf("OK\n");
printf("YUV-to-RGB conversion.. ");
BmpPrep(bmp, FORMAT, j->X, j->Y);
{
PBYTE BMPLINE= (PBYTE)bmp + bmp->bmfh.bfOffBits;
int h0= j->Hmax / j->Components[0].Hi;
int v0= j->Vmax / j->Components[0].Vi;
int h1= j->Hmax / j->Components[1].Hi;
int v1= j->Vmax / j->Components[1].Vi;
int h2= j->Hmax / j->Components[2].Hi;
int v2= j->Vmax / j->Components[2].Vi;
int x, y;
for (y=0; y < j->Y; y++)
{
PBYTE BMPPIX= BMPLINE;
TCOEF *C0= j->Components[0].du[ j->Components[0].du_width*((y/v0)/8) ] + 8*((y/v0)&7);
TCOEF *C1= j->Components[1].du[ j->Components[1].du_width*((y/v1)/8) ] + 8*((y/v1)&7);
TCOEF *C2= j->Components[2].du[ j->Components[2].du_width*((y/v2)/8) ] + 8*((y/v2)&7);
for (x=0; x < j->X; x++)
{
TCOEF c0= C0[ (x/h0/8)*64+((x/h0)&7) ];
TCOEF c1= C1[ (x/h1/8)*64+((x/h1)&7) ];
TCOEF c2= C2[ (x/h2/8)*64+((x/h2)&7) ];
// ITU BT.601 full-range YUV-to-RGB integer approximation
{
int y= (c0<<5)+16;
int u= c1-128;
int v= c2-128;
*BMPPIX++= CLIP[(y + 57 * u)>>5]; // B
*BMPPIX++= CLIP[(y - 11 * u - 23 * v)>>5]; // G
*BMPPIX++= CLIP[(y + 45 * v)>>5]; // R
}
}
BMPLINE += bmp->biPitch;
}
}
printf("done\n");
WriteBmp(bmp, path);
free(bmp);
}
else printf("FAILED!\n");
}
}
else printf("ERROR CODE: %d\n", err);
if (j->jpeg_mem) free(j->jpeg_mem);
}