summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Schinagl <oliver@schinagl.nl>2005-04-06 18:56:04 (GMT)
committerOliver Schinagl <oliver@schinagl.nl>2005-04-06 18:56:04 (GMT)
commit4ab8e8dcbd487b10f86eb3bbd7a1f8c3a5344cd4 (patch)
tree3abeeb6906dc43d9b50f29840c2f0442eba1338a
parentb3bceeefd41d45662bf342a834a5163cb07fae51 (diff)
download5kk53-4ab8e8dcbd487b10f86eb3bbd7a1f8c3a5344cd4.zip
5kk53-4ab8e8dcbd487b10f86eb3bbd7a1f8c3a5344cd4.tar.gz
5kk53-4ab8e8dcbd487b10f86eb3bbd7a1f8c3a5344cd4.tar.bz2
Added RGB-YCbCr and JPEG header reader
-rw-r--r--src/main.c144
1 files changed, 113 insertions, 31 deletions
diff --git a/src/main.c b/src/main.c
index 39d5bfc..0b0d84a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,19 +5,70 @@
#define DECODE 1
#define MAXROWS 1024
#define MAXCOLS 768
+#define HEADER 56
+
+#define SOI_MK 0xFFD8 /* start of image */
+#define SOF_MK 0xFFC0 /* start of frame */
+#define EOI_MK 0xFFD9 /* end of image */
+#define DRI_MK 0xFFDD /* restart interval */
+
+#define get_size(fi) (((unsigned int)Image1[fi]) << 8) + (unsigned int)Image1[fi+1]
+
+unsigned char Image1[MAXCOLS*3*MAXROWS+HEADER];
+
+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
+unsigned char RGB2Y (unsigned char R, unsigned char G, unsigned char B) {
+ return clip(0.299 * R + 0.587 * G + 0.114 * B);
+}
+
+// Cb = - 0.1687 R - 0.3313 G + 0.5 B + 128
+unsigned char RGB2Cb (unsigned char R, unsigned char G, unsigned char B) {
+ return clip(- 0.1687 * R - 0.3313 * G + 0.5 * B + 128);
+}
+
+//Cr = 0.5 R - 0.4187 G - 0.0813 B + 128
+unsigned char RGB2Cr (unsigned char R, unsigned char G, unsigned char B) {
+ return clip(0.5 * R - 0.4187 * G - 0.0813 * B + 128);
+}
+
+
+/* RGB -> YCbCr (also from JPEG to BMP) */
+// R = Y + 1.402 (Cr-128)
+unsigned char YCbCr2R (unsigned char Y, unsigned char Cr) {
+ return clip(Y + 1.402 *(Cr-128));
+}
+
+// G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)
+unsigned char YCbCr2G (unsigned char Y, unsigned char Cb, unsigned char Cr) {
+ return clip(Y - 0.34414 *(Cb-128) - 0.71414 *(Cr-128));
+}
+
+// B = Y + 1.772 (Cb-128)
+unsigned char YCbCr2B (unsigned char Y, unsigned char Cb) {
+ return clip(Y + 1.772 *(Cb-128));
+}
+
-unsigned char Image1[MAXCOLS*3*MAXROWS];
int main(int argc, char *argv[]) {
int retval, i;
int workspace[DCTSIZE_BLOCK];
- int type;
- int x, y, offset, depth;
+ int type, hCounter = 0;
+ int x_size, y_size, offset, depth;
char *fileIn, *fileOut;
FILE *fI, *fO;
unsigned char ch, mrgb[3];
+ unsigned int aux;
+/*******************************************************************************************/
/* Just for testing on the PC */
if(argc != 4)
{
@@ -38,41 +89,71 @@ int main(int argc, char *argv[]) {
return 0;
}
- switch(type) {
- case ENCODE:
- fseek(fI, 10L, SEEK_SET);
- fread(mrgb, 1, 4, fI);
- offset = mrgb[3];
- offset <<= 8;
- offset += mrgb[2];
- offset <<= 8;
- offset += mrgb[1];
- offset <<= 8;
- offset += mrgb[0];
-
+ /* Read the max possible size to the memory */
+ fread(Image1, sizeof(unsigned char), (MAXROWS*MAXCOLS*3+HEADER), fI);
+/*******************************************************************************************/
- fseek(fI, 18L, SEEK_SET);
- fread(mrgb, 1, 4, fI);
- x = ((int)mrgb[3] << 24) + ((int)mrgb[2] << 16) + ((int)mrgb[1] << 8) + mrgb[0];
- fseek(fI, 22L, SEEK_SET);
- fread(mrgb, 1, 4, fI);
- y = ((int)mrgb[3] << 24) + ((int)mrgb[2] << 16) + ((int)mrgb[1] << 8) + mrgb[0];
- fseek(fI, 28L, SEEK_SET);
- fread(mrgb, 1, 2, fI);
- depth = ((int)mrgb[1] << 8) + mrgb[0];
+/* The image must be in the memory by now */
- printf("Image Resolution: %d x %d x %i \n", x, y, depth);
-
- fseek(fI, offset, SEEK_SET);
+ switch(type) {
+ case ENCODE:
+ offset = ((int)Image1[13] << 24) + ((int)Image1[12] << 16) + ((int)Image1[11] << 8) + Image1[10];
+ x_size = ((int)Image1[21] << 24) + ((int)Image1[20] << 16) + ((int)Image1[19] << 8) + Image1[18];
+ y_size = ((int)Image1[25] << 24) + ((int)Image1[24] << 16) + ((int)Image1[23] << 8) + Image1[22];
+ depth = ((int)Image1[29] << 8) + Image1[28];
- fread(Image1, sizeof(unsigned char), (x*y*(depth/3)), fI);
+/**/ printf("Image Resolution: %d x %d x %i Offset: %i\n", x_size, y_size, depth, offset);
break;
case DECODE:
- break;
- }
-/* End for just for testing on the PC */
+ /* First find the SOI marker: */
+ aux = get_size(hCounter); hCounter+=2;
+ if (aux != SOI_MK)
+ { printf("INFO:\tNO SOI marker!\n"); return 0;}
+
+ printf("%ld:\tINFO:\tFound the SOI marker!\n", hCounter);
+
+ /* Read first marker */
+ while( aux != SOF_MK)
+ {
+ aux = get_size(hCounter); hCounter+=2;
+ if(aux == SOF_MK)
+ {
+ get_size(hCounter); hCounter+=2; /* header size, don't care */
+
+ /* load basic image parameters */
+ hCounter++; /* precision, 8bit, don't care */
+ y_size = get_size(hCounter); hCounter+=2;
+ x_size = get_size(hCounter); hCounter+=2;
+
+ printf("\tINFO:\tImage size is %d by %d\n", x_size, y_size);
+
+ depth = Image1[hCounter]; /* # of components */
+
+ printf("\tINFO:\t");
+ switch (depth)
+ {
+ case 1:
+ printf("Monochrome");
+ break;
+ case 3:
+ printf("Color");
+ break;
+ default:
+ printf("Not a");
+ break;
+ }
+ printf(" JPEG image!\n");
+
+ return 0;
+ }
+ else
+ {
+ hCounter = hCounter + get_size(hCounter);
+ }
+ }
+}
retval = 0;
@@ -112,3 +193,4 @@ int main(int argc, char *argv[]) {
return retval;
}
+