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

Subversion Repositories mb-jpeg

[/] [mb-jpeg/] [tags/] [STEP2_2b/] [encoder/] [io.c] - Blame information for rev 66

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 33 quickwayne
//---------------------------------------------------------------------------
2
#include <stdio.h>
3
#ifdef __MICROBLAZE
4
#include "xup2pro.h"
5
#endif
6
 
7
#include "io.h"
8
#pragma hdrstop
9
//---------------------------------------------------------------------------
10
#pragma argsused
11
 
12
JPEGHEADER _jpegheader;
13
 
14
unsigned char qtable[64] = {16, 8, 8, 16, 12, 8, 16, 16, 16, 16, 16, 16, 16, 16,
15
16, 32, 32, 16, 16, 16, 16, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64,
16
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 64, 64, 128, 128, 128, 128, 128, 64, 64,
17
128, 128, 128, 128, 128, 64, 128, 128, 128};
18
 
19
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
20
                                    {0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D},   //standard AC table count
21
                                    {0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},  //standard DC table count
22
                                    {0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D}};  //standard AC table count
23
 
24
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};
25
unsigned char huffACvalues[162] = {0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
26
                                0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82,
27
                                0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
28
                                0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64,
29
                                0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87,
30
                                0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
31
                                0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
32
                                0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
33
                                0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA};
34
 
35
                                //old huffman table, and propably  a incorrect one.
36
                                /*{0x00, 0x01, 0x04, 0x0A, 0x0B, 0x0C, 0x1A, 0x1B, 0x1C, 0x3A, 0x3B, 0x78, 0x79, 0x7A, 0x7B, 0xF8, 0xF9, 0xFA, 0xF6,
37
                                0xF7, 0xF8, 0xF9, 0xFA, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xF6, 0xF7, 0xF8, 0xF9, 0xF4, 0xF5, 0xF6, 0xF7, 0xC0, 0x82,
38
                                0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
39
                                0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
40
                                0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB,
41
                                0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
42
                                0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1,
43
                                0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4,
44
                                0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE};*/
45
 
46
int getbmpheader(FILE * file, INFOHEADER *header)
47
{
48
        int retval;
49
#ifdef __MICROBLAZE       
50
 
51
//      sysace_fread(header, 1, 14, file);
52
//       retval = sysace_fread(header, 1, sizeof(INFOHEADER), file);
53
 
54
       memcpy(header, bmpimage+14, sizeof(INFOHEADER));
55
 
56
        header->size = htonl(header->size);
57
        header->width = htonl(header->width);
58
        header->height = htonl(header->height);
59
        header->planes = hton(header->planes);
60
        header->bits = hton(header->bits);
61
        header->compression = htonl(header->compression);
62
        header->imagesize = htonl(header->imagesize);
63
        header->xresolution = htonl(header->xresolution);
64
        header->yresolution= htonl(header->yresolution);
65
        header->ncolours= htonl(header->ncolours);
66
        header->importantcolours= htonl(header->importantcolours);
67
 
68
        return 1;
69
 
70
#else           
71
        fseek(file,14,SEEK_SET);
72
        retval = fread(header, sizeof(INFOHEADER), 1, file);
73
#endif          
74
 
75
        return retval;
76
}
77
 /*
78
int getjpegheader(FILE * file, JPEGHEADER *header)
79
{
80
        unsigned int retval;
81
 
82
        return retval;
83
}
84
 
85
void writebmpheader(FILE * file, BMPHEADER *header)
86
{
87
        //write header
88
 
89
        //fill file with 0's
90
        fwrite(0, 1, header->info.width*header->info.height, file);
91
}
92
*/
93
void writejpegheader(FILE * file, INFOHEADER *header)
94
{
95
        JPEGHEADER *jpegheader;
96
        unsigned int headersize, huffmantablesize, previoussize;
97
        unsigned char QTcount, i, j, components, id, huffmantablecount;
98
        unsigned short length, headerlength;
99
 
100
        //Number of Quatization Tables
101
        QTcount = 2;
102
        headerlength = 12; //12 bytes are needed for the markers
103
        huffmantablecount = 4;      //          // 2 AC and 2 DC tables
104
        huffmantablesize = 0;
105
        jpegheader = &_jpegheader;//(JPEGHEADER *)malloc(550);
106
 
107
        jpegheader->SOIMarker[0] = 0xff;
108
        jpegheader->SOIMarker[1] = 0xd8;
109
 
110
        //APP0 segment
111
        jpegheader->app0.APP0Marker[0] = 0xff;
112
        jpegheader->app0.APP0Marker[1] = 0xe0;
113
 
114
        headerlength += 16; //APP0 marker is always 16 bytes long
115
        jpegheader->app0.Length[0] = 0x00;
116
        jpegheader->app0.Length[1] = 0x10;
117
        jpegheader->app0.Identifier[0] = 0x4a;
118
        jpegheader->app0.Identifier[1] = 0x46;
119
        jpegheader->app0.Identifier[2] = 0x49;
120
        jpegheader->app0.Identifier[3] = 0x46;
121
        jpegheader->app0.Identifier[4] = 0x00;
122
        jpegheader->app0.Version[0] = 0x01;
123
        jpegheader->app0.Version[1] = 0x00;
124
        jpegheader->app0.Units = 0x00;
125
        jpegheader->app0.XDensity[0] = 0x00;
126
        jpegheader->app0.XDensity[1] = 0x01;
127
        jpegheader->app0.YDensity[0] = 0x00;
128
        jpegheader->app0.YDensity[1] = 0x01;
129
        jpegheader->app0.ThumbWidth = 0x00;
130
        jpegheader->app0.ThumbHeight = 0x00;
131
 
132
        //Quantization Table Segment
133
        jpegheader->qt.QTMarker[0] = 0xff;
134
        jpegheader->qt.QTMarker[1] = 0xdb;
135
        length = (QTcount<<6) + QTcount + 2;
136
        headerlength += length;
137
        jpegheader->qt.Length[0] = (length & 0xff00)>>8;
138
        jpegheader->qt.Length[1] = length & 0xff;
139
       // jpegheader->qt.QTInfo = 0x00; // index = 0, precision = 0
140
        //write Quantization table to header
141
        i = 0;
142
    /*     jpegheader->qt.QTInfo[0] = 0;
143
        for(i=0;i<64;i++) {
144
                jpegheader->qt.QTInfo[i+1] = qtable[i];
145
        }
146
        jpegheader->qt.QTInfo[65] = 1;
147
        for(i=0;i<64;i++) {
148
                jpegheader->qt.QTInfo[i+66] = qtable[i];
149
        }  */
150
        for (id=0; id<QTcount; id++) {
151
                jpegheader->qt.QTInfo[(id<<6)+id] = id;
152
                for(i=0;i<64;i++) {
153
                        jpegheader->qt.QTInfo[i+1+id+(id<<6)] = qtable[i];
154
                }
155
        }
156
 
157
        //Start of Frame segment
158
        jpegheader->sof0.SOF0Marker[0] = 0xff;
159
        jpegheader->sof0.SOF0Marker[1] = 0xc0;
160
        if(header->bits == 8) {
161
                components = 0x01;
162
        }
163
        else {
164
                components = 0x03;
165
        }
166
        length = 8 + 3*components;
167
        headerlength += length;
168
        jpegheader->sof0.Length[0] = (length & 0xff00) >> 8;
169
        jpegheader->sof0.Length[1] = length & 0xff;
170
        jpegheader->sof0.DataPrecision = 0x08;
171
        jpegheader->sof0.ImageHeight[0] = (header->height & 0xff00) >> 8;
172
        jpegheader->sof0.ImageHeight[1] = header->height & 0xff;
173
        jpegheader->sof0.ImageWidth[0] = (header->width & 0xff00) >> 8;
174
        jpegheader->sof0.ImageWidth[1] = header->width & 0xff;
175
#ifdef __MICROBLAZE
176
        xil_printf("--->%d %d %d %d\r\n", jpegheader->sof0.ImageHeight[0], jpegheader->sof0.ImageHeight[1], jpegheader->sof0.ImageWidth[0], jpegheader->sof0.ImageWidth[1]);
177
#else
178
        printf("--->%d %d %d %d\r\n", jpegheader->sof0.ImageHeight[0], jpegheader->sof0.ImageHeight[1], jpegheader->sof0.ImageWidth[0], jpegheader->sof0.ImageWidth[1]);
179
#endif
180
        jpegheader->sof0.Components  = components;
181
        for (i=0; i < components; i++) {
182
                jpegheader->sof0.ComponentInfo[i][0] = i+1; //color component
183
                jpegheader->sof0.ComponentInfo[i][1] = 0x11; //no sampling has been done
184
                jpegheader->sof0.ComponentInfo[i][2] = (i==0)? 0x00 : 0x01; //quantization table ID
185
        }
186
        //Start of Huffman Table Segment
187
 
188
        jpegheader->ht.HTMarker[0] = 0xff;
189
        jpegheader->ht.HTMarker[1] = 0xc4;
190
 
191
        //Set dummy HT segment length
192
        length = 0;//tablecount*17;
193
        jpegheader->ht.Length[0] = (length & 0xff00) >> 8;
194
        jpegheader->ht.Length[1] = length & 0xff;
195
        previoussize = 0;
196
        for (id=0; id < huffmantablecount; id++) {
197
            huffmantablesize = 0;
198
            switch (id) {
199
            case 0 : jpegheader->ht.HuffmanInfo[previoussize] = 0x00;
200
                     break;
201
            case 1 : jpegheader->ht.HuffmanInfo[previoussize] = 0x10;
202
                     break;
203
            case 2 : jpegheader->ht.HuffmanInfo[previoussize] = 0x01;
204
                     break;
205
            case 3 : jpegheader->ht.HuffmanInfo[previoussize] = 0x11;
206
                     break;
207
            }
208
            for (i=1; i <= 16; i++) {
209
                    jpegheader->ht.HuffmanInfo[i+previoussize] =  huffmancount[id][i-1];
210
                    huffmantablesize += huffmancount[id][i-1];
211
            }
212
 
213
            for (i=0; i < huffmantablesize; i++) {
214
                    jpegheader->ht.HuffmanInfo[i+previoussize+17] = (id%2 == 1)? huffACvalues[i] : huffDCvalues[i];
215
            }
216
            previoussize += huffmantablesize + 17;
217
        }
218
        //Set real HT segment length
219
        length = 2+previoussize;
220
        headerlength += length;
221
        jpegheader->ht.Length[0] = (length & 0xff00) >> 8;
222
        jpegheader->ht.Length[1] = length & 0xff;
223
        //Reset marker segment
224
      /*  jpegheader->dri.DRIMarker[0] = 0xff;
225
        jpegheader->dri.DRIMarker[1] = 0xdd;
226
        jpegheader->dri.Length[0] = 0x00;
227
        jpegheader->dri.Length[1] = 0x04;
228
        jpegheader->dri.RestartInteral[0] = 0x00; //no restart markers
229
        jpegheader->dri.RestartInteral[1] = 0x00; //no restart markers
230
        headerlength  += 6;  //length of DRI segment
231
       */
232
        //Start of Scan Header Segment
233
        jpegheader->sos.SOSMarker[0] = 0xff;
234
        jpegheader->sos.SOSMarker[1] = 0xda;
235
        length = 6 + (components<<1);
236
        headerlength += length;
237
        jpegheader->sos.Length[0] = (length & 0xff00) >> 8;
238
        jpegheader->sos.Length[1] =  length & 0xff;
239
        jpegheader->sos.ComponentCount = components; //number of color components in the image
240
        jpegheader->sos.Component[0][0] = 0x01; //Y component
241
        jpegheader->sos.Component[0][1] = 0x00; //indexes of huffman tables for Y-component
242
        if (components == 0x03) {
243
                jpegheader->sos.Component[1][0] = 0x02; //the CB component
244
                jpegheader->sos.Component[1][1] = 0x11; //indexes of huffman tables for CB-component
245
                jpegheader->sos.Component[2][0] = 0x03; //The CR component
246
                jpegheader->sos.Component[2][1] = 0x11; //indexes of huffman tables for CR-component
247
        }
248
        //following bytes are ignored since progressive scan is not to be implemented
249
        jpegheader->sos.Ignore[0] = 0x00;
250
        jpegheader->sos.Ignore[1] = 0x3f;
251
        jpegheader->sos.Ignore[2] = 0x00;
252
 
253
#ifdef __MICROBLAZE             
254
        sysace_fwrite(jpegheader, 1, headerlength, file);
255
        xil_printf("jpeg header size %x\r\n", headerlength);
256
#else
257
        fwrite(jpegheader,headerlength,1,file);
258
        printf("jpeg header size %x\r\n", headerlength);
259
#endif
260
 
261
}
262
 
263
void writejpegfooter(FILE * file)
264
{
265
        unsigned char footer[2];
266
        footer[0] = 0xff;
267
        footer[1] = 0xd9;
268
#ifdef __MICROBLAZE
269
        sysace_fwrite(footer, 1, sizeof(footer), file);
270
#else
271
//        fseek(file,0,SEEK_END);
272
        fwrite(footer,sizeof(footer),1,file);
273
#endif          
274
}
275
 
276
static  unsigned char buffer[MATRIX_SIZE*3];
277
static int __count2=0;
278
 
279
void readbmpfile(FILE * file, signed char pixelmatrix[MATRIX_SIZE][MATRIX_SIZE*3], unsigned int mrow, unsigned int mcol, INFOHEADER * header)
280
{
281
        unsigned int row, col;
282
        int offset;
283
 
284
        for(row = 0;row < MATRIX_SIZE; row++) {
285
                //Find first point of row in the matrix to be read.
286
#ifdef __MICROBLAZE                
287
//               sysace_fread(buffer, 1, MATRIX_SIZE*3, file);
288
                offset = bmpsize-3*header->width*(row + 1 + mrow*MATRIX_SIZE)+MATRIX_SIZE*3*mcol;
289
                memcpy(buffer, bmpimage + offset, MATRIX_SIZE*3);
290
#else
291
                fseek(file,-(3*header->width*(row + 1 + mrow*MATRIX_SIZE)-(MATRIX_SIZE*3)*mcol),SEEK_END);
292
                //Read row from matrix
293
                fread(buffer, 1, MATRIX_SIZE*3, file);
294
                //copy row into pixelmatrix
295
#endif                
296
                for(col = 0; col < MATRIX_SIZE*3; col++) {
297
                        pixelmatrix[row][col] = buffer[col]- 128;
298
#if 0                                           
299
#ifdef __MICROBLAZE
300
       if (__count2<4*24*8) {
301
        xil_printf("%x ", pixelmatrix[row][col]);
302
        __count2++;
303
        if ((__count2&0x0f)==0) xil_printf("\r\n");
304
        }
305
#else
306
       if (__count2<4*24*8) {
307
        printf(" %x ", pixelmatrix[row][col]);
308
        __count2++;
309
        if ((__count2&0x0f)==0) printf("\r\n");
310
        }
311
#endif
312
#endif                                          
313
                }
314
        }
315
 }
316
/*USED VALUES OF ROY's CODE THIS MIGHT LEAD TO PROBLEMS*/
317
signed char RGB2Y (int r, int g, int b) {
318
   return ((66*r + 129*g + 25*b + 128)>>8)+128;
319
}
320
/*USED VALUES OF ROY's CODE THIS MIGHT LEAD TO PROBLEMS*/
321
signed char RGB2Cr (int r, int g, int b) {
322
   return ((-38*r - 74*g + 112*b + 128)>>8)+128;
323
}
324
/*USED VALUES OF ROY's CODE THIS MIGHT LEAD TO PROBLEMS*/
325
signed char RGB2Cb (int r, int g, int b) {
326
   return ((112*r - 94*g - 18*b + 128)>>8)+128;
327
}
328
 
329
static int __count1=120;
330
 
331
void RGB2YCrCb(signed char pixelmatrix[MATRIX_SIZE][MATRIX_SIZE*3],signed char YMatrix[MATRIX_SIZE][MATRIX_SIZE],signed char CrMatrix[MATRIX_SIZE][MATRIX_SIZE],signed char CbMatrix[MATRIX_SIZE][MATRIX_SIZE])
332
{
333
        unsigned int row, col;
334
        for(row = 0;row < MATRIX_SIZE; row++) {
335
                for(col = 0; col < MATRIX_SIZE; col++) {
336
                        YMatrix[row][col] = RGB2Y(pixelmatrix[row][col*3+2],pixelmatrix[row][col*3+1],pixelmatrix[row][col*3]) - 128;
337
                        CrMatrix[row][col] = RGB2Cr(pixelmatrix[row][col*3+2],pixelmatrix[row][col*3+1],pixelmatrix[row][col*3]) - 128;
338
                        CbMatrix[row][col] = RGB2Cb(pixelmatrix[row][col*3+2],pixelmatrix[row][col*3+1],pixelmatrix[row][col*3]) - 128;
339
#if 0                                           
340
#ifdef __MICROBLAZE
341
       if (__count1<64) {
342
        xil_printf("-------------->%x %x %x\r\n", YMatrix[row][col], CrMatrix[row][col], CbMatrix[row][col]);
343
        __count1++;
344
        }
345
#else
346
       if (__count1<64) {
347
        printf("-------------->%x %x %x\r\n", YMatrix[row][col], CrMatrix[row][col], CbMatrix[row][col]);
348
        __count1++;
349
        }
350
#endif
351
#endif
352
                }
353
        }
354
}
355
/*
356
void readjpegfile(FILE * file, unsigned char bitstream[])
357
{
358
}
359
 
360
 
361
void writebmpfile(FILE * file, unsigned char pixelmatrix[MATRIX_SIZE][MATRIX_SIZE], unsigned int mrow, unsigned int mcol, unsigned int width)
362
{
363
        unsigned int row;
364
        for(row = 0;row < MATRIX_SIZE; row++) {
365
                //Find first point of row in the matrix to be read.
366
                fseek(file,-(width*(row + 1 + mrow*MATRIX_SIZE)-(MATRIX_SIZE*mcol)),SEEK_END);
367
                //Read row from matrix
368
                fwrite(pixelmatrix[row], 1, MATRIX_SIZE, file);
369
        }
370
}
371
 
372
void writejpegfile(FILE * file, unsigned char bitstream[])
373
{
374
}    */

powered by: WebSVN 2.1.0

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