JPEG to 2d array

O

OTRJOKE

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.
 
J

John Gordon

In said:
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?
 
B

BartC

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.
 
M

Malcolm McLean

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.
 
B

Bl0ckeduser .

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;
}
 
P

Paul

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
 
A

Avatar

Interpreting the file contents

What are you having trouble with specifically?

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top