Re: using libjpeg



On Jan 30, 6:47 am, oswald.ha...@xxxxxxxxx wrote:
hi
I have a char* imgnames[] that contain full path names of jpg files
(ie like "F:\myimgs\jpg\one.jpg",... etc) .I wish to read each
corresponding .jpg file and extract info into a struct ..and thus
create an array of structures.in each struct i would like to store
imgname,width,height,and value of each pixel(as a long?).

This should get you started. It uses libjpeg to read pixels from a
jpeg image. It reads from stdin, so use fopen() if you want to read
from a different file. The data is read in to image as RGBRGBRGB,
assuming the image is an RGB jpeg. It should be straightforward to
modify this to your needs.

#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>

int main()
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
unsigned char *image;

/*Initialize, open the JPEG and query the parameters */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, stdin);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);

/* allocate data and read the image as RGBRGBRGBRGB */
image = malloc(cinfo.output_width * cinfo.output_height * 3);
for(int i=0; i < cinfo.output_height; i++)
{
unsigned char * ptr = image + i * 3 * cinfo.output_width;
jpeg_read_scanlines(&cinfo, &ptr, 1);
}


/*Write a PPM */
printf("P6\n%i %i 255\n", cinfo.output_width, cinfo.output_height);
fwrite(image, 1, cinfo.output_width * cinfo.output_height * 3,
stdout);

return 0;
}


-Ed

--
(You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)

/d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage
.



Relevant Pages

  • using libjpeg
    ... I have a char* imgnamesthat contain full path names of jpg files ... create an array of structures.in each struct i would like to store ...
    (comp.unix.programmer)
  • Re: using libjpeg
    ... I have a char* imgnamesthat contain full path names of jpg files ... create an array of structures.in each struct i would like to store ...
    (comp.unix.programmer)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)
  • [PATCH] a tar filesystem for 2.6.*(easily access tar file)
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)