how can i read jpg format images (or) pixel values
Printable View
how can i read jpg format images (or) pixel values
First of all, let's assume that you're not using TurboC as your compiler.
That being the case, the easiest way to read a JPG file into a format where you can read pixels is to use a library.
libjpeg - Wikipedia
Yes, you can use fopen() to open the file, and fread() to read the whole file into memory.
But extracting pixels from the binary data involves a LOT of work, and a LOT of very serious maths.
If you want reasonable results in anything less than 6 months, then use a library.
Here's code for loading JPEGs and all the other popular file formats for good measure.
GitHub - MalcolmMcLean/babyxrc: Resource compiler, packages images, fonts, cursors, etc as compilable C source. Portable ANSI C.
stb_image makes reading images quite painless. It's just a single header file you include in your own code, and that's it. Also it's a public domain license.
Here's a dumb example of reading the first 10 pixels of a JPG file...
Code:#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const size_t NUM_PIXELS_TO_PRINT = 10U;
int main(void) {
int width, height, comp;
unsigned char *data = stbi_load("image.jpg", &width, &height, &comp, 0);
if (data) {
printf("width = %d, height = %d, comp = %d (channels)\n", width, height, comp);
for (size_t i = 0; i < NUM_PIXELS_TO_PRINT * comp; i++) {
printf("%02x%s", data[i], ((i + 1) % comp) ? "" : "\n");
}
printf("\n");
}
return 0;
}