2016-08-18 3 views
1

원하는 내용 :

기존의 jpeg를 새 것으로 작게 만듭니다.JPEG로 읽고 크기를 조정하여 디스크에 저장하십시오.

예 : before_3000x2000.jpeg →→→ after_1024x768.jpeg

나는 이와 같은 유사 기능이 필요합니다 생각 :

foo는 (before_file_path, after_file_path, new_width, new_height)

decompress_jpeg의 사용 (before_f하게

내가 뭔가를 잘못 이해하는 경우 ile_path)

크기 조정 (decompressed_data, new_width, new_height)

compress_jpeg (resized_data)

save_jpeg (compressed_data, after_file_path)

내 접근 방식을 수정하십시오.

나는 가정 일 : 해상도가 달라집니다

  • 때문에, 먼저 제대로 픽셀 데이터로 작업 할 수 있도록 기존의 JPEG 압축을 해제해야합니다.
  • 둘째, 픽셀 데이터를 foo() 함수를 사용하여 더 작은 버퍼로 크기를 조정합니다.
  • 셋째, 버퍼를 압축하여 디스크에 저장합니다. 내가 시도 (실패) 무엇

: 우선 들어

, 내가 제공

write_JPEG_file (문자 * 파일 이름, INT 품질)

기능을 장난 시도 in example.cjpeglib-9b. 한 가지 색상으로 지정하는 해상도로 테스트 그림을 작성하고 싶습니다. 하지만 문제가 생겼어. I 생각해 나는 임의의 메모리를 쓰고 있는데 그 이유를 모른다. (여기에 그림이 있습니다 : img_100x100) 맨 위의 특정 지점까지 흰 색이 있음을 알 수 있습니다. 이 부분은 첫 번째 루프에서 할당 한 내용에 따라 변경됩니다. 그러나 일정량이 지나면 랜덤 노이즈로 바뀝니다. 왜?

나는 변경하는 것 :

나는
image_width, image_height, image_buffer 내가 fopen_s(...) 모든 fopen(...) 대체 정의.
내가

jpeg_set_quality(...) 100에 품질을 하드 코딩 코드 :

#include <iostream> 
#include "jpeg-9b\jpeglib.h" 
#include "jpeg-9b\jerror.h" 

int TEST_WRITE_JPEG(char* file_name) 
{ 
    // DEFINE A PIC HERE 
    const int image_width = 100;  /* Number of columns in image */ 
    const int image_height = 100; /* Number of rows in image */ 
    JSAMPLE* image_buffer = new JSAMPLE[image_width * image_height](); /* Points to large array of R,G,B-order data */ 
    for (int i = 0; i < image_width*image_height; i++) 
    { 
     image_buffer[i] = 255; 
    } 

    /* Step 1: allocate and initialize JPEG compression object */ 
    struct jpeg_compress_struct cinfo; 
    struct jpeg_error_mgr jerr; 
    FILE * outfile; 
    JSAMPROW row_pointer[1]; 
    int row_stride; 

    cinfo.err = jpeg_std_error(&jerr); 
    jpeg_create_compress(&cinfo); 

    /* Step 2: specify data destination (eg, a file) */ 
    if (fopen_s(&outfile, file_name, "wb") != NULL) 
    { 
     fprintf(stderr, "can't open %s\n", file_name); 
     exit(1); 
    } 
    jpeg_stdio_dest(&cinfo, outfile); 

    /* Step 3: set parameters for compression */ 
    cinfo.image_width = image_width; /* image width and height, in pixels */ 
    cinfo.image_height = image_height; 
    cinfo.input_components = 3;   /* # of color components per pixel */ 
    cinfo.in_color_space = JCS_RGB;  /* colorspace of input image */ 

    jpeg_set_defaults(&cinfo); 
    jpeg_set_quality(&cinfo, 100, TRUE); 

    /* Step 4: Start compressor */ 
    jpeg_start_compress(&cinfo, TRUE); 

    /* Step 5: while (scan lines remain to be written) */ 
    row_stride = image_width * 3; 

    while (cinfo.next_scanline < cinfo.image_height) 
    { 
     row_pointer[0] = (JSAMPROW)&image_buffer[cinfo.next_scanline * row_stride]; 
     (void)jpeg_write_scanlines(&cinfo, row_pointer, 1); 
    } 

    /* Step 6: Finish compression */ 
    jpeg_finish_compress(&cinfo); 
    /* After finish_compress, we can close the output file. */ 
    fclose(outfile); 

    /* Step 7: release JPEG compression object */ 
    /* This is an important step since it will release a good deal of memory. */ 
    jpeg_destroy_compress(&cinfo); 

    return 0; 
} 

지우기 질문 :

내가 this 사진을 쓰는 이유는 무엇입니까

아닌 일반 흰색 하나?
image_buffer에 128x128 이상의 부호없는 문자가 포함되어있는 경우 프로그램이 중단되는 이유는 무엇입니까?

+0

당신은 이것을 원하지 않습니다. 낮은 Q 값으로 저장하려고합니다.이 방법은 데이터를 픽셀 화합니다. – EJP

+0

@EJP 특별히 구체적으로하고 싶지 않습니까? 크기 조정 부분에 대해 말씀하시는 것 같습니까? 정교하게 만들어 주시겠습니까? –

답변

1

각 픽셀에 3 바이트가 사용되므로 image_width * image_height 개의 요소 만 할당하는 동안 3 * image_width * image_height 개의 요소를 할당해야합니다. 충분한 요소를 할당하고 초기화하십시오.

+0

예, 제출 한 후 3 초가 지나면 즉시 보았습니다. 와우, 창피하다. 고마워요! 그게 모든 것을 고쳤습니다. 크래시가 없으며 큰 크기에서도 완벽하게 작동합니다. 너무 일찍부터 답을 수락 할 수 없습니다. –

관련 문제