image processing - How to convert a jpg to yuv in C? -
even though question of nature sounds similar, having problems in converting jpg image yuv in c (without using opencv).
this have understood of now, how solve problem :
- identify structure of file formats jpg , yuv. i.e each byte in file contains. think jpg format looks like.
- with above structure tried read jpg file , tried decipher 18th , 19th bytes. did type cast them both char , int don`t meaningful values width , height of image.
once have read these values, should able convert them jpg yuv. looking @ resource.
appropriately, construct yuv image , write (.yuv) file.
kindly me pointing me appropriate resources. keep updating progress on post. in advance.
based on https://en.wikipedia.org/wiki/yuv#y.27uv444_to_rgb888_conversion
decoding jpeg, in pure c without libraries ... following code straightforward ...
https://bitbucket.org/halicery/firerainbow-progressive-jpeg-decoder/src
assuming have jpeg decoded rgb using above or library (using library easier).
int width = (width of image); int height = (height of image); byte *mydata = (pointer rgb pixels); byte *cursor; size_t byte_count = (length of pixels .... i.e. width x height x 3); int n; (cursor = mydata, n = 0; n < byte_count; cursor += 3, n += 3) { int red = cursor[0], green = cursor[1], blue = cursor[2]; int y = 0.299 * red + 0.587 * green + 0.114 * blue; int u = -0.147 * red + -0.289 * green + 0.436 * blue; int v = 0.615 * red + -0.515 * green + -0.100 * blue; cursor[0] = y, cursor[1] = u, cursor[2] = v; } // @ point, entire image has been converted yuv ...
and write file ...
file* fout = fopen ("myfile.yuv, "wb"); if (fout) { fwrite (mydata, 1, byte_count, fout); fclose (fout); }
Comments
Post a Comment