From a9fec5259222066d6a49151af1929f5e11cafa11 Mon Sep 17 00:00:00 2001 From: Oliver Schinagl Date: Sun, 10 Apr 2005 20:54:38 +0000 Subject: bmpjpeg.c -> bmp to jpeg in 8x8 and YCrCb and jpeg 8x8 YCrCb to bmp color.c -> RGB to YCrCb and YCrCb to RGB --- src/bmpjpeg.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/color.c | 31 +++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/bmpjpeg.c create mode 100644 src/color.c diff --git a/src/bmpjpeg.c b/src/bmpjpeg.c new file mode 100644 index 0000000..3d76e70 --- /dev/null +++ b/src/bmpjpeg.c @@ -0,0 +1,142 @@ +#include "color.h" + +void BMPto8x8AndYCbCr(int Start_x, int Start_y, int ImageSize_x, int ImageSize_y, int offset, unsigned char depth ,unsigned char *Image, unsigned char *blok8x8) +{ + unsigned char i; + int x,y,Start_point; + depth = depth/8; + Start_point = offset + (ImageSize_x*(ImageSize_y-1)+Start_x-(Start_y*ImageSize_x))*depth; + for(i=0;i<(64*depth);i+=depth) + { + if((i%8)==0) printf("\n"); + if((Start_y+(i/(8*depth))) < ImageSize_y) + { + if((Start_x+((i%(8*depth))/depth)) < ImageSize_x) + { + if(depth == 3) + { + RGB2YCbCr(Image[Start_point+(i%(8*depth)) -(i/(8*depth))*(ImageSize_x*depth)], + Image[Start_point+(i%(8*depth))+1-(i/(8*depth))*(ImageSize_x*depth)], + Image[Start_point+(i%(8*depth))+2-(i/(8*depth))*(ImageSize_x*depth)], + &blok8x8[i],&blok8x8[i+1],&blok8x8[i+2]); + + printf("%02x%02x%02x|",blok8x8[i+0],blok8x8[i+1],blok8x8[i+2]); + } + if(depth == 1) + { + blok8x8[i+0] = Image[Start_point+(i%(8*depth))-(i/(8*depth))*(ImageSize_x*depth)]; + printf("%02x ",blok8x8[i]); + } + } + } + else + i = (64*depth); + } +} + +void YCbCrAnd8x8toBMP(int Start_x, int Start_y, int ImageSize_x, int ImageSize_y, int offset, unsigned char depth ,unsigned char *Image, unsigned char *blok8x8) +{ + unsigned char i; + int x,y,Start_point; + depth = depth/8; + Start_point = offset + (ImageSize_x*(ImageSize_y-1)+Start_x-(Start_y*ImageSize_x))*depth; + for(i=0;i<(64*depth);i+=depth) + { + if((i%8)==0) printf("\n"); + if((Start_y+(i/(8*depth))) < ImageSize_y) + { + if((Start_x+((i%(8*depth))/depth)) < ImageSize_x) + { + if(depth == 3) + { + YCbCr2RGB(blok8x8[i+0],blok8x8[i+1],blok8x8[i+2], + &Image[Start_point+(i%(8*depth)) -(i/(8*depth))*(ImageSize_x*depth)], + &Image[Start_point+(i%(8*depth))+1-(i/(8*depth))*(ImageSize_x*depth)], + &Image[Start_point+(i%(8*depth))+2-(i/(8*depth))*(ImageSize_x*depth)]); + + printf("%02x%02x%02x|",blok8x8[i+0],blok8x8[i+1],blok8x8[i+2]); + } + if(depth == 1) + { + Image[Start_point+(i%(8*depth))-(i/(8*depth))*(ImageSize_x*depth)] = blok8x8[i+0]; + printf("%02x ",blok8x8[i]); + } + } + } + else + i = (64*depth); + } + +} + +/***************************** +The code without the RGB - > YUV +****************************** + +void BMPto8x8(int Start_x, int Start_y, int ImageSize_x, int ImageSize_y, int offset, unsigned char depth ,unsigned char *Image, unsigned char *blok8x8) +{ + unsigned char i; + int x,y,Start_point; + depth = depth/8; + Start_point = offset + (ImageSize_x*(ImageSize_y-1)+Start_x-(Start_y*ImageSize_x))*depth; + for(i=0;i<(64*depth);i+=depth) + { + if((i%8)==0) printf("\n"); + if((Start_y+(i/(8*depth))) < ImageSize_y) + { + if((Start_x+((i%(8*depth))/depth)) < ImageSize_x) + { + if(depth == 3) + { + blok8x8[i+2] = Image[Start_point+(i%(8*depth)) -(i/(8*depth))*(ImageSize_x*depth)]; //R + blok8x8[i+1] = Image[Start_point+(i%(8*depth))+1-(i/(8*depth))*(ImageSize_x*depth)]; //G + blok8x8[i+0] = Image[Start_point+(i%(8*depth))+2-(i/(8*depth))*(ImageSize_x*depth)]; //B + printf("%02x%02x%02x|",blok8x8[i+0],blok8x8[i+1],blok8x8[i+2]); + } + if(depth == 1) + { + blok8x8[i+0] = Image[Start_point+(i%(8*depth))-(i/(8*depth))*(ImageSize_x*depth)]; + printf("%02x ",blok8x8[i]); + } + } + } + else + i = (64*depth); + } +} + +void B8x8toBMP(int Start_x, int Start_y, int ImageSize_x, int ImageSize_y, int offset, unsigned char depth ,unsigned char *Image, unsigned char *blok8x8) +{ + unsigned char i; + int x,y,Start_point; + depth = depth/8; + Start_point = offset + (ImageSize_x*(ImageSize_y-1)+Start_x-(Start_y*ImageSize_x))*depth; + for(i=0;i<(64*depth);i+=depth) + { + if((i%8)==0) printf("\n"); + if((Start_y+(i/(8*depth))) < ImageSize_y) + { + if((Start_x+((i%(8*depth))/depth)) < ImageSize_x) + { + if(depth == 3) + { + Image[Start_point+(i%(8*depth)) -(i/(8*depth))*(ImageSize_x*depth)] = blok8x8[i+2]; //R + Image[Start_point+(i%(8*depth))+1-(i/(8*depth))*(ImageSize_x*depth)] = blok8x8[i+1]; //G + Image[Start_point+(i%(8*depth))+2-(i/(8*depth))*(ImageSize_x*depth)] = blok8x8[i+0]; //B + printf("%02x%02x%02x|",blok8x8[i+0],blok8x8[i+1],blok8x8[i+2]); + } + if(depth == 1) + { + Image[Start_point+(i%(8*depth))-(i/(8*depth))*(ImageSize_x*depth)] = blok8x8[i+0]; + printf("%02x ",blok8x8[i]); + } + } + } + else + i = (64*depth); + } + +} + +*/ + diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000..7057a23 --- /dev/null +++ b/src/color.c @@ -0,0 +1,31 @@ +#include "color.h" + +#define Mul_Shift(x,y) ((x*y)>>8) + +unsigned char clip(int value) { + if (value < 0) return (char) 0; + if (value > 255) return (char) 255; + return (unsigned char) value; +} + +/* RGB -> YCbCr (also from BMP to JPEG) */ +// Y = 0.299 R + 0.587 G + 0.114 B +// Cb = - 0.1687 R - 0.3313 G + 0.5 B + 128 +// Cr = 0.5 R - 0.4187 G - 0.0813 B + 128 +void RGB2YCbCr (unsigned char R, unsigned char G, unsigned char B,unsigned char *Y,unsigned char *Cb,unsigned char *Cr) { + *Y = clip(Mul_Shift(R,77) + Mul_Shift(G,150) + Mul_Shift(B,29)); + *Cb = clip((B>>1)-Mul_Shift(R,43) - Mul_Shift(G,85) + 128); + *Cr = clip((R>>1) - Mul_Shift(G,107) - Mul_Shift(B,21) + 128); +} + +/* RGB -> YCbCr (also from JPEG to BMP) */ +// R = Y + 1.402 (Cr-128) +// G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128) +// B = Y + 1.772 (Cb-128) +void YCbCr2RGB (unsigned char Y, unsigned char Cb, unsigned char Cr,unsigned char *R,unsigned char *G,unsigned char *B) { + *R = clip(Y + Mul_Shift((Cr-128),359)); + *G = clip(Y - Mul_Shift((Cb-128),88) - Mul_Shift((Cr-128),183)); + *B = clip(Y + Mul_Shift((Cb-128),454)); +} + + -- cgit v0.12