Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Image I/O in C

  • 06-02-2008 10:07am
    #1
    Registered Users, Registered Users 2 Posts: 2,327 ✭✭✭


    Hey,

    I'm trying to find out how I can do image i/o in C code. Basically I want to be able to read the image into an array with the value of each pixel (0-255) stored and be able to output from the array to an image after processing. Any suggestions ?


Comments

  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    you would do this the same way you would read in any file by using fopen

    The most important part though is the format the image is in.
    What sort of images are you looking at reading in?

    bitmaps, jpegs, pgm, gif, raw image data?


  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    the file i/o would be along the lines of this
    char myArray[1000]
    
    void main()
    {
    
        FILE *inputFilePtr; 
        inputFilePtr = fopen("afile.xxx", "rb");
    
       //then fread() by the required amount into your array
      
       fread(*myArray,elementSize,numberOfElements,*inputFilePtr)
    
       //process array now
    
      .............................
    
    }
    
    


  • Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭DublinDilbert


    If its a compressed format you'll need a library to de-compress it before you can modify any of the byte values in it, eg for JPEG you'll need to do an inverse DCT inverse quantisation etc...

    There are some un-compressed file formats which you should be able to access using straight file I/O, PGM (programable gray map) is one of these.... The likes on irfran view will read in a JPEG and output it as a PGM file for you.


  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    And with JPEG even before you get to any image data you will have to write yourself a nice entropy decoder to handle the variable code words used in the file container.

    You would be best finding some free library's if you want to work with these types of images. heres a trial one

    PGM's are great if you want to learn about images, but their everyday use is rather limited (They don't have colour)

    Bitmaps are very easy to work with and are very well documented. they have a small header, and are often uncompressed. (apart from maybe a small amout of run length encoding, but its often not implemented)
    They are also used a lot (in windows anyway)


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    This sounds like a college project you're just starting.

    Anyways I'd recommend for now using 24-bit bitmaps (so the colour RGB is one byte each) so there's feck all processing to do. 30 seconds will find you the read/write code for a 24-bit bitmap. And its less than 20 lines of code (which is very straight-forward).

    As other people have stated, with other formats they can become very complex, such as png, tiff, jpeg. There's often third part libraries for these (libpng and libjpeg) but it's down to whether or not you want to use them or write your own. Be prepared in the case of jpeg writing your own parser would be quite complex.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    silas wrote: »
    As other people have stated, with other formats they can become very complex, such as png, tiff, jpeg. There's often third part libraries for these (libpng and libjpeg) but it's down to whether or not you want to use them or write your own. Be prepared in the case of jpeg writing your own parser would be quite complex.

    Going back in time a bit (ok, a lot), I found that pcx was a drop dead simple format to implement.
    See: http://www.fileformat.info/format/pcx/

    (fileformat.info is pretty good - gif is there too along with others)

    Back in the day, bmp was too darn big and gif was too scary.

    Of course, there's nothing to stop you just dumping raw data to disk. Slap a header on it and that's practically bmp right there.

    Conor


Advertisement