Thread: Reading a PPM image into a 1d or 2d array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19

    Reading a PPM image into a 1d or 2d array

    I have an assignment where I need to take an assigned image, rotate it 90 degrees to the right, flip it upside down, and turn it to grayscale; each with different outputs.

    I was thinking if I can read in the image into a 2d array //array[height][width]\\ or even just an array with the values of each pixel (r, g, b), I can just modify the array and spit it out when done.

    if i can just get it into the array, I think I'll be fine. We've done similar assignments with modifying arrays for grayscale images and the other two I can probably figure out. But how am I going to read in each pixel into an array with three different values in each slot?

    this is my code so far:
    Code:
    #include<stdio.h>
    I'm pretty lost.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    What you need to do is write two fucntions, one to laod a ppm file given the filename, and one to save an image in ppm format, given an image buffer.

    Store 24 bit images in flat buffers, allocated with malloc(width * height * 3). Then calcuate each pixel and channel offset usingthe formula offset = y *width*3 + x * 3 + channel. Then write fucntions to maniopulate those buffers - rotate they, greyscale them, etc.
    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


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BMP image files reading
    By igor in forum C Programming
    Replies: 24
    Last Post: 09-10-2012, 07:19 PM
  2. Image reading and writing
    By jawad242 in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2011, 10:07 AM
  3. Reading an image
    By drrcknlsn in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2008, 10:21 PM
  4. Fat Image reading
    By cfrost in forum C++ Programming
    Replies: 0
    Last Post: 03-05-2005, 06:06 AM
  5. Reading a bitmap image
    By usamaalam in forum C Programming
    Replies: 19
    Last Post: 03-06-2004, 06:13 AM