使用缓冲区读取JPEG文件:segfault

时间:2015-11-20 11:51:14

标签: c libjpeg

基本上我试图为读取JPEG文件编写代码(使用libjpeg),但我有段错误,我想知道是否有人能看到我的错误在哪里?我确定它来自malloc,但我不知道。我尝试通过放置一些printf进行一些调试但是它们绝对没有出现,wtf?

感谢您的帮助伙伴......

main.c:

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


int main (int argc, char** argv){


    int *H;
    int *W;
    int *C;
    printf("COUCOUCOUCOUCOU");
    FILE *fichier = NULL;
    char car;

    fichier = fopen("cara.jpg", "r");

    if (fichier == NULL)
        printf("Probleme lecture");


    lire(fichier, H, W, C);

    fclose(fichier);
    return 0;
}   

lire.c:

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

unsigned char** lire (FILE* file, int *H, int *W, int *C){

struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

int n = 0;
unsigned char** buffer;

printf("SHITSHITSHITSHIT\n");
fflush(stdout);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo); // Initialisation de la structure

jpeg_stdio_src(&cinfo,file);  // file est de type FILE * (descripteur de fichier
                              // sur le fichier jpega decompresser)
jpeg_read_header(&cinfo,TRUE);// lecture des infos sur l'image jpeg


jpeg_start_decompress(&cinfo);// lancement du processus de decompression


*H = cinfo.output_height;
*W = cinfo.output_width;
*C = cinfo.output_components;

buffer=(unsigned char **)malloc( (*H) *sizeof(unsigned char*) );

while (n < *H)
 {
    buffer[n] = (unsigned char*) malloc( (*W) * (*C) *sizeof(unsigned char *) );
        printf("DEBUG\n");
    fflush(stdout);
     jpeg_read_scanlines(&cinfo,buffer+n,1); // lecture des n lignes suivantes de l'image
                                          // dans le buffer (de type unsigned char *)
     n++;
}

jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);

return buffer;
}

我编译:

gcc -c -I/usr/local/include *.c
gcc *.o -o programme -ljpeg

1 个答案:

答案 0 :(得分:2)

当您致电lire时,您将H,W和C传递给该功能。这些是指针,它们未初始化,因此您在lire中遇到崩溃。

*H = cinfo.output_height;

您需要像这样调用lire函数:

int H;
int W;
int C;

....

lire(fichier, &H, &W, &C);
相关问题