OpenCores
URL https://opencores.org/ocsvn/mb-jpeg/mb-jpeg/trunk

Subversion Repositories mb-jpeg

[/] [mb-jpeg/] [tags/] [STEP2_2b/] [bmp2jpg_mb/] [bmp2jpg_mb.c] - Blame information for rev 68

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

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

powered by: WebSVN 2.1.0

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