summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 39d99bbfb782811bb91c3368c6a0af8b5fa97f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include "dct.h"
#include "bmpjpeg.h"
#include "bmp.h"
#include "jpeg.h"

#define ENCODE 0
#define DECODE 1
#define MAXROWS 1200
#define MAXCOLS 800
#define HEADER	54

unsigned char Image1[MAXCOLS*3*MAXROWS+HEADER];
unsigned char Image2[MAXCOLS*3*MAXROWS+HEADER];


int main(int argc, char *argv[]) {
	int retval, i,j;
	int workspace[DCTSIZE_BLOCK];
	unsigned char blok8x8[64*3];
	unsigned char type;
	unsigned int x_size, y_size, offset;
	unsigned char depth;

	char *fileIn, *fileOut;

	FILE *fI, *fO;



/*******************************************************************************************/
/*	Just for testing on the PC		*/
	if(argc != 4)
	{
		printf("need more arguments, 0|1 for de/encrypt and 2 more for the in/ouput files");
		return 0;
	}

  	type = atoi(argv[1]);
    fileIn = argv[2];
  	fileOut = argv[3];

  	if (NULL == (fI = fopen(fileIn, "rb"))){
  	  printf("unable to open the file to read from (%s)",fileIn);
  	  return 0;
  	}
  	if ((fO = fopen(fileOut,"wb")) == NULL) {
  	  printf("unable to open the file to write (%s)",fileOut);
  	  return 0;
  	}

	/*		Read the max possible size to the memory			*/
	fread(Image1, sizeof(unsigned char), (MAXROWS*MAXCOLS*3+HEADER), fI);
/*******************************************************************************************/

/* The image must be in the memory by now	*/

//	retval = 0;

	GetBMPHeaderInfo (Image1,&offset,&x_size,&y_size,&depth);
	/*printf("Before:");
	for(i=0;i<(x_size*y_size*3+offset);i++)
	{
		if((i%24)==0) printf("\n");
		printf("%02x ",Image1[i]);
	}
*/

	switch(type) {
	case ENCODE:
	//	GetBMPHeaderInfo (Image1,&offset,&x_size,&y_size,&depth);
		printf("Image Resolution: %d x %d x %i Offset: %i\n", x_size, y_size, depth, offset);
		for(i=0;i<x_size;i+=8)
		{
			for(j=0;j<y_size;j+=8)
			{
				//printf("\nImage Resolution: %i %i\n", i,j);
				// RGB -> YUV & Convert to 8x8 --> done
				BMPto8x8AndYCbCr(i,j,x_size,y_size,offset,depth,Image1,blok8x8);
				// DCT
				fdct(blok8x8);
				//fdct(workspace);

				// ZZ

				// Q

				// VLC (huffman)

				YCbCrAnd8x8toBMP(i,j,x_size,y_size,offset,depth,Image2,blok8x8);
			}
		}

	break;
	case DECODE:
		GetJPEGHeaderInfo (Image1,&offset,&x_size,&y_size,&depth);
		for(i=0;i<x_size;i+=8)
		{
			for(j=0;j<y_size;j+=8)
			{

			// VLD (huffman)

			// IQ

			// ZZ

			// IDCT
			//idct(workspace);
			// YUV -> RGB & Convert 8x8 to BMP style &
			YCbCrAnd8x8toBMP(i,j,x_size,y_size,offset,depth,Image2,blok8x8);
		}
		}
		 WriteBMPHeaderInfo(Image2,offset,x_size,y_size, depth);

	break;
	}
	WriteBMPHeaderInfo(Image2,offset,x_size,y_size, depth);
	//Write Image to the output file.
	fwrite(Image2, sizeof(unsigned char), (x_size*y_size*(depth/8))+offset, fO);
	fclose(fI);
	fclose(fO);

	return retval;
}