Thread: reading image

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    reading image

    how can i read jpg format images (or) pixel values

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    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.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a PPM image into a 1d or 2d array
    By Robert Harp in forum C Programming
    Replies: 1
    Last Post: 04-04-2013, 10:18 AM
  2. BMP image files reading
    By igor in forum C Programming
    Replies: 24
    Last Post: 09-10-2012, 07:19 PM
  3. Image reading and writing
    By jawad242 in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2011, 10:07 AM
  4. Reading an image
    By drrcknlsn in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2008, 10:21 PM
  5. Fat Image reading
    By cfrost in forum C++ Programming
    Replies: 0
    Last Post: 03-05-2005, 06:06 AM

Tags for this Thread