1 |
33 |
quickwayne |
#ifdef __MICROBLAZE
|
2 |
|
|
|
3 |
|
|
#include <stdio.h>
|
4 |
|
|
#include <stdlib.h>
|
5 |
|
|
|
6 |
|
|
#include "xup2pro.h"
|
7 |
|
|
|
8 |
|
|
#include "zzq.h"
|
9 |
|
|
#include "io.h"
|
10 |
|
|
#include "huffman.h"
|
11 |
|
|
#include "dct.h"
|
12 |
|
|
|
13 |
|
|
char* bmpimage;
|
14 |
|
|
int bmpsize;
|
15 |
|
|
|
16 |
|
|
INFOHEADER _bmpheader;
|
17 |
|
|
|
18 |
|
|
static signed char pixelmatrix[MATRIX_SIZE][MATRIX_SIZE*3],YMatrix[MATRIX_SIZE][MATRIX_SIZE],CrMatrix[MATRIX_SIZE][MATRIX_SIZE],CbMatrix[MATRIX_SIZE][MATRIX_SIZE];
|
19 |
|
|
static signed short temp[MATRIX_SIZE][MATRIX_SIZE], dctresult[MATRIX_SIZE][MATRIX_SIZE];
|
20 |
|
|
static signed char output[MATRIX_SIZE][MATRIX_SIZE];
|
21 |
|
|
static signed char bitstream[NUMBER_OF_PIXELS] ;
|
22 |
|
|
static unsigned char header[389];
|
23 |
|
|
|
24 |
|
|
int ejpgl_error(int errno, void* remark);
|
25 |
|
|
|
26 |
|
|
int main()
|
27 |
|
|
{
|
28 |
|
|
SYSACE_FILE *infile;
|
29 |
|
|
SYSACE_FILE *outfile;
|
30 |
|
|
SYSACE_FILE* outfile2;
|
31 |
|
|
|
32 |
|
|
int i;
|
33 |
|
|
INFOHEADER *bmpheader;
|
34 |
|
|
JPEGHEADER *jpegheader;
|
35 |
|
|
unsigned int col, cols, row, rows, remaining,component;
|
36 |
|
|
unsigned char amount_remaining, Ydcvalue, Cbdcvalue, Crdcvalue ;
|
37 |
|
|
int encode, compression;
|
38 |
|
|
|
39 |
|
|
encode = 1;
|
40 |
|
|
compression = 0;
|
41 |
|
|
|
42 |
|
|
bmpimage=(unsigned char*)0x70000000;
|
43 |
|
|
bmpsize=0;
|
44 |
|
|
|
45 |
|
|
xil_printf("\r\nBMP2JPG Code Compiled at %s %s\r\n", __DATE__, __TIME__);
|
46 |
|
|
|
47 |
|
|
bmpheader=&_bmpheader;
|
48 |
|
|
|
49 |
|
|
if ((infile = sysace_fopen("image01.bmp", "r")) == NULL) {
|
50 |
|
|
ejpgl_error(eOPENINPUT_FILE, 0);
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
bmpsize = sysace_fread(bmpimage, 1, 65536, infile);
|
54 |
|
|
xil_printf("bmpsize %d\r\n", bmpsize);
|
55 |
|
|
if (bmpsize==65536) {
|
56 |
|
|
ejpgl_error(eLARGE_INPUTFILE, 0);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
if ((outfile2 = sysace_fopen("image01b.bmp", "w")) == NULL) {
|
60 |
|
|
ejpgl_error(eOPENOUTPUT_FILE, 0);
|
61 |
|
|
}
|
62 |
|
|
sysace_fwrite(bmpimage, 1, bmpsize, outfile2);
|
63 |
|
|
sysace_fclose(outfile2);
|
64 |
|
|
|
65 |
|
|
if (getbmpheader(infile,bmpheader) == 0) { //File is a valid BMP
|
66 |
|
|
ejpgl_error(eINVALID_BMP, 0);
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
xil_printf("Image width: %d pixels\r\n", bmpheader->width);
|
70 |
|
|
xil_printf("Image height: %d pixels\r\n", bmpheader->height);
|
71 |
|
|
|
72 |
|
|
rows = bmpheader->height>>3;
|
73 |
|
|
cols = bmpheader->width>>3;
|
74 |
|
|
remaining=0x00;
|
75 |
|
|
amount_remaining=0x00;
|
76 |
|
|
Ydcvalue = 0x00;
|
77 |
|
|
Crdcvalue = 0x00;
|
78 |
|
|
Cbdcvalue = 0x00;
|
79 |
|
|
|
80 |
|
|
if ((outfile = sysace_fopen("image01.jpg", "w")) == NULL) {
|
81 |
|
|
ejpgl_error(eOPENOUTPUT_FILE, 0);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
writejpegheader(outfile,bmpheader);
|
85 |
|
|
|
86 |
|
|
for (row = 0; row < rows; row++) {
|
87 |
|
|
for (col = 0; col < cols; col++) {
|
88 |
|
|
readbmpfile(infile,pixelmatrix,row,col,bmpheader);
|
89 |
|
|
RGB2YCrCb(pixelmatrix,YMatrix,CrMatrix,CbMatrix);
|
90 |
|
|
for(component=0;component<3;component++) {
|
91 |
|
|
switch (component) {
|
92 |
|
|
case 0 ://Y-encoding
|
93 |
|
|
dct(YMatrix,dctresult);
|
94 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
95 |
|
|
Ydcvalue = EncodeDataUnit(bitstream,Ydcvalue,outfile, &remaining, &amount_remaining,component);
|
96 |
|
|
break;
|
97 |
|
|
case 1 ://Cr-encoding
|
98 |
|
|
dct(CrMatrix,dctresult);
|
99 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
100 |
|
|
Crdcvalue = EncodeDataUnit(bitstream,Crdcvalue,outfile, &remaining, &amount_remaining,component);
|
101 |
|
|
break;
|
102 |
|
|
case 2 ://Cb-encoding
|
103 |
|
|
dct(CbMatrix,dctresult);
|
104 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
105 |
|
|
Cbdcvalue = EncodeDataUnit(bitstream,Cbdcvalue,outfile, &remaining, &amount_remaining,component);
|
106 |
|
|
break;
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
HuffmanEncodeFinishSend(&remaining,&amount_remaining,outfile);
|
112 |
|
|
xil_printf("\r\nProcessed %d %dx%d-blocks.\r\n",(row-1)*cols+col,MATRIX_SIZE,MATRIX_SIZE);
|
113 |
|
|
writejpegfooter(outfile);
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
sysace_fclose(outfile);
|
117 |
|
|
sysace_fclose(infile);
|
118 |
|
|
return 0;
|
119 |
|
|
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
int ejpgl_error(int errno, void* remark) {
|
123 |
|
|
|
124 |
|
|
xil_printf("--> Error %d\r\n", errno);
|
125 |
|
|
exit(1);
|
126 |
|
|
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
#else
|
132 |
|
|
|
133 |
|
|
//---------------------------------------------------------------------------
|
134 |
|
|
typedef union { /* block of pixel-space values */
|
135 |
|
|
unsigned char block[8][8];
|
136 |
|
|
unsigned char linear[64];
|
137 |
|
|
} PBlock;
|
138 |
|
|
|
139 |
|
|
typedef union { /* block of frequency-space values */
|
140 |
|
|
int block[8][8];
|
141 |
|
|
int linear[64];
|
142 |
|
|
} FBlock;
|
143 |
|
|
|
144 |
|
|
#include <stdio.h>
|
145 |
|
|
#include <stdlib.h>
|
146 |
|
|
#ifndef __MICROBLAZE
|
147 |
|
|
#include <windows.h>
|
148 |
|
|
#endif
|
149 |
|
|
#include "zzq.h"
|
150 |
|
|
#include "io.h"
|
151 |
|
|
#include "huffman.h"
|
152 |
|
|
#include "dct.h"
|
153 |
|
|
#pragma hdrstop
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
//---------------------------------------------------------------------------
|
157 |
|
|
|
158 |
|
|
INFOHEADER _bmpheader;
|
159 |
|
|
|
160 |
|
|
#pragma argsused
|
161 |
|
|
int main(int argc, char* argv[])
|
162 |
|
|
{
|
163 |
|
|
int encode, compression,i;
|
164 |
|
|
signed char pixelmatrix[MATRIX_SIZE][MATRIX_SIZE*3],YMatrix[MATRIX_SIZE][MATRIX_SIZE],CrMatrix[MATRIX_SIZE][MATRIX_SIZE],CbMatrix[MATRIX_SIZE][MATRIX_SIZE];
|
165 |
|
|
/*= {{124, 105, 139, 95, 143, 98, 132, 114},
|
166 |
|
|
{105, 157, 61, 187, 51, 176, 80, 132},
|
167 |
|
|
{139, 61, 205, 17, 221, 32, 176, 98},
|
168 |
|
|
{95, 187, 17, 239, 0, 221, 51, 143},
|
169 |
|
|
{143, 51, 221, 0, 239, 17, 187, 95},
|
170 |
|
|
{98, 176, 32, 221, 17, 205, 61, 139},
|
171 |
|
|
{132, 80, 176, 51, 187, 61, 157, 105},
|
172 |
|
|
{114, 132, 98, 143, 95, 139, 105, 124}}; */
|
173 |
|
|
signed short temp[MATRIX_SIZE][MATRIX_SIZE], dctresult[MATRIX_SIZE][MATRIX_SIZE];
|
174 |
|
|
signed char output[MATRIX_SIZE][MATRIX_SIZE];
|
175 |
|
|
signed char bitstream[NUMBER_OF_PIXELS] ;//= {15,0,-2,-1,-1,-1,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
176 |
|
|
unsigned char header[389];
|
177 |
|
|
FILE *infile, *outfile;
|
178 |
|
|
INFOHEADER *bmpheader;
|
179 |
|
|
JPEGHEADER *jpegheader;
|
180 |
|
|
#ifndef __MICROBLAZE
|
181 |
|
|
LARGE_INTEGER st, en;
|
182 |
|
|
#endif
|
183 |
|
|
unsigned int col, cols, row, rows, remaining,component;
|
184 |
|
|
unsigned char amount_remaining, Ydcvalue, Cbdcvalue, Crdcvalue ;
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
// bmpheader = (INFOHEADER *)malloc(sizeof(INFOHEADER));
|
188 |
|
|
bmpheader=&_bmpheader;
|
189 |
|
|
|
190 |
|
|
switch (argc) {
|
191 |
|
|
case 3 :
|
192 |
|
|
encode = 0;
|
193 |
|
|
break;
|
194 |
|
|
case 4 :
|
195 |
|
|
encode = 1;
|
196 |
|
|
break;
|
197 |
|
|
case 5 :
|
198 |
|
|
encode = 1;
|
199 |
|
|
compression = atoi(argv[4]);
|
200 |
|
|
break;
|
201 |
|
|
default :
|
202 |
|
|
printf("invalid number of parameters\n\nUSAGE: jpegcodec source_file destination_file [/E] [compression_rate]\n");
|
203 |
|
|
printf("\nsource_file \t\t location of original file");
|
204 |
|
|
printf("\ndestination_file \t location of the new file");
|
205 |
|
|
printf("\n/E \t\t\t When set the source_file will be encoded to JPEG \n\t\t\t otherwise it will be decoded to BMP.");
|
206 |
|
|
printf("\ncompression_rate \t specifies the compression ratio, defaults to the\n\t\t\t standard compression ratio. (Only valid when /D is set)\n");
|
207 |
|
|
system("PAUSE");
|
208 |
|
|
return 0;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
infile = fopen(argv[1],"rb");
|
212 |
|
|
if (infile == NULL) {
|
213 |
|
|
printf("Input file %s does not exist!\n\nUSAGE: jpegcodec source_file destination_file [/E] [compression_rate]\n",argv[1]);
|
214 |
|
|
printf("\nsource_file \t\t location of original file");
|
215 |
|
|
printf("\ndestination_file \t location of the new file");
|
216 |
|
|
printf("\n/E \t\t\t When set the source_file will be encoded to JPEG \n\t\t\t otherwise it will be decoded to BMP.");
|
217 |
|
|
printf("\ncompression_rate \t specifies the compression ratio, defaults to the\n\t\t\t standard compression ratio. (Only valid when /D is set)\n");
|
218 |
|
|
system("PAUSE");
|
219 |
|
|
}
|
220 |
|
|
else { //start codec
|
221 |
|
|
outfile = fopen(argv[2],"wb");
|
222 |
|
|
#ifndef __MICROBLAZE
|
223 |
|
|
QueryPerformanceCounter(&st);
|
224 |
|
|
#endif
|
225 |
|
|
if(encode) { //encode infile to JPEG
|
226 |
|
|
if (getbmpheader(infile,bmpheader)) { //File is a valid BMP
|
227 |
|
|
printf("\nImage width: %d pixels", bmpheader->width);
|
228 |
|
|
printf("\nImage height: %d pixels", bmpheader->height);
|
229 |
|
|
rows = bmpheader->height>>3;
|
230 |
|
|
cols = bmpheader->width>>3;
|
231 |
|
|
remaining=0x00;
|
232 |
|
|
amount_remaining=0x00;
|
233 |
|
|
Ydcvalue = 0x00;
|
234 |
|
|
Crdcvalue = 0x00;
|
235 |
|
|
Cbdcvalue = 0x00;
|
236 |
|
|
writejpegheader(outfile,bmpheader);
|
237 |
|
|
|
238 |
|
|
for (row = 0; row < rows; row++) {
|
239 |
|
|
for (col = 0; col < cols; col++) {
|
240 |
|
|
readbmpfile(infile,pixelmatrix,row,col,bmpheader);
|
241 |
|
|
RGB2YCrCb(pixelmatrix,YMatrix,CrMatrix,CbMatrix);
|
242 |
|
|
for(component=0;component<3;component++)
|
243 |
|
|
{
|
244 |
|
|
switch (component) {
|
245 |
|
|
case 0 ://Y-encoding
|
246 |
|
|
dct(YMatrix,dctresult);
|
247 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
248 |
|
|
Ydcvalue = EncodeDataUnit(bitstream,Ydcvalue,outfile, &remaining, &amount_remaining,component);
|
249 |
|
|
break;
|
250 |
|
|
case 1 ://Cr-encoding
|
251 |
|
|
dct(CrMatrix,dctresult);
|
252 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
253 |
|
|
Crdcvalue = EncodeDataUnit(bitstream,Crdcvalue,outfile, &remaining, &amount_remaining,component);
|
254 |
|
|
break;
|
255 |
|
|
case 2 ://Cb-encoding
|
256 |
|
|
dct(CbMatrix,dctresult);
|
257 |
|
|
zzq(dctresult,bitstream,compression,encode);
|
258 |
|
|
Cbdcvalue = EncodeDataUnit(bitstream,Cbdcvalue,outfile, &remaining, &amount_remaining,component);
|
259 |
|
|
break;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
HuffmanEncodeFinishSend(&remaining,&amount_remaining,outfile);
|
265 |
|
|
printf("\nProcessed %d %dx%d-blocks.",(row-1)*cols+col,MATRIX_SIZE,MATRIX_SIZE);
|
266 |
|
|
//write JPEG footer
|
267 |
|
|
writejpegfooter(outfile);
|
268 |
|
|
|
269 |
|
|
}
|
270 |
|
|
else {
|
271 |
|
|
printf("\n%s is not a valid BMP-file",argv[1]);
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
} else { //decode infile to BMP
|
275 |
|
|
|
276 |
|
|
// readjpegfile(infile,inputbuffer);
|
277 |
|
|
// huffmancodec(inputbuffer,bitstream,encode);
|
278 |
|
|
// zzq(pixelmatrix,bitstream,compression,encode);
|
279 |
|
|
// DCT(dctmatrix,outputbuffer,encode);
|
280 |
|
|
// writebmp(outputbuffer);*/
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
fclose(outfile);
|
284 |
|
|
fclose(infile);
|
285 |
|
|
/* infile = fopen("test.jpg","rb");
|
286 |
|
|
outfile = fopen("header.h","wb");
|
287 |
|
|
fread(header,389,1,infile);
|
288 |
|
|
for(i=0;i<389;i++)
|
289 |
|
|
fprintf(outfile,"0x%x, ",header[i]);
|
290 |
|
|
fclose(outfile);
|
291 |
|
|
fclose(infile); */
|
292 |
|
|
#ifndef __MICROBLAZE
|
293 |
|
|
QueryPerformanceCounter(&en);
|
294 |
|
|
printf("\nExecution time: %f seconds",(double)(en.QuadPart-st.QuadPart)/1000000);
|
295 |
|
|
#endif
|
296 |
|
|
// free(bmpheader);
|
297 |
|
|
}
|
298 |
|
|
printf("\n\nHit ENTER to close this window.");
|
299 |
|
|
getchar();
|
300 |
|
|
return 0;
|
301 |
|
|
}
|
302 |
|
|
//---------------------------------------------------------------------------
|
303 |
|
|
|
304 |
|
|
#endif
|
305 |
|
|
|