JPEG to 2d array

Discussion in 'C Programming' started by OTRJOKE@gmail.com, Jan 30, 2012.

  1. Guest

    I am trying to find an efficient way to read a jpeg into a 2d array.
    I'd like to have a number represent each pixels.
     
    , Jan 30, 2012
    #1
    1. Advertising

  2. John Gordon Guest

    In <> "" <> writes:

    > I am trying to find an efficient way to read a jpeg into a 2d array.
    > I'd like to have a number represent each pixels.


    What are you having trouble with specifically?

    Opening/reading the jpeg file?
    Interpreting the file contents?
    Declaring a 2d array?
    Something else?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jan 30, 2012
    #2
    1. Advertising

  3. BartC Guest

    "" <> wrote in message
    news:...
    > I am trying to find an efficient way to read a jpeg into a 2d array.
    > I'd like to have a number represent each pixels.


    Do you already have an *inefficient* way to read a jpeg..?

    Jpeg is a complex format, most would use an existing library to do the bulk
    of the work.

    Once you have the library, it should make clear how the image data is read
    (converted into uncompressed representation in memory), then it will be
    easier to see how to convert/adapt that to an array format accessible from
    C.

    --
    Bartc
     
    BartC, Jan 30, 2012
    #3
  4. On Jan 30, 10:23 pm, "" <> wrote:
    > I am trying to find an efficient way to read a jpeg into a 2d array.
    > I'd like to have a number represent each pixels.
    >

    Buy a copy of Basic Algorithms, by me. It contains a fully working
    JPEG encoder/decoder, plus all the description of how the file format
    works.

    --
    Visit my website
    http://www.malcommclean.site11.com/www
     
    Malcolm McLean, Jan 30, 2012
    #4
  5. On Jan 30, 5:23 pm, "" <> wrote:
    > I am trying to find an efficient way to read a jpeg into a 2d array.
    > I'd like to have a number represent each pixels.


    As has been said earlier, you probably want a library.

    I'm not an expert or a professional programmer, but, hoping this
    could help, I've written a simple program that does what you
    explain using the SDL and SDL_image libraries. (Since I am not
    an expert, there may be subtle errors in my program).

    The code loads a JPEG into a matrix of 32-bit integers and then
    prints out this matrix.
    (It assumes JPEGs no bigger than 512x512 pixels).

    -----------------------------------------------------------------

    #include "SDL.h"
    #include "SDL_image.h"

    /* the getpixel routine below was copied from the SDL documentation */

    /*
    * Return the pixel value at (x, y)
    * NOTE: The surface must be locked before calling this!
    */
    Uint32 getpixel(SDL_Surface *surface, int x, int y)
    {
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to retrieve */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x *
    bpp;

    switch(bpp) {
    case 1:
    return *p;

    case 2:
    return *(Uint16 *)p;

    case 3:
    if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
    return p[0] << 16 | p[1] << 8 | p[2];
    else
    return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
    return *(Uint32 *)p;

    default:
    return 0; /* shouldn't happen, but avoids warnings */
    }
    }

    int main(int argc, char** argv)
    {
    Uint32 image[512][512];
    SDL_Surface *surf;
    int i, j;

    if(!(surf = IMG_Load("image.jpeg")))
    {
    printf("could not load the image\n");
    return 0;
    }

    for(i = 0; i < surf->w; i++)
    for(j = 0; j < surf->h; j++)
    image[j] = getpixel(surf, i, j);

    for(i = 0; i < surf->w; i++)
    {
    for(j = 0; j < surf->h; j++)
    printf("%d ", (int)(Uint32)image[j]);
    putchar('\n');
    }

    SDL_FreeSurface(surf);
    return 0;
    }

    -----------------------------------------------------------------

    Bl0ckeduser
     
    Bl0ckeduser ., Jan 30, 2012
    #5
  6. Paul Guest

    wrote:
    > I am trying to find an efficient way to read a jpeg into a 2d array.
    > I'd like to have a number represent each pixels.


    There are free image conversion packages, such as
    pbmplus, netpbm, imagemagick, which may make reference
    to a JPEG library.

    http://netpbm.sourceforge.net/doc/jpegtopnm.html

    The library comes from this group.

    http://www.ijg.org/

    If you want real efficiency, this looks interesting...

    http://en.wikipedia.org/wiki/Libjpeg

    "libjpeg-turbo

    libjpeg-turbo is a fork of libjpeg that uses SIMD instructions
    to accelerate JPEG encoding and decoding. Fedora 14 switched to
    libjpeg-turbo in 2010. Mozilla switched to libjpeg-turbo in Firefox 5.0.
    Chrome 11 also has switched to this version"

    And everyone knows, if you put the word "turbo" in your software,
    it gets an extra 15 horsepower :)

    Paul
     
    Paul, Jan 31, 2012
    #6
  7. Avatar Guest

    Interpreting the file contents

    On Jan 30, 5:31 pm, John Gordon <> wrote:
    > In <> "" <> writes:
    >
    > > I am trying to find an efficient way to read a jpeg into a 2d array.
    > > I'd like to have a number represent each pixels.

    >
    > What are you having trouble with specifically?
    >
    > Opening/reading the jpeg file?
    > Interpreting the file contents?
    > Declaring a 2d array?
    > Something else?
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"
     
    Avatar, Feb 4, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Nation

    How to send a small jpeg to a newsgroup?

    Jack Nation, Jul 21, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    800
    Chad Z. Hower aka Kudzu
    Jul 25, 2005
  2. walala
    Replies:
    0
    Views:
    8,730
    walala
    Aug 1, 2003
  3. Replies:
    2
    Views:
    517
  4. lovaspillando
    Replies:
    0
    Views:
    1,032
    lovaspillando
    Aug 26, 2007
  5. Ivan Alameda Carballo
    Replies:
    0
    Views:
    494
    Ivan Alameda Carballo
    Aug 26, 2007
Loading...

Share This Page