Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define W 384
- #define H 288
- #define Bytes 3
- #define SIZE (W*H*Bytes)
- // YUV444 -> RGB888
- // write new img to a file
- // get green value for X = 60, Y = 257
- // both have 3 Bpp
- //|R0|R1|R2|R3|G0|G1|G2|G3|B0|B1|B2|B3|
- FILE* openRead(char *path)
- {
- FILE *f;
- if( (f = fopen(path, "rb")) == NULL)
- {
- perror("can't open read file");
- exit(-2);
- }
- return f;
- }
- FILE* openWrite(char *path)
- {
- FILE *g;
- if((g = fopen(path, "wb")) == NULL)
- {
- perror("can't open write file");
- exit(-2);
- }
- return g;
- }
- int clamp(double x, int min, int max)
- {
- if(x < min)
- x = min;
- else
- if(x > max)
- x = max;
- return x;
- }
- void scan_image(int x, int y, char *path, char *newPath)
- {
- FILE *f = openRead(path);
- FILE *g = openWrite(newPath);
- int c, d, e, R, G, B;
- int Y, U, V;
- char newCharBuffer[W*H*3] = {0};
- fread(newCharBuffer, (W*H*3), 1, f);
- for(int i = 0; i < (W*H*3) ; i = i + 3) //cate 3 bytes Y, U, V
- {
- Y = newCharBuffer[i];
- U = newCharBuffer[i+1];
- V = newCharBuffer[i+2];
- if(i >= 0 && i <= 15)
- {
- printf("\nY = %d\n", Y);
- printf("U = %d\n", U);
- printf("V = %d\n", V);
- }
- c = Y - 16;
- d = U - 128;
- e = V - 128;
- R = clamp(((298*c + 409*e + 128) >> 8), 0, 255);
- G = clamp(((298*c - 100*d - 208*e + 128) >> 8), 0, 255);
- B = clamp(((298*c + 516*d + 128) >> 8), 0, 255);
- if(i >= 0 && i <= 15)
- {
- printf("\nR = %d\n", R);
- printf("G = %d\n", G);
- printf("B = %d\n", B);
- }
- newCharBuffer[i] = R;
- newCharBuffer[i+1] = G;
- newCharBuffer[i+2] = B;
- }
- fwrite(newCharBuffer, (W*H*3), 1, g); // bun
- fclose(f); fclose(g);
- }
- int main()
- {
- int x = 60;
- int y = 257;
- char path[] = "img2.raw";
- char newPath[] = "newImg.raw";
- scan_image(x, y, path, newPath);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement