2011-12-01 5 views
1

다음 libjpeg 버전의 RHEL 6.0 x86_64 상자에 있습니다.libjpeg RAW로 압축 해제하지 않음

[[email protected] jpeg_to_raw.c]$ rpm -qa libjpeg 
libjpeg-6b-46.el6.x86_64 

다음 코드는 .jpeg 파일을 입력 받아 .raw 파일을 작성합니다. 그러나 나는 IrfanView와 (및 관련 플러그인)을 사용하여 파일 내 이미지의 작은 부분을 열 때

[[email protected] jpeg_to_raw.c]$ ls -l 
total 600 
-rwxrwxr-x 1 mehoggan mehoggan 10113 Dec 1 10:32 jpeg_to_raw 
-rw-rw-r-- 1 mehoggan mehoggan 3311 Dec 1 10:32 jpeg_to_raw.c 
-rw-rw-r-- 1 mehoggan mehoggan  75 Dec 1 10:27 Makefile 
-rw-rw-r-- 1 mehoggan mehoggan 215205 Dec 1 09:19 test.jpg 
-rw-rw-r-- 1 mehoggan mehoggan 374850 Dec 1 10:32 test_out.raw 

: 나는 프로그램이 작동하고 있는게 틀림 없어 파일의 크기가 확장 프로그램을 실행할 때 열립니다. 코드는 아래에서 확인할 수 있습니다.

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

/* we will be using this uninitialized pointer later to store raw, uncompressd image */ 
unsigned char *raw_image = NULL; 
unsigned int size; 

/** 
* print the information for what was stored in the JPEG File 
**/ 
void print_jpeg_info(struct jpeg_decompress_struct cinfo) 
{ 
    printf("JPEG File Information: \n"); 
    printf("Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height); 
    printf("Color components per pixel: %d.\n", cinfo.num_components); 
    printf("Color space: %d.\n", cinfo.jpeg_color_space); 
    printf("Raw flag is: %d.\n", cinfo.raw_data_out); 
} 

/** 
* read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the 
* raw_image buffer in an uncompressed format. 
* 
* \returns positive integer if successful, -1 otherwise 
* \param *filename char string specifying the file name to read from 
**/ 
int read_jpeg_file(char *filename) 
{ 
    /* these are standard libjpeg structures for reading(decompression) */ 
    struct jpeg_decompress_struct cinfo; 
    struct jpeg_error_mgr jerr; 
    /* libjpeg data structure for storing one row, that is, scanline of an image */ 
    JSAMPROW row_pointer[1]; 
    FILE *infile = fopen(filename, "rb"); 
    unsigned long location = 0; 
    int i = 0; 
    if (!infile) { 
     printf("Error opening jpeg file %s\n!", filename); 
     return -1; 
    } 
    /* here we set up the standard libjpeg error handler */ 
    cinfo.err = jpeg_std_error(&jerr); 
    /* setup decompression process and source, then read JPEG header */ 
    jpeg_create_decompress(&cinfo); 
    /* this makes the library read from infile */ 
    jpeg_stdio_src(&cinfo, infile); 
    /* reading the image header which contains image information */ 
    jpeg_read_header(&cinfo, TRUE); 
    print_jpeg_info(cinfo); 
    jpeg_start_decompress(&cinfo); 

    /* allocate memory to hold the uncompressed image */ 
    size = cinfo.output_width*cinfo.output_height*cinfo.num_components; 
    raw_image = (unsigned char*)malloc(size); 
    /* now actually read the jpeg into the raw buffer */ 
    row_pointer[0] = (unsigned char *)malloc(cinfo.output_width*cinfo.num_components); 
    /* read one scan line at a time */ 
    while (cinfo.output_scanline < cinfo.image_height) { 
     jpeg_read_scanlines(&cinfo, row_pointer, 1); 
     for (i=0; i<cinfo.image_width*cinfo.num_components;i++) { 
      raw_image[location++] = row_pointer[0][i]; 
     } 
    } 
    /* wrap up decompression, destroy objects, free pointers and close open files */ 
    jpeg_finish_decompress(&cinfo); 
    jpeg_destroy_decompress(&cinfo); 
    free(row_pointer[0]); 
    fclose(infile); 
    /* yup, we succeeded! */ 
    return 1; 
} 

    int main(int argc, char *argv[]) 
    { 
     char *infilename = "test.jpg"; 
     if (read_jpeg_file(infilename) > 0) { 
      size_t count = size/sizeof(unsigned char*); 
      fprintf(stdout, "The number of unsigned chars in raw_image = %d\n", (int)count); 
      FILE *ofile = fopen("test_out.raw", "w+"); 
      ssize_t data_out = fwrite(raw_image, count, sizeof(unsigned char), ofile); 
      fprintf(stdout, "%d", (int)data_out); 
      fclose(ofile); 
     } 
     else 
      return -1; 
     return 0; 
    } 

프로그램이 모든 데이터를 쓰지 않는 이유는 무엇입니까? 아니면 왜 데이터를 손상시킬 수 있습니까?

간단한 애플리케이션을 구축하는 데 사용되는 메이크이다

jpeg_to_raw : jpeg_to_raw.c 
    gcc jpeg_to_raw.c -Wall -o jpeg_to_raw -ljpeg 

답변

2

를 size_t 카운트 = 사이즈 /를 sizeof (부호의 char *);

은 기록 된 원시 데이터의 1/4 만 가져옵니다 (실제로는 64 비트 모드에서 1/8). 개수는 크기와 동일해야합니다 (포인터가 아닌 문자 개수)

관련 문제