Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Reading JPG file - segmentation fault.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
conio123
n00b
n00b


Joined: 06 Nov 2008
Posts: 24

PostPosted: Sat Nov 15, 2008 10:43 pm    Post subject: [SOLVED] Reading JPG file - segmentation fault. Reply with quote

I am making a program which is using JpegLib that I have installed in my Gentoo.
Here is a code of a testing program:
Code:

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <jerror.h>

typedef struct jpeg_decompress_struct decInfo;
typedef struct imgSize {
  int width;
  int height;
} imgSize;

imgSize getImageSize(const char* filename) {
  decInfo cinfo;
  FILE* readMe;
  imgSize returningValue;
  if(!(readMe=fopen(filename, "rb")))
    exit(0);
  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo, readMe);
  printf("Before reading a header\n");
  jpeg_read_header(&cinfo, TRUE);
  printf("After reading a header\n");
  returningValue.width=cinfo.output_width;
  returningValue.height=cinfo.output_height;
  return returningValue;
}

int main(int argc, char** argv) {
  if(argc!=2) {
    printf("ARGC\n");
    return 0;
  }
  imgSize picture;
  picture=getImageSize(argv[1]);
  return 0;
}

When I run it with test.jpg file (it's a jpg file I downloaded from the Internet) I get:
Code:

konrad@kater ~/programJPG $ ./cinfo test.jpg
Before reading a header
Segmentation fault

What is wrong with it? Why do I get segmentation fault?


Last edited by conio123 on Sun Nov 16, 2008 12:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
MotivatedTea
Apprentice
Apprentice


Joined: 06 Nov 2006
Posts: 269
Location: Toronto, Canada

PostPosted: Sun Nov 16, 2008 12:20 am    Post subject: Reply with quote

You're missing some steps. The jpeglib source ships with example code showing how to read and write JPG files. Find jpegsrc.v6b.tar.gz in your distfiles directory, unpack it somewhere, and read through the file "example.c". Start by reading through the read_JPEG_file() function.
Back to top
View user's profile Send private message
conio123
n00b
n00b


Joined: 06 Nov 2008
Posts: 24

PostPosted: Sun Nov 16, 2008 9:28 am    Post subject: Reply with quote

Well, I did like that. The only thing I didn't "copy" from example file is error handling procedure with setjmp() functions - one of variables in cinfo struct was uninitialised. But, in my opinion, it shouldn't have an influence on a program.
Back to top
View user's profile Send private message
TNorthover
Guru
Guru


Joined: 25 Jan 2004
Posts: 434
Location: Edinburgh, UK

PostPosted: Sun Nov 16, 2008 10:03 am    Post subject: Reply with quote

conio123 wrote:
Well, I did like that. The only thing I didn't "copy" from example file is error handling procedure with setjmp() functions - one of variables in cinfo struct was uninitialised. But, in my opinion, it shouldn't have an influence on a program.

Have you tried initializing that to make sure? My experience of jpeglib is that it's ridiculously fragile.
Back to top
View user's profile Send private message
conio123
n00b
n00b


Joined: 06 Nov 2008
Posts: 24

PostPosted: Sun Nov 16, 2008 12:22 pm    Post subject: Reply with quote

Ok, setting jmp_buffer and initialising proper variables made the program working. Thanks :)
Back to top
View user's profile Send private message
otmane
n00b
n00b


Joined: 21 Jun 2010
Posts: 1

PostPosted: Mon Jun 21, 2010 2:53 pm    Post subject: Reply with quote

Hi, i m interested in what you did to find the make the program work. I tested with the piece of code you sent and i had the same behaviour : A segfault when we call the jpeg_read_headers.
Could you plz post the corrected code?

Thanks in advance
Back to top
View user's profile Send private message
ritzmax72
Tux's lil' helper
Tux's lil' helper


Joined: 10 Aug 2014
Posts: 84

PostPosted: Sat May 11, 2024 12:18 am    Post subject: Reply with quote

otmane wrote:
Hi, i m interested in what you did to find the make the program work. I tested with the piece of code you sent and i had the same behaviour : A segfault when we call the jpeg_read_headers.
Could you plz post the corrected code?

Thanks in advance


He added these
Code:

dInfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;

  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error.
     * We need to clean up the JPEG object, close the input file, and return.
     */
    jpeg_destroy_decompress(&dInfo);
    fclose(infile);
    fclose(outthumbfile);
    return;
  }



and defined my_error_exit

Code:


METHODDEF(void)
my_error_exit(j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_ptr myerr = (my_error_ptr)cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  (*cinfo->err->output_message) (cinfo);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}


Don't forget to include header:
Code:
 #include<setjmp.h>
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum