URL
https://opencores.org/ocsvn/mb-jpeg/mb-jpeg/trunk
Subversion Repositories mb-jpeg
Compare Revisions
- This comparison shows the changes necessary to convert path
/mb-jpeg/tags/STEP2_2b/mb-bmp2jpg
- from Rev 61 to Rev 66
- ↔ Reverse comparison
Rev 61 → Rev 66
/xupv2p.c
0,0 → 1,181
#ifdef __XUPV2P |
|
// Microblaze related declaration |
|
#include <xparameters.h> |
#include <sysace_stdio.h> |
|
#include "xio.h" |
|
#include "ejpgl.h" |
|
#define __MICROBLAZE |
#define __BIGENDIAN |
|
// XUPV2P board related declaration |
|
#define BMP_ADDRESS 0x30000000 |
#define BMP_MAXSIZE 4*1024*1024 |
|
|
SYSACE_FILE *infile; |
SYSACE_FILE *outfile; |
|
char* bmpimage; |
int bmpsize; |
|
INFOHEADER _bmpheader; |
INFOHEADER *bmpheader; |
JPEGHEADER _jpegheader; |
|
int getbmpheader(INFOHEADER *header); |
void writejpegfooter(); |
|
|
unsigned long htonl(unsigned long x) { |
|
return ((((x)&0xff000000)>>24) | (((x)&0x00ff0000)>>8) | (((x)&0x0000ff00)<<8) | (((x)&0x000000ff)<<24)); |
|
} |
|
unsigned short hton(unsigned short x) { |
|
return ((((x) & 0xff00)>>8) | (((x) &0x00ff)<<8)); |
|
} |
|
|
int openBMPJPG(int argc, char* bmpfilename, char* jpgfilename) { |
int jpegheadersize; |
|
bmpimage=(unsigned char*)BMP_ADDRESS; |
bmpsize=0; |
|
xil_printf("\r\nBMP2JPG Code Compiled at %s %s\r\n", __DATE__, __TIME__); |
|
bmpfilename = "image01.bmp"; // argc argv is not accepted on XUPV2P yet |
jpgfilename = "image01.jpg"; |
|
bmpheader=&_bmpheader; |
|
if ((infile = sysace_fopen(bmpfilename, "r")) == NULL) { // not "rb" |
xil_printf("\n\r%s is not a valid BMP-file",bmpfilename); |
exit(0); |
} |
|
bmpsize = sysace_fread(bmpimage, 1, BMP_MAXSIZE, infile); |
xil_printf("bmpsize %d\r\n", bmpsize); |
if (bmpsize==BMP_MAXSIZE) { |
xil_printf("\n\r%s is too large",bmpfilename); |
exit(0); |
} |
|
|
if (getbmpheader(bmpheader) == 0) { //File is a valid BMP |
xil_printf("\r\n%s is not a valid BMP-file",bmpfilename); |
exit(0); |
} |
|
xil_printf("Image width: %d pixels\r\n", bmpheader->width); |
xil_printf("Image height: %d pixels\r\n", bmpheader->height); |
|
outfile = sysace_fopen(jpgfilename, "w"); // not "wb" |
if (outfile == NULL) { |
xil_printf("\r\nerror in writing jpg header"); |
exit(0); |
} |
|
jpegheadersize = writejpegheader(bmpheader, &_jpegheader); |
if (jpegheadersize == 0) return 0; |
|
sysace_fwrite(&_jpegheader,jpegheadersize,1,outfile); |
|
return 1; |
|
} |
|
int closeBMPJPG() { |
unsigned int col, cols, row, rows; |
|
rows = bmpheader->height>>4; |
cols = bmpheader->width>>4; |
xil_printf("\r\nProcessed more than %d %dx%d-blocks.",(row-1)*cols,MATRIX_SIZE,MATRIX_SIZE); // +col |
|
writejpegfooter(); |
|
sysace_fclose(outfile); |
sysace_fclose(infile); |
|
return 0; |
|
} |
|
static unsigned char buffer[MACRO_BLOCK_SIZE*3]; // move array on main memory |
|
void get_MB(int mb_row, int mb_col, signed char pixelmatrix[MACRO_BLOCK_SIZE][MACRO_BLOCK_SIZE*3]) { |
unsigned int row, col; |
int offset; |
|
for(row = 0;row < MACRO_BLOCK_SIZE; row++) { |
// offset = bmpsize-3*bmpheader->width*(row + 1 + mb_row*MATRIX_SIZE)+MATRIX_SIZE*3*mb_col; |
// memcpy(pixelmatrix[row], bmpimage + offset, MATRIX_SIZE*3); |
offset = bmpsize-3*bmpheader->width*(row + 1 + mb_row*MACRO_BLOCK_SIZE)+MACRO_BLOCK_SIZE*3*mb_col; |
memcpy(buffer, bmpimage + offset, MACRO_BLOCK_SIZE*3); |
for(col = 0; col < MACRO_BLOCK_SIZE*3; col++) { |
pixelmatrix[row][col] = buffer[col]- 128; |
} |
} |
|
} |
|
void put_char(unsigned char c) { |
|
sysace_fwrite(&c, 1, 1, outfile); |
|
} |
|
|
int getbmpheader(INFOHEADER *header) |
{ |
memcpy(header, bmpimage+14, sizeof(INFOHEADER)); |
|
#if defined(__BIGENDIAN) // for Big Endian processors |
|
header->size = htonl(header->size); |
header->width = htonl(header->width); |
header->height = htonl(header->height); |
header->planes = hton(header->planes); |
header->bits = hton(header->bits); |
header->compression = htonl(header->compression); |
header->imagesize = htonl(header->imagesize); |
header->xresolution = htonl(header->xresolution); |
header->yresolution= htonl(header->yresolution); |
header->ncolours= htonl(header->ncolours); |
header->importantcolours= htonl(header->importantcolours); |
|
#endif |
|
return 1; |
|
} |
|
void writejpegfooter() |
{ |
unsigned char footer[2]; |
footer[0] = 0xff; |
footer[1] = 0xd9; |
// fseek(file,0,SEEK_END); |
sysace_fwrite(footer,sizeof(footer),1, outfile); |
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
/bmp2jpg.c
0,0 → 1,50
#include <stdio.h> |
#include "ejpgl.h" |
|
extern INFOHEADER *bmpheader; |
|
signed char pixelmatrix[MACRO_BLOCK_SIZE][MACRO_BLOCK_SIZE*3]; |
signed char YMatrix[MATRIX_SIZE][MATRIX_SIZE]; |
signed char CrMatrix[MATRIX_SIZE][MATRIX_SIZE]; |
signed char CbMatrix[MATRIX_SIZE][MATRIX_SIZE]; |
|
|
int main(int argc, char* argv[]) |
{ |
int compression,sample; |
unsigned int col, cols, row, rows; |
|
openBMPJPG(argc, argv[1], argv[2]); |
|
rows = bmpheader->height>>4; |
cols = bmpheader->width>>4; |
|
dct_init_start(); |
zzq_encode_init_start(compression); |
vlc_init_start(); |
|
for (row = 0; row < rows; row++) { |
for (col = 0; col < cols; col++) { |
get_MB(row, col, pixelmatrix); |
for(sample=0;sample<5;sample++) { |
if(sample<4) { |
RGB2YCrCb(pixelmatrix,YMatrix,CrMatrix,CbMatrix,sample); |
dct(YMatrix,0); |
} else { |
dct(CrMatrix,1); |
dct(CbMatrix,2); |
} |
} |
} |
} |
|
dct_stop_done(); |
zzq_encode_stop_done(); |
vlc_stop_done(); |
|
closeBMPJPG(); |
return 0; |
|
} |
|
|
/huffman.c
0,0 → 1,365
/* |
Only encoder |
This version works correctly, it is tested with testcase.jpg |
The translation into real huffman codes works. |
Changed: If huffman wants to send 0xFFxx (FF in one byte) than there must be 0x00 inserted between FF and xx |
possible fault in finish send: |
-must it be filled up with zeros? YES |
-must it be filled up to one bye? or 2 byte? --> in this code there is filled up to 2 bytes, but I (joris) thinks this must be filled up to 1 byte. |
still dont know |
- 24-11-05 code clean up |
- 24-11-05 tables added for color |
|
|
|
Block numbers: |
Y = 0 |
cb =1 |
cr= 2 |
*/ |
//--------------------------------------------------------------------------- |
|
#include "ejpgl.h" |
|
static unsigned int vlc_remaining; |
static unsigned char vlc_amount_remaining; |
static unsigned char dcvalue[4]; // 3 is enough |
|
int vlc_init_start() { |
|
vlc_remaining=0x00; |
vlc_amount_remaining=0x00; |
memset(dcvalue, 0, 4); |
return 0; |
|
} |
|
int vlc_stop_done() { |
|
HuffmanEncodeFinishSend(); |
|
} |
|
#define vlc_output_byte(c) put_char(c) |
|
|
static unsigned char convertDCMagnitudeCLengthTable[16] = { |
0x02, 0x02, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00, 0x00, 0x00 |
}; |
|
static unsigned short convertDCMagnitudeCOutTable[16] = { |
0x0000, 0x0001, 0x0002, 0x0006, 0x000e, 0x001e, 0x003e, 0x007e, |
0x00fe, 0x01fe, 0x03fe, 0x07fe, 0x0000, 0x0000, 0x0000, 0x0000 |
}; |
|
void ConvertDCMagnitudeC(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght) |
{ |
unsigned char len; |
|
/* if ((magnitude>16) || ((len=convertDCMagnitudeCLengthTable[magnitude])==0)) { |
printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n"); |
} */ |
|
*lenght = len; |
*out = convertDCMagnitudeCOutTable[magnitude]; |
|
} |
|
static unsigned char convertACMagnitudeCLengthTable[256] = { |
0x02, 0x02, 0x03, 0x04, 0x05, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, // 00 - 0f |
0x00, 0x04, 0x06, 0x08, 0x09, 0x0b, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 10 - 1f |
0x00, 0x05, 0x08, 0x0a, 0x0c, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 20 - 2f |
0x00, 0x05, 0x08, 0x0a, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 30 - 3f |
0x00, 0x06, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 40 - 4f |
0x00, 0x06, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 50 - 5f |
0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 60 - 6f |
0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 70 - 7f |
0x00, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 80 - 8f |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 90 - 9f |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // a0 - af |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // b0 - bf |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // c0 - cf |
0x00, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // d0 - df |
0x00, 0x0e, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // e0 - ef |
0x0a, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00 |
}; |
|
static unsigned short convertACMagnitudeCOutTable[256] = { |
0x0000, 0x0001, 0x0004, 0x000a, 0x0018, 0x0019, 0x0038, 0x0078, 0x01f4, 0x03f6, 0x0ff4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 00 - 0f |
0x0000, 0x000b, 0x0039, 0x00f6, 0x01f5, 0x07f6, 0x0ff5, 0xff88, 0xff89, 0xff8a, 0xff8b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 10 - 1f |
0x0000, 0x001a, 0x00f7, 0x03f7, 0x0ff6, 0x7fc2, 0xff8c, 0xff8d, 0xff8e, 0xff8f, 0xff90, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 20 - 2f |
0x0000, 0x001b, 0x00f8, 0x03f8, 0x0ff7, 0xff91, 0xff92, 0xff93, 0xff94, 0xff95, 0xff96, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 30 - 3f |
0x0000, 0x003a, 0x01f6, 0xff97, 0xff98, 0xff99, 0xff9a, 0xff9b, 0xff9c, 0xff9d, 0xff9e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 40 - 4f |
0x0000, 0x003b, 0x03f9, 0xff9f, 0xffa0, 0xffa1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0xFFA6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 50 - 5f |
0x0000, 0x0079, 0x07f7, 0xffa7, 0xffa8, 0xffa9, 0xffaa, 0xffab, 0xFFAc, 0xFFAf, 0xFFAe, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 60 - 6f |
0x0000, 0x007a, 0x07f8, 0xffaf, 0xffb0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0xFFB6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 70 - 7f |
0x0000, 0x00f9, 0xffb7, 0xFFB8, 0xFFB9, 0xFFBa, 0xFFBb, 0xFFBc, 0xFFBd, 0xFFBe, 0xFFBf, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 80 - 8f |
0x0000, 0x01f7, 0xffc0, 0xffc1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7, 0xFFC8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 90 - 9f |
0x0000, 0x01f8, 0xffc9, 0xFFCa, 0xFFCb, 0xFFCc, 0xFFCd, 0xFFCe, 0xFFCf, 0xFFd0, 0xFFd1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // a0 - af |
0x0000, 0x01f9, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8, 0xFFD9, 0xFFDa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // b0 - bf |
0x0000, 0x01fa, 0xFFDb, 0xFFDc, 0xFFDd, 0xFFDe, 0xFFDf, 0xFFe0, 0xFFe1, 0xFFe2, 0xFFe3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // c0 - cf |
0x0000, 0x07f9, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEa, 0xFFEb, 0xFFEc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // d0 - df |
0x0000, 0x3fe0, 0xffed, 0xFFEe, 0xFFEf, 0xFFf0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0xFFF5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // e0 - ef |
0x03fa, 0x7fc3, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 |
}; |
|
//=========================================================================== |
void ConvertACMagnitudeC(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght) |
{ |
unsigned char len; |
|
len = convertACMagnitudeCLengthTable[magnitude]; |
/* if (!len) { |
printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n"); |
} */ |
*lenght = len; |
*out = convertACMagnitudeCOutTable[magnitude]; |
|
} |
|
static unsigned char convertDCMagnitudeYLengthTable[16] = { |
0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x05, |
0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00 |
}; |
|
static unsigned short convertDCMagnitudeYOutTable[16] = { |
0x0000, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x000e, 0x001e, |
0x003e, 0x007e, 0x00fe, 0x01fe, 0x0000, 0x0000, 0x0000, 0x0000 |
}; |
|
//=========================================================================== |
void ConvertDCMagnitudeY(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght) |
{ |
unsigned char len; |
|
/* if ((magnitude>16) || ((len=convertDCMagnitudeYLengthTable[magnitude])==0)) { |
printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n"); |
} */ |
*lenght = len; |
*out = convertDCMagnitudeYOutTable[magnitude]; |
} |
|
static unsigned char convertACMagnitudeYLength[256] = { |
0x04, 0x02, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0a, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 00 - 0f |
0x00, 0x04, 0x05, 0x07, 0x09, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 10 - 1f |
0x00, 0x05, 0x08, 0x0a, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 20 - 2f |
0x00, 0x06, 0x09, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 30 - 3f |
0x00, 0x06, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 40 - 4f |
0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 50 - 5f |
0x00, 0x07, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 60 - 6f |
0x00, 0x08, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 70 - 7f |
0x00, 0x09, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 80 - 8f |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 90 - 9f |
0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // a0 - af |
0x00, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // b0 - bf |
0x00, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // c0 - cf |
0x00, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // d0 - df |
0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // e0 - ef |
0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00 |
}; |
|
static unsigned short convertACMagnitudeYOut[256] = { |
0xFFFA, 0xFFF0, 0xFFF1, 0xFFF4, 0xFFFB, 0xFFFA, 0xFFF8, 0xFFF8, 0xFFF6, 0xFF82, 0xFF83, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 00 - 0f |
0x0000, 0xFFFC, 0xFFFB, 0xFFF9, 0xFFF6, 0xFFF6, 0xFF84, 0xFF85, 0xFF86, 0xFF87, 0xFF88, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 10 - 1f |
0x0000, 0xFFFC, 0xFFF9, 0xFFF7, 0xFFF4, 0xFF89, 0xFF8A, 0xFF8B, 0xFF8C, 0xFF8D, 0xFF8E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 20 - 2f |
0x0000, 0xFFFA, 0xFFF7, 0xFFF5, 0xFF8F, 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 30 - 3f |
0x0000, 0xFFFB, 0xFFF8, 0xFF96, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 40 - 4f |
0x0000, 0xFFFA, 0xFFF7, 0xFF9E, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 50 - 5f |
0x0000, 0xFFFB, 0xFFF6, 0xFFA6, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 60 - 6f |
0x0000, 0xFFFA, 0xFFF7, 0xFFAE, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 70 - 7f |
0x0000, 0xFFF8, 0xFFC0, 0xFFB6, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 80 - 8f |
0x0000, 0xFFF9, 0xFFBE, 0xFFBF, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // 90 - 9f |
0x0000, 0xFFFA, 0xFFC7, 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // a0 - af |
0x0000, 0xFFF9, 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // b0 - bf |
0x0000, 0xFFFA, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF, 0xFFE0, 0xFFE1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // c0 - cf |
0x0000, 0xFFF8, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // d0 - df |
0x0000, 0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // e0 - ef |
0xFFF9, 0xFFF5, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 |
}; |
|
//=========================================================================== |
void ConvertACMagnitudeY(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght) |
{ |
unsigned char len; |
|
len = convertACMagnitudeYLength[magnitude]; |
/* if (!len) { |
#ifndef __MICROBLAZE |
printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n"); |
#endif |
} */ |
*lenght = len; |
*out = convertACMagnitudeYOut[magnitude]; |
|
} |
|
char Extend (char additional, unsigned char magnitude) |
{ |
int vt= 1 << (magnitude-1); |
if ( additional < vt ) return (additional + (-1 << magnitude) + 1); |
else return additional; |
} |
|
void ReverseExtend (char value, unsigned char *magnitude, unsigned char *bits) |
{ |
// printf("reverseextend value= %d\n",*magnitude); |
if (value >=0) |
{ |
*bits=value; |
} |
else |
{ |
value=-value; |
*bits=~value; |
} |
*magnitude=0; |
while (value !=0) |
{ |
value>>=1; |
++*magnitude; |
} |
// printf("reverseextend magnitude= %d bits= %d",magnitude,bits); |
return; |
} |
|
void WriteRawBits16(unsigned char amount_bits, unsigned int bits) //*remaining needs bo be more than 8 bits because 8 bits could be added and ther ecould already be up ot 7 bits in *remaining |
// this function collects bits to send |
// if there less than 16 bits collected, nothing is send and these bits are stored in *remaining. In *amount_remaining there is stated how much bits are stored in *remaining |
// if more than 16 bits are collected, 16 bits are send and the remaining bits are stored again |
{ |
unsigned short int send; |
unsigned int mask; |
unsigned char send2; |
int count; |
mask=0x00; //init mask |
vlc_remaining=(vlc_remaining<<amount_bits); //shift to make place for the new bits |
for (count=amount_bits; count>0; count--) mask=(mask<<1)|0x01; //create mask for adding bit |
vlc_remaining=vlc_remaining | (bits&mask); //add bits |
vlc_amount_remaining=vlc_amount_remaining + amount_bits; //change *amount_remaining to the correct new value |
if (vlc_amount_remaining >= 16) //are there more than 16 bits in buffer, send 16 bits |
{ |
/* #ifndef __MICROBLAZE |
if (vlc_amount_remaining >= 32 ) printf("ERROR, more bits to send %d",vlc_amount_remaining); |
#endif */ |
send=vlc_remaining>>(vlc_amount_remaining-16); //this value can be send/stored (in art this can be dony by selecting bits) |
send2=(send & 0xFF00) >>8; |
vlc_output_byte(send2); |
if (send2==0xFF) |
{ |
send2=0x00; |
vlc_output_byte(send2); |
} |
send2=send & 0xFF; |
vlc_output_byte(send2); |
if (send2==0xFF) |
{ |
send2=0x00; |
vlc_output_byte(send2); |
} |
vlc_amount_remaining=vlc_amount_remaining-16; //descrease by 16 because these are send |
} |
return; |
} |
|
void HuffmanEncodeFinishSend() |
// There are still some bits left to send at the end of the 8x8 matrix (or maybe the file), |
// the remaining bits are filled up with ones and send |
// possible fault: -must it be filled up with ones? |
{ |
unsigned short int send; |
unsigned int mask; |
int count; |
mask=0x00; //init mask |
if (vlc_amount_remaining >= 8) //2 bytes to send, send first byte |
{ |
send=vlc_remaining>>(vlc_amount_remaining-8); //shift so that first byte is ready to send |
vlc_output_byte(send&0xff); |
if (send==0xFF) //is this still needed???? |
{ |
send=0x00; |
vlc_output_byte(send&0xff); |
} |
vlc_amount_remaining=vlc_amount_remaining -8; // lower the value to the amount of bits that still needs to be send |
} |
if (vlc_amount_remaining >= 0) //there is a last byte to send |
{ |
send=vlc_remaining<<(8-vlc_amount_remaining); //shift the last bits to send to the front of the byte |
mask=0x00; //init mask |
for (count=(8-vlc_amount_remaining); count>0; count--) mask=(mask<<1)|0x01; //create mask to fill byte up with ones |
send=send | mask; //add the ones to the byte |
vlc_output_byte(send&0xff); |
vlc_amount_remaining=0x00; //is this needed? |
} |
return; |
} |
|
void HuffmanEncodeUsingDCTable(unsigned char magnitude) |
// Translate magnitude into needed data (from table) and send it |
{ |
unsigned char send; |
unsigned short int huffmancode, huffmanlengt; |
ConvertDCMagnitudeY(magnitude, &huffmancode, &huffmanlengt); |
WriteRawBits16(huffmanlengt,huffmancode); |
//printf("Write DC magnitude= %2x \n",magnitude); |
//WriteRawBits16(0x08,magnitude,remaining,amount_remaining, file); |
return; |
} |
|
void HuffmanEncodeUsingACTable(unsigned char mag) |
// Translate magnitude into needed data (from table) and send it |
{ |
unsigned char send; |
unsigned short int huffmancode, huffmanlengt; |
ConvertACMagnitudeY(mag, &huffmancode, &huffmanlengt); |
WriteRawBits16(huffmanlengt,huffmancode); |
return; |
} |
|
char EncodeDataUnit(char dataunit[64], unsigned int color) |
{ |
char difference; |
unsigned char magnitude,zerorun,ii,ert; |
unsigned int bits; |
unsigned char bit_char; |
char last_dc_value; |
//init |
// PrintMatrix(dataunit) ; |
last_dc_value = dcvalue[color]; |
difference = dataunit[0] - last_dc_value; |
last_dc_value=dataunit[0]; |
ReverseExtend(difference, &magnitude,&bit_char); |
bits = bit_char; |
HuffmanEncodeUsingDCTable(magnitude); |
WriteRawBits16(magnitude,bits); |
zerorun=0; |
ii=1; |
while ( ii < 64 ) |
{ |
if (dataunit[ii] != 0 ) |
{ |
while ( zerorun >= 16 ) |
{ |
HuffmanEncodeUsingACTable(0xF0); |
zerorun=zerorun-16; |
// printf("16 zeros: %d\n",zerorun); |
} |
ReverseExtend(dataunit[ii],&magnitude,&bit_char); |
bits=bit_char; |
ert= ((int)zerorun *16); //ERROR !!!!!!!!!!! |
ert=ert + magnitude; |
HuffmanEncodeUsingACTable(ert); |
WriteRawBits16(magnitude,bits); |
zerorun=0; |
} |
else zerorun=zerorun+1; |
ii++; |
} |
if ( zerorun != 0 ) |
{ |
HuffmanEncodeUsingACTable(0x00); |
} |
dcvalue[color] = last_dc_value; |
return 0; |
} |
|
/ejpgl.h
0,0 → 1,122
#ifndef _EJPGL_H |
#define _EJPGL_H |
|
#define MATRIX_SIZE 8 |
#define MACRO_BLOCK_SIZE 16 |
#define NUMBER_OF_PIXELS MATRIX_SIZE*MATRIX_SIZE |
|
|
typedef struct { |
unsigned int size; /* Header size in bytes */ |
int width,height; /* Width and height of image */ |
unsigned short int planes; /* Number of colour planes */ |
unsigned short int bits; /* Bits per pixel */ |
unsigned int compression; /* Compression type */ |
unsigned int imagesize; /* Image size in bytes */ |
int xresolution,yresolution; /* Pixels per meter */ |
unsigned int ncolours; /* Number of colours */ |
unsigned int importantcolours; /* Important colours */ |
unsigned char palette[1024]; /* Storage for palette */ |
} INFOHEADER; |
|
typedef struct { |
int restofheader; //TODO |
INFOHEADER info; /* Information header */ |
} BMPHEADER; |
|
typedef struct { |
unsigned int row; /* Width and height of image */ |
unsigned int col; /* Width and height of image */ |
} BLOCKINFO; |
|
typedef struct { |
unsigned char QTMarker[2]; |
unsigned char Length[2]; |
unsigned char QTInfo[130]; //bit 0..3: number of QT (0..3, otherwise error) |
// bit 4..7: precision of QT, 0 = 8 bit, otherwise 16 bit |
// unsigned char ValuesQT[]; //max 192 values. 64*(precision+1) bytes |
} QTINFO; |
|
typedef struct { |
unsigned char HTMarker[2]; |
unsigned char Length[2]; |
unsigned char HuffmanInfo[416]; //Array containing ALL huffman information |
//For each color component holds: |
//First byte is used as info byte, followed by 16 bytes with values used |
//for counting the different huffman codes, finally the corresponding |
//huffman codes will follow. This sequence can repeat it self for |
//different Huffman tables, both DC or AC tables. |
|
//The structure of the information byte is as follows: |
//bit 0..3 : number of HT (0..3, otherwise error) |
//bit 4 : type of HT, 0 = DC table, 1 = AC table |
//bit 5..7 : not used, must be 0 (Used for progressive scan JPEG) |
} HTINFO; |
|
|
typedef struct { |
unsigned char APP0Marker[2]; |
unsigned char Length[2]; |
unsigned char Identifier[5]; |
unsigned char Version[2]; |
unsigned char Units; |
unsigned char XDensity[2]; |
unsigned char YDensity[2]; |
unsigned char ThumbWidth; |
unsigned char ThumbHeight; |
} APP0INFO; |
|
typedef struct { |
unsigned char SOF0Marker[2]; |
unsigned char Length[2]; |
unsigned char DataPrecision; //This is in bits/sample, usually 8 (12 and 16 not supported by most software). |
unsigned char ImageHeight[2]; |
unsigned char ImageWidth[2]; |
unsigned char Components; //Usually 1 = grey scaled, 3 = color YcbCr or YIQ 4 = color CMYK |
unsigned char ComponentInfo[3][3]; //Read each component data of 3 bytes. It contains, |
//(component Id(1byte)(1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q), |
//sampling factors (1byte) (bit 0-3 vertical., 4-7 horizontal.), |
//quantization table number (1 byte)). |
} SOF0INFO; |
|
typedef struct { |
unsigned char SOSMarker[2]; |
unsigned char Length[2]; //This must be equal to 6+2*(number of components in scan). |
unsigned char ComponentCount; //This must be >= 1 and <=4 (otherwise error), usually 1 or 3 |
unsigned char Component[3][2]; // For each component, read 2 bytes. It contains, |
//1 byte Component Id (1=Y, 2=Cb, 3=Cr, 4=I, 5=Q), |
//1 byte Huffman table to use : |
//bit 0..3 : AC table (0..3) |
//bit 4..7 : DC table (0..3) |
unsigned char Ignore[3]; //We have to skip 3 bytes |
} SOSINFO; |
|
typedef struct { |
unsigned char DRIMarker[2]; |
unsigned char Length[2]; |
unsigned char RestartInteral[2]; // Interval of the restart markers |
} DRIINFO; |
|
typedef struct { |
unsigned char SOIMarker[2]; //Start of image marker |
APP0INFO app0; |
QTINFO qt; |
SOF0INFO sof0; |
HTINFO ht; |
// DRIINFO dri; |
SOSINFO sos; |
} JPEGHEADER; |
|
|
int openBMPJPG(int argc, char* bmpfilename, char* jpgfilename); |
int closeBMPJPG(); |
|
|
void HuffmanEncodeFinishSend(); |
|
|
|
int idct8x8(int* fblock, char* sblock); |
|
#endif |
|
/ColorConversion.c
0,0 → 1,27
#include "ejpgl.h" |
|
#define RGB2Y(r, g, b) (((66*r + 129*g + 25*b + 128)>>8)+128) |
#define RGB2Cr(r, g, b) (((-38*r - 74*g + 112*b + 128)>>8)+128) |
#define RGB2Cb(r, g, b) (((112*r - 94*g - 18*b + 128)>>8)+128) |
|
void RGB2YCrCb(signed char pixelmatrix[MACRO_BLOCK_SIZE][MACRO_BLOCK_SIZE*3],signed char YMatrix[MATRIX_SIZE][MATRIX_SIZE],signed char CrMatrix[MATRIX_SIZE][MATRIX_SIZE],signed char CbMatrix[MATRIX_SIZE][MATRIX_SIZE], unsigned int sample) |
{ |
unsigned int row, col, rowoffset, coloffset, xoffset, yoffset; |
for(row = 0;row < MATRIX_SIZE; row++) { |
for(col = 0; col < MATRIX_SIZE; col++) { |
coloffset = (sample&0x01)*8; |
rowoffset = (sample&0x02)*4; |
YMatrix[row][col] = RGB2Y(pixelmatrix[row+rowoffset][(col+coloffset)*3+2],pixelmatrix[row+rowoffset][(col+coloffset)*3+1],pixelmatrix[row+rowoffset][(col+coloffset)*3]) - 128; |
if (col%2==0) { |
yoffset = (sample&0x01)*4; |
xoffset = (sample&0x02)*2; |
if (row%2==0) { |
CrMatrix[xoffset+(row>>1)][yoffset+(col>>1)] = RGB2Cr(pixelmatrix[row+rowoffset][(col+coloffset)*3+2],pixelmatrix[row+rowoffset][(col+coloffset)*3+1],pixelmatrix[row+rowoffset][(col+coloffset)*3]) - 128; |
} else { |
CbMatrix[xoffset+((row)>>2)][yoffset+(col>>2)] = RGB2Cb(pixelmatrix[row+rowoffset][(col+coloffset)*3+2],pixelmatrix[row+rowoffset][(col+coloffset)*3+1],pixelmatrix[row+rowoffset][(col+coloffset)*3]) - 128; |
} |
} |
} |
} |
} |
|
/zzq.c
0,0 → 1,84
#include "ejpgl.h" |
|
unsigned char quantization_table[MATRIX_SIZE][MATRIX_SIZE] ={ |
{4, 3, 3, 4, 4, 5, 6, 6}, |
{3, 3, 4, 4, 5, 6, 6, 6}, |
{4, 4, 4, 4, 5, 6, 6, 6}, |
{4, 4, 4, 5, 6, 6, 6, 6}, |
{4, 4, 5, 6, 6, 7, 7, 6}, |
{4, 5, 6, 6, 6, 7, 7, 6}, |
{6, 6, 6, 6, 7, 7, 7, 7}, |
{6, 6, 6, 7, 7, 7, 7, 7} |
}; |
|
signed char bitstream[NUMBER_OF_PIXELS] ; |
|
int zzq_encode_init_start(int compression) { |
|
return 0; |
|
} |
|
int zzq_encode_stop_done() { |
|
|
} |
|
void zzq_encode(signed short pixelmatrix[MATRIX_SIZE][MATRIX_SIZE], int color) |
{ |
int i, x, y, jumped, deltax, deltay; |
|
x = y = deltax = deltay = jumped = 0; |
|
for(i=0;i<NUMBER_OF_PIXELS;i++) |
{ |
if(pixelmatrix[y][x]>0) |
bitstream[i] = (pixelmatrix[y][x]>>quantization_table[y][x]); |
else |
bitstream[i] = -((-pixelmatrix[y][x])>>quantization_table[y][x]); |
|
if((y == 0) || (y == MATRIX_SIZE-1)) { //on top or bottom side of matrix |
if(!jumped) { //first jump to element on the right |
x++; |
jumped = 1; |
} else { //modify direction |
if(i<(NUMBER_OF_PIXELS>>1)) { |
deltax = -1; |
deltay = 1; |
} else { |
deltax = 1; |
deltay = -1; |
} |
x += deltax; |
y += deltay; |
jumped = 0; |
} |
} else if ((x == 0) || (x == MATRIX_SIZE-1)) { //on left or right side of matrix |
if(!jumped) { //jump to element below |
y++; |
jumped = 1; |
} else { //modify direction |
if(i<(NUMBER_OF_PIXELS>>1)) { |
deltax = 1; |
deltay = -1; |
} else { |
deltax = -1; |
deltay = 1; |
} |
x += deltax; |
y += deltay; |
jumped = 0; |
} |
} |
else {//not on the edges of the matrix |
x += deltax; |
y += deltay; |
} |
} |
|
EncodeDataUnit(bitstream, color); |
|
return; |
|
} |
|
/jpeg.c
0,0 → 1,170
#include "ejpgl.h" |
|
unsigned char qtable[64] = {16, 8, 8, 16, 12, 8, 16, 16, 16, 16, 16, 16, 16, 16, |
16, 32, 32, 16, 16, 16, 16, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 64, 64, 128, 128, 128, 128, 128, 64, 64, |
128, 128, 128, 128, 128, 64, 128, 128, 128}; |
|
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 |
{0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D}, //standard AC table count |
{0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, //standard DC table count |
{0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D}}; //standard AC table count |
|
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}; |
unsigned char huffACvalues[162] = {0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, |
0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, |
0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, |
0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, |
0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, |
0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, |
0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, |
0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, |
0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA}; |
|
int writejpegheader(INFOHEADER *header, JPEGHEADER *jpegheader) |
{ |
unsigned int huffmantablesize, previoussize; |
unsigned char QTcount, i, j, components, id, huffmantablecount; |
unsigned short length, headerlength; |
|
//Number of Quatization Tables |
QTcount = 2; |
headerlength = 12; //12 bytes are needed for the markers |
huffmantablecount = 4; //2 AC and 2 DC tables |
huffmantablesize = 0; |
|
jpegheader->SOIMarker[0] = 0xff; |
jpegheader->SOIMarker[1] = 0xd8; |
|
//APP0 segment |
jpegheader->app0.APP0Marker[0] = 0xff; |
jpegheader->app0.APP0Marker[1] = 0xe0; |
|
headerlength += 16; //APP0 marker is always 16 bytes long |
jpegheader->app0.Length[0] = 0x00; |
jpegheader->app0.Length[1] = 0x10; |
jpegheader->app0.Identifier[0] = 0x4a; |
jpegheader->app0.Identifier[1] = 0x46; |
jpegheader->app0.Identifier[2] = 0x49; |
jpegheader->app0.Identifier[3] = 0x46; |
jpegheader->app0.Identifier[4] = 0x00; |
jpegheader->app0.Version[0] = 0x01; |
jpegheader->app0.Version[1] = 0x00; |
jpegheader->app0.Units = 0x00; |
jpegheader->app0.XDensity[0] = 0x00; |
jpegheader->app0.XDensity[1] = 0x01; |
jpegheader->app0.YDensity[0] = 0x00; |
jpegheader->app0.YDensity[1] = 0x01; |
jpegheader->app0.ThumbWidth = 0x00; |
jpegheader->app0.ThumbHeight = 0x00; |
|
//Quantization Table Segment |
jpegheader->qt.QTMarker[0] = 0xff; |
jpegheader->qt.QTMarker[1] = 0xdb; |
length = (QTcount<<6) + QTcount + 2; |
headerlength += length; |
jpegheader->qt.Length[0] = (length & 0xff00)>>8; |
jpegheader->qt.Length[1] = length & 0xff; |
// jpegheader->qt.QTInfo = 0x00; // index = 0, precision = 0 |
//write Quantization table to header |
i = 0; |
|
for (id=0; id<QTcount; id++) { |
jpegheader->qt.QTInfo[(id<<6)+id] = id; |
for(i=0;i<64;i++) { |
jpegheader->qt.QTInfo[i+1+id+(id<<6)] = qtable[i]; |
} |
} |
|
//Start of Frame segment |
jpegheader->sof0.SOF0Marker[0] = 0xff; |
jpegheader->sof0.SOF0Marker[1] = 0xc0; |
if(header->bits == 8) { |
components = 0x01; |
} |
else { |
components = 0x03; |
} |
length = 8 + 3*components; |
headerlength += length; |
jpegheader->sof0.Length[0] = (length & 0xff00) >> 8; |
jpegheader->sof0.Length[1] = length & 0xff; |
jpegheader->sof0.DataPrecision = 0x08; |
jpegheader->sof0.ImageHeight[0] = (header->height & 0xff00) >> 8; |
jpegheader->sof0.ImageHeight[1] = header->height & 0xff; |
jpegheader->sof0.ImageWidth[0] = (header->width & 0xff00) >> 8; |
jpegheader->sof0.ImageWidth[1] = header->width & 0xff; |
jpegheader->sof0.Components = components; |
for (i=0; i < components; i++) { |
jpegheader->sof0.ComponentInfo[i][0] = i+1; //color component |
if(i==0) { |
jpegheader->sof0.ComponentInfo[i][1] = 0x22; //4:2:0 subsampling |
} else { |
jpegheader->sof0.ComponentInfo[i][1] = 0x11; //4:2:0 subsampling |
} |
jpegheader->sof0.ComponentInfo[i][2] = (i==0)? 0x00 : 0x01; //quantization table ID |
} |
//Start of Huffman Table Segment |
|
jpegheader->ht.HTMarker[0] = 0xff; |
jpegheader->ht.HTMarker[1] = 0xc4; |
|
//Set dummy HT segment length |
length = 0;//tablecount*17; |
jpegheader->ht.Length[0] = (length & 0xff00) >> 8; |
jpegheader->ht.Length[1] = length & 0xff; |
previoussize = 0; |
for (id=0; id < huffmantablecount; id++) { |
huffmantablesize = 0; |
switch (id) { |
case 0 : jpegheader->ht.HuffmanInfo[previoussize] = 0x00; |
break; |
case 1 : jpegheader->ht.HuffmanInfo[previoussize] = 0x10; |
break; |
case 2 : jpegheader->ht.HuffmanInfo[previoussize] = 0x01; |
break; |
case 3 : jpegheader->ht.HuffmanInfo[previoussize] = 0x11; |
break; |
} |
for (i=1; i <= 16; i++) { |
jpegheader->ht.HuffmanInfo[i+previoussize] = huffmancount[id][i-1]; |
huffmantablesize += huffmancount[id][i-1]; |
} |
|
for (i=0; i < huffmantablesize; i++) { |
jpegheader->ht.HuffmanInfo[i+previoussize+17] = (id%2 == 1)? huffACvalues[i] : huffDCvalues[i]; |
} |
previoussize += huffmantablesize + 17; |
} |
//Set real HT segment length |
length = 2+previoussize; |
headerlength += length; |
jpegheader->ht.Length[0] = (length & 0xff00) >> 8; |
jpegheader->ht.Length[1] = length & 0xff; |
//Reset marker segment |
|
//Start of Scan Header Segment |
jpegheader->sos.SOSMarker[0] = 0xff; |
jpegheader->sos.SOSMarker[1] = 0xda; |
length = 6 + (components<<1); |
headerlength += length; |
jpegheader->sos.Length[0] = (length & 0xff00) >> 8; |
jpegheader->sos.Length[1] = length & 0xff; |
jpegheader->sos.ComponentCount = components; //number of color components in the image |
jpegheader->sos.Component[0][0] = 0x01; //Y component |
jpegheader->sos.Component[0][1] = 0x00; //indexes of huffman tables for Y-component |
if (components == 0x03) { |
jpegheader->sos.Component[1][0] = 0x02; //the CB component |
jpegheader->sos.Component[1][1] = 0x11; //indexes of huffman tables for CB-component |
jpegheader->sos.Component[2][0] = 0x03; //The CR component |
jpegheader->sos.Component[2][1] = 0x11; //indexes of huffman tables for CR-component |
} |
//following bytes are ignored since progressive scan is not to be implemented |
jpegheader->sos.Ignore[0] = 0x00; |
jpegheader->sos.Ignore[1] = 0x3f; |
jpegheader->sos.Ignore[2] = 0x00; |
|
return headerlength; |
|
} |
|
/dct.c
0,0 → 1,584
#include "ejpgl.h" |
|
extern signed int weights[512]; |
signed short dctresult[MATRIX_SIZE][MATRIX_SIZE]; |
|
int dct_init_start() { |
|
return 0; |
|
} |
|
int dct_stop_done() { |
|
return 0; |
|
} |
|
/* |
Function Name: dct |
|
Operation: Find the 8x8 DCT of an array using separable DCT |
First, finds 1-d DCT along rows, storing the result in inter[][] |
Then, 1-d DCT along columns of inter[][] is found |
|
Input: pixels is the 8x8 input array |
|
Output: dct is the 8x8 output array |
*/ |
|
void dct(signed char pixels[8][8], int color) |
{ |
int inr, inc; /* rows and columns of input image */ |
int intr, intc; /* rows and columns of intermediate image */ |
int outr, outc; /* rows and columns of dct */ |
int f_val; /* cumulative sum */ |
int inter[8][8]; /* stores intermediate result */ |
int i,j,k; |
k=0; |
|
for (intr=0; intr<8; intr++) |
for (intc=0; intc<8; intc++) { |
for (i=0,f_val=0; i<8; i++) { |
f_val += (pixels[intr][i]* weights[k]);//cos((double)(2*i+1)*(double)intc*PI/16); |
k++; |
} |
if (intc!=0) inter[intr][intc] = f_val>>15; |
else inter[intr][intc] = (11585*(f_val>>14))>>15; |
|
} |
|
/* find 1-d dct along columns */ |
|
for (outc=0, k=0; outc<8; outc++) |
for (outr=0; outr<8; outr++) { |
for (i=0,f_val=0; i<8; i++) { |
f_val += (inter[i][outc] *weights[k]); |
k++; |
} |
if (outr!=0) dctresult[outr][outc] = f_val>>15; |
else dctresult[outr][outc] = (11585*(f_val>>14)>>15); |
} |
|
zzq_encode(dctresult, color); |
return; |
|
} |
|
|
signed int weights[512] = { |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16384, |
16069, |
13623, |
9102, |
3196, |
-3196, |
-9102, |
-13623, |
-16069, |
15137, |
6270, |
-6270, |
-15137, |
-15137, |
-6270, |
6270, |
15137, |
13623, |
-3196, |
-16069, |
-9103, |
9102, |
16069, |
3196, |
-13623, |
11585, |
-11585, |
-11585, |
11585, |
11585, |
-11585, |
-11585, |
11585, |
9102, |
-16069, |
3196, |
13623, |
-13623, |
-3197, |
16069, |
-9102, |
6270, |
-15137, |
15137, |
-6270, |
-6270, |
15137, |
-15137, |
6270, |
3196, |
-9103, |
13623, |
-16069, |
16069, |
-13623, |
9102, |
-3196 |
}; |
|
|