-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
66 lines (53 loc) · 1.88 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "load.c"
void main()
{
//load image
BMPIMAGE image;
load(&image, "parrot.bmp"); //bmp from paint not from paint.net cause paint.net doesn't save filesize in bi.biSizeImage
//convert image from RGB to YCbCr (convImage has no infos anymore, just image data)
YCbCrTRIPLE *convImage = convert(&image);
//free imagedata since not needed anymore
free(image.imageData);
//allocate memory for each component
BYTE *YImage = (BYTE *)malloc(image.bi.biSizeImage / 3);
BYTE *CbImage = (BYTE *)malloc(image.bi.biSizeImage / 3);
BYTE *CrImage = (BYTE *)malloc(image.bi.biSizeImage / 3);
for (int i = 0; i < image.bi.biHeight; i++)
{
for (int j = 0; j < image.bi.biWidth; j++)
{
//current position in array
int curPos = i * image.bi.biWidth + j;
//split into three separate arrays
YImage[curPos] = convImage[curPos].Y;
CbImage[curPos] = convImage[curPos].Cb;
CrImage[curPos] = convImage[curPos].Cr;
}
}
//free convImage, since not needed anymore
free(convImage);
//create output file
FILE *out = fopen("out.bmp", "wb");
//write fileheader
fwrite(&(image.bf), sizeof(BITMAPFILEHEADER), 1, out);
//write infoheader
fwrite(&(image.bi), sizeof(BITMAPINFOHEADER), 1, out);
//write pixels
//fwrite(convImage, image.bi.biSizeImage, 1, out); //to use this line instead of for loop move free from above to below
for (int i = 0; i<image.bi.biSizeImage / 3;i++)
{
fwrite(YImage + i,1, 1, out);
fwrite(CbImage + i,1, 1, out);
fwrite(CrImage + i,1, 1, out);
}
//close and free stuff
fclose(out);
free(YImage);
free(CbImage);
free(CrImage);
//quick way to see if ran to the end
printf("successful\n");
}