OpenCores
URL https://opencores.org/ocsvn/mpdma/mpdma/trunk

Subversion Repositories mpdma

[/] [mpdma/] [trunk/] [mb-bmp2jpg/] [bmp2jpg_mb.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 quickwayne
#include <stdio.h>
2
#include <stdlib.h>
3
 
4
#include "ejpgl.h"
5
#include "mb.h"
6
 
7
#include "zzq.h" 
8
#include "io.h"
9
#include "huffman.h"
10
#include "dct.h"
11
 
12
#ifndef __MICROBLAZE
13
#error This code is for Micrblaze processor only
14
#endif
15
 
16
char* bmpimage;
17
int bmpsize;
18
 
19
INFOHEADER _bmpheader;
20
INFOHEADER *bmpheader;
21
JPEGHEADER _jpegheader;
22
JPEGHEADER *jpegheader;
23
 
24
SYSACE_FILE *infile;
25
SYSACE_FILE *outfile;
26
 
27
unsigned char qtable[64] = {16, 8, 8, 16, 12, 8, 16, 16, 16, 16, 16, 16, 16, 16,
28
16, 32, 32, 16, 16, 16, 16, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64,
29
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 64, 64, 128, 128, 128, 128, 128, 64, 64,
30
128, 128, 128, 128, 128, 64, 128, 128, 128};
31
 
32
unsigned char huffmancount[4][16] = {{0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},  //standard DC table count
33
                                    {0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D},   //standard AC table count
34
                                    {0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},  //standard DC table count
35
                                    {0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D}};  //standard AC table count
36
 
37
unsigned char huffDCvalues[12] ={0x00,  0x01,  0x02,  0x03,  0x04,  0x05,  0x06,  0x07,  0x08,  0x09,  0x0a,  0x0b};// {0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E};
38
unsigned char huffACvalues[162] = {0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
39
                                0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82,
40
                                0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
41
                                0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64,
42
                                0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87,
43
                                0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
44
                                0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
45
                                0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
46
                                0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA};
47
 
48
 
49
signed char pixelmatrix[MACRO_BLOCK_SIZE][MACRO_BLOCK_SIZE*3];
50
signed char YMatrix[MATRIX_SIZE][MATRIX_SIZE];
51
signed char CrMatrix[MATRIX_SIZE][MATRIX_SIZE];
52
signed char CbMatrix[MATRIX_SIZE][MATRIX_SIZE];
53
 
54
int ejpgl_error(int errno, void* remark);
55
 
56
static unsigned char buffer[MACRO_BLOCK_SIZE*3];  // move array on main memory
57
 
58
void get_MB(int mb_row, int mb_col, signed char pixelmatrix[MACRO_BLOCK_SIZE][MACRO_BLOCK_SIZE*3]) {
59
       unsigned int row, col;
60
        int offset;
61
 
62
        for(row = 0;row < MACRO_BLOCK_SIZE; row++) {
63
//              offset = bmpsize-3*bmpheader->width*(row + 1 + mb_row*MATRIX_SIZE)+MATRIX_SIZE*3*mb_col;
64
//              memcpy(pixelmatrix[row], bmpimage + offset, MATRIX_SIZE*3);
65
                offset = bmpsize-3*bmpheader->width*(row + 1 + mb_row*MACRO_BLOCK_SIZE)+MACRO_BLOCK_SIZE*3*mb_col;
66
                memcpy(buffer, bmpimage + offset, MACRO_BLOCK_SIZE*3);
67
                        for(col = 0; col < MACRO_BLOCK_SIZE*3; col++) {
68
                                pixelmatrix[row][col] = buffer[col]- 128;
69
                        }
70
        }
71
 
72
}
73
 
74
void put_char(unsigned char c) {
75
 
76
        sysace_fwrite(&c, 1, 1, outfile);
77
 
78
}
79
 
80
unsigned long htonl(unsigned long x) {
81
 
82
        return ((((x)&0xff000000)>>24) | (((x)&0x00ff0000)>>8) | (((x)&0x0000ff00)<<8) | (((x)&0x000000ff)<<24));
83
 
84
}
85
 
86
unsigned short hton(unsigned short x) {
87
 
88
        return ((((x) & 0xff00)>>8) | (((x) &0x00ff)<<8));
89
 
90
}
91
 
92
 
93
int main()
94
{
95
  SYSACE_FILE* outfile2;
96
 
97
  int i;
98
  unsigned int col, cols, row, rows;
99
  int compression;
100
  int sample;
101
 
102
  compression = 0;
103
 
104
//  bmpimage=(unsigned char*)0x70000000;
105
  bmpimage=(unsigned char*)0x30000000;
106
  bmpsize=0;
107
 
108
  xil_printf("\r\nBMP2JPG Code Compiled at %s %s\r\n", __DATE__, __TIME__);
109
 
110
  bmpheader=&_bmpheader;
111
 
112
  if ((infile = sysace_fopen("image01.bmp", "r")) == NULL) {
113
        ejpgl_error(eOPENINPUT_FILE, 0);
114
        }
115
 
116
  bmpsize = sysace_fread(bmpimage, 1, 1024*1024, infile);
117
  xil_printf("bmpsize %d\r\n", bmpsize);
118
 
119
#if 0
120
  bmpsize = sysace_fread(bmpimage, 1, 65536, infile);
121
  xil_printf("bmpsize %d\r\n", bmpsize);
122
  if (bmpsize==65536) {
123
        ejpgl_error(eLARGE_INPUTFILE, 0);
124
        }
125
#endif
126
 
127
/*  if ((outfile2 = sysace_fopen("image01b.bmp", "w")) == NULL) {   // see if the BMP file is correctly read into memory
128
        ejpgl_error(eOPENOUTPUT_FILE, 0);
129
        }
130
 
131
  sysace_fwrite(bmpimage, 1, bmpsize, outfile2);
132
  sysace_fclose(outfile2); */
133
 
134
  if (getbmpheader(infile,bmpheader) == 0) { //File is a valid BMP
135
        ejpgl_error(eINVALID_BMP, 0);
136
        }
137
 
138
  xil_printf("Image width: %d pixels\r\n", bmpheader->width);
139
  xil_printf("Image height: %d pixels\r\n", bmpheader->height);
140
 
141
  rows = bmpheader->height>>4; // 3;
142
  cols = bmpheader->width>>4; // 3;
143
 
144
  if ((outfile = sysace_fopen("image01.jpg", "w")) == NULL) {
145
        ejpgl_error(eOPENOUTPUT_FILE, 0);
146
        }
147
 
148
  writejpegheader(outfile,bmpheader);
149
 
150
  dct_init_start();
151
  zzq_encode_init_start(compression);
152
  vlc_init_start();
153
 
154
   for (row = 0; row < rows; row++) {
155
        for (col = 0; col < cols; col++) {
156
                get_MB(row, col, pixelmatrix);
157
 
158
// dct->zz/q->vlc               dct call zz/q call vlc
159
 
160
/*              RGB2Y_matrix(pixelmatrix, pmatrix2);
161
                dct(pmatrix2, 0);
162
                RGB2Cr_matrix(pixelmatrix, pmatrix2);
163
                dct(pmatrix2, 1);
164
                RGB2Cb_matrix(pixelmatrix, pmatrix2);
165
                dct(pmatrix2, 2); */
166
                                        for(sample=0;sample<5;sample++) {
167
                                                if(sample<4) {
168
                                                        RGB2YCrCb(pixelmatrix,YMatrix,CrMatrix,CbMatrix,sample);
169
                                                        //Y-encoding
170
                                                        dct(YMatrix,0);
171
                                                } else {
172
                                                        //Cr-encoding
173
                                                        dct(CrMatrix,1);
174
                                                        //Cb-encoding
175
                                                        dct(CbMatrix,2);
176
                                                }
177
                                        }
178
 
179
        }
180
   }
181
 
182
   HuffmanEncodeFinishSend();
183
   xil_printf("\r\nProcessed %d %dx%d-blocks.\r\n",(row-1)*cols+col,MATRIX_SIZE,MATRIX_SIZE);
184
   writejpegfooter(outfile);
185
 
186
 
187
   sysace_fclose(outfile);
188
   sysace_fclose(infile);
189
   return 0;
190
 
191
}
192
 
193
int ejpgl_error(int errno, void* remark) {
194
 
195
        xil_printf("--> Error %d\r\n", errno);
196
        exit(1);
197
 
198
}
199
 
200
int getbmpheader(FILE * file, INFOHEADER *header)
201
{
202
       memcpy(header, bmpimage+14, sizeof(INFOHEADER));
203
 
204
#if defined(__MICROBLAZE)      // for Big Endian processors
205
 
206
        header->size = htonl(header->size);
207
        header->width = htonl(header->width);
208
        header->height = htonl(header->height);
209
        header->planes = hton(header->planes);
210
        header->bits = hton(header->bits);
211
        header->compression = htonl(header->compression);
212
        header->imagesize = htonl(header->imagesize);
213
        header->xresolution = htonl(header->xresolution);
214
        header->yresolution= htonl(header->yresolution);
215
        header->ncolours= htonl(header->ncolours);
216
        header->importantcolours= htonl(header->importantcolours);
217
 
218
#endif
219
 
220
        return 1;
221
 
222
}
223
 
224
void writejpegheader(FILE * file, INFOHEADER *header)
225
{
226
        JPEGHEADER *jpegheader;
227
        unsigned int headersize, huffmantablesize, previoussize;
228
        unsigned char QTcount, i, j, components, id, huffmantablecount;
229
        unsigned short length, headerlength;
230
 
231
        //Number of Quatization Tables
232
        QTcount = 2;
233
        headerlength = 12; //12 bytes are needed for the markers
234
        huffmantablecount = 4;  //2 AC and 2 DC tables
235
        huffmantablesize = 0;
236
        jpegheader = &_jpegheader;//(JPEGHEADER *)malloc(550);
237
 
238
        jpegheader->SOIMarker[0] = 0xff;
239
        jpegheader->SOIMarker[1] = 0xd8;
240
 
241
        //APP0 segment
242
        jpegheader->app0.APP0Marker[0] = 0xff;
243
        jpegheader->app0.APP0Marker[1] = 0xe0;
244
 
245
        headerlength += 16; //APP0 marker is always 16 bytes long
246
        jpegheader->app0.Length[0] = 0x00;
247
        jpegheader->app0.Length[1] = 0x10;
248
        jpegheader->app0.Identifier[0] = 0x4a;
249
        jpegheader->app0.Identifier[1] = 0x46;
250
        jpegheader->app0.Identifier[2] = 0x49;
251
        jpegheader->app0.Identifier[3] = 0x46;
252
        jpegheader->app0.Identifier[4] = 0x00;
253
        jpegheader->app0.Version[0] = 0x01;
254
        jpegheader->app0.Version[1] = 0x00;
255
        jpegheader->app0.Units = 0x00;
256
        jpegheader->app0.XDensity[0] = 0x00;
257
        jpegheader->app0.XDensity[1] = 0x01;
258
        jpegheader->app0.YDensity[0] = 0x00;
259
                jpegheader->app0.YDensity[1] = 0x01;
260
        jpegheader->app0.ThumbWidth = 0x00;
261
        jpegheader->app0.ThumbHeight = 0x00;
262
 
263
        //Quantization Table Segment
264
        jpegheader->qt.QTMarker[0] = 0xff;
265
        jpegheader->qt.QTMarker[1] = 0xdb;
266
        length = (QTcount<<6) + QTcount + 2;
267
        headerlength += length;
268
        jpegheader->qt.Length[0] = (length & 0xff00)>>8;
269
        jpegheader->qt.Length[1] = length & 0xff;
270
       // jpegheader->qt.QTInfo = 0x00; // index = 0, precision = 0
271
        //write Quantization table to header
272
        i = 0;
273
    /*     jpegheader->qt.QTInfo[0] = 0;
274
        for(i=0;i<64;i++) {
275
                jpegheader->qt.QTInfo[i+1] = qtable[i];
276
        }
277
        jpegheader->qt.QTInfo[65] = 1;
278
        for(i=0;i<64;i++) {
279
                jpegheader->qt.QTInfo[i+66] = qtable[i];
280
        }  */
281
        for (id=0; id<QTcount; id++) {
282
                jpegheader->qt.QTInfo[(id<<6)+id] = id;
283
                for(i=0;i<64;i++) {
284
                        jpegheader->qt.QTInfo[i+1+id+(id<<6)] = qtable[i];
285
                }
286
        }
287
 
288
        //Start of Frame segment
289
        jpegheader->sof0.SOF0Marker[0] = 0xff;
290
        jpegheader->sof0.SOF0Marker[1] = 0xc0;
291
        if(header->bits == 8) {
292
                components = 0x01;
293
        }
294
        else {
295
                components = 0x03;
296
                }
297
        length = 8 + 3*components;
298
        headerlength += length;
299
        jpegheader->sof0.Length[0] = (length & 0xff00) >> 8;
300
        jpegheader->sof0.Length[1] = length & 0xff;
301
        jpegheader->sof0.DataPrecision = 0x08;
302
        jpegheader->sof0.ImageHeight[0] = (header->height & 0xff00) >> 8;
303
        jpegheader->sof0.ImageHeight[1] = header->height & 0xff;
304
        jpegheader->sof0.ImageWidth[0] = (header->width & 0xff00) >> 8;
305
        jpegheader->sof0.ImageWidth[1] = header->width & 0xff;
306
        jpegheader->sof0.Components  = components;
307
        for (i=0; i < components; i++) {
308
                        jpegheader->sof0.ComponentInfo[i][0] = i+1; //color component
309
                        if(i==0) {
310
                                jpegheader->sof0.ComponentInfo[i][1] = 0x22; //4:2:0 subsampling
311
                        } else {
312
                                jpegheader->sof0.ComponentInfo[i][1] = 0x11; //4:2:0 subsampling
313
                        }
314
            jpegheader->sof0.ComponentInfo[i][2] = (i==0)? 0x00 : 0x01; //quantization table ID
315
                }
316
        //Start of Huffman Table Segment
317
 
318
        jpegheader->ht.HTMarker[0] = 0xff;
319
        jpegheader->ht.HTMarker[1] = 0xc4;
320
 
321
        //Set dummy HT segment length
322
        length = 0;//tablecount*17;
323
        jpegheader->ht.Length[0] = (length & 0xff00) >> 8;
324
        jpegheader->ht.Length[1] = length & 0xff;
325
        previoussize = 0;
326
        for (id=0; id < huffmantablecount; id++) {
327
            huffmantablesize = 0;
328
            switch (id) {
329
            case 0 : jpegheader->ht.HuffmanInfo[previoussize] = 0x00;
330
                     break;
331
            case 1 : jpegheader->ht.HuffmanInfo[previoussize] = 0x10;
332
                     break;
333
            case 2 : jpegheader->ht.HuffmanInfo[previoussize] = 0x01;
334
                     break;
335
            case 3 : jpegheader->ht.HuffmanInfo[previoussize] = 0x11;
336
                     break;
337
                        }
338
            for (i=1; i <= 16; i++) {
339
                    jpegheader->ht.HuffmanInfo[i+previoussize] =  huffmancount[id][i-1];
340
                    huffmantablesize += huffmancount[id][i-1];
341
            }
342
 
343
            for (i=0; i < huffmantablesize; i++) {
344
                    jpegheader->ht.HuffmanInfo[i+previoussize+17] = (id%2 == 1)? huffACvalues[i] : huffDCvalues[i];
345
            }
346
            previoussize += huffmantablesize + 17;
347
        }
348
        //Set real HT segment length
349
        length = 2+previoussize;
350
        headerlength += length;
351
        jpegheader->ht.Length[0] = (length & 0xff00) >> 8;
352
        jpegheader->ht.Length[1] = length & 0xff;
353
        //Reset marker segment
354
      /*  jpegheader->dri.DRIMarker[0] = 0xff;
355
        jpegheader->dri.DRIMarker[1] = 0xdd;
356
        jpegheader->dri.Length[0] = 0x00;
357
        jpegheader->dri.Length[1] = 0x04;
358
        jpegheader->dri.RestartInteral[0] = 0x00; //no restart markers
359
        jpegheader->dri.RestartInteral[1] = 0x00; //no restart markers
360
        headerlength  += 6;  //length of DRI segment
361
       */
362
        //Start of Scan Header Segment
363
        jpegheader->sos.SOSMarker[0] = 0xff;
364
        jpegheader->sos.SOSMarker[1] = 0xda;
365
        length = 6 + (components<<1);
366
        headerlength += length;
367
        jpegheader->sos.Length[0] = (length & 0xff00) >> 8;
368
        jpegheader->sos.Length[1] =  length & 0xff;
369
        jpegheader->sos.ComponentCount = components; //number of color components in the image
370
        jpegheader->sos.Component[0][0] = 0x01; //Y component
371
        jpegheader->sos.Component[0][1] = 0x00; //indexes of huffman tables for Y-component
372
        if (components == 0x03) {
373
                jpegheader->sos.Component[1][0] = 0x02; //the CB component
374
                                jpegheader->sos.Component[1][1] = 0x11; //indexes of huffman tables for CB-component
375
                jpegheader->sos.Component[2][0] = 0x03; //The CR component
376
                jpegheader->sos.Component[2][1] = 0x11; //indexes of huffman tables for CR-component
377
        }
378
        //following bytes are ignored since progressive scan is not to be implemented
379
        jpegheader->sos.Ignore[0] = 0x00;
380
        jpegheader->sos.Ignore[1] = 0x3f;
381
        jpegheader->sos.Ignore[2] = 0x00;
382
 
383
        sysace_fwrite(jpegheader, 1, headerlength, file);
384
        xil_printf("jpeg header size %x\r\n", headerlength);
385
 
386
}
387
 
388
void writejpegfooter(FILE * file)
389
{
390
        unsigned char footer[2];
391
        footer[0] = 0xff;
392
        footer[1] = 0xd9;
393
//        fseek(file,0,SEEK_END);
394
        sysace_fwrite(footer,sizeof(footer),1,file);
395
}
396
 
397
 
398
 
399
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.