2012-05-07 2 views
2

RGB 데이터가 포함 된 버퍼를 jpeg로 변환하는 쉬운 방법이 필요합니다. libjpeg를 사용하여 이미 시도했지만, 제대로 작동하지는 않습니다. (A)에RGB 버퍼에서 JPEG 버퍼로 무엇이 잘못 되었습니까?

enter image description here

직접 화상을 저장 : 메모리 내의 동일한 화상을 인코딩 libjpeg 사용

Bitmap Screenshot

이 생성 : 예를 들어, 완충액을 저장하는 비트 맵이 생성로서 파일은 경고, 오류 또는 아무것도주지 않고 중단됩니다.

나는 확실히 작동하는 무언가가 필요합니다!

내가

void OnKeyPress(unsigned char key, int x, int y) { 
    if (key != static_cast<unsigned char>('p')) 
    return; 

    int width = g_current_width; 
    int height = g_current_height; 
    boost::scoped_array<boost::uint8_t> buffer(new boost::uint8_t[3 * width * height]); 

    glReadBuffer(GL_FRONT); 
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, 
       reinterpret_cast<GLvoid *>(buffer.get())); 
    glReadBuffer(GL_BACK); 

    FlipImage(buffer.get(), width, height); 

    // Generate a BMP files for testing purposes 
    SaveRGB("screenshot.bmp", buffer.get(), width, height); 

    jpeg_compress_struct cinfo; 
    jpeg_error_mgr jerr; 
    cinfo.err = jpeg_std_error(&jerr); 
    jerr.trace_level = 10; 
    jpeg_create_compress(&cinfo); 
    boost::uint8_t *jpeg_buffer_raw = NULL; 
    unsigned long outbuffer_size = 0; 
    jpeg_mem_dest(&cinfo, &jpeg_buffer_raw, &outbuffer_size); 

    cinfo.image_width = width; 
    cinfo.image_height = height; 
    cinfo.input_components = 3; 
    cinfo.in_color_space = JCS_RGB; 
    jpeg_set_defaults(&cinfo); 

    jpeg_set_quality(&cinfo, 100, true); 
    jpeg_start_compress(&cinfo, true); 
    int row_stride = width * 3; 
    JSAMPROW row_pointer[1]; 
    int counter = 0; 
    std::cout << boost::format("height: %d\n") % height; 
    boost::uint8_t *r_buffer = buffer.get(); 
    while (cinfo.next_scanline < cinfo.image_height) { 
    row_pointer[0] = &r_buffer[cinfo.next_scanline * row_stride]; 
    jpeg_write_scanlines(&cinfo, row_pointer, 1); 
    std::cout << boost::format("current line: %d\n") % (counter++); 
    } 
    jpeg_finish_compress(&cinfo); 
    jpeg_destroy_compress(&cinfo); 
    std::ofstream jpegfile("screenshot.jpg"); 
    jpegfile.write(reinterpret_cast<const char*>(jpeg_buffer_raw), outbuffer_size); 
    jpegfile.flush(); 
    // calling free(jpeg_buffer_raw); or delete[] jpeg_buffer_raw; generates an error 
} 
+3

방법입니다 당신 libjpeg 전화? 코드를 보여주세요. – Thomas

+1

SO는 질의 응답 사이트입니다. 귀하의 질문은 무엇인가? –

+0

여러분, 코드를 사용하여 질문을 업데이트합니다. – Sambatyon

답변

2

그래서 뭐하는 거지, 내가 문제를 발견하고 맨 끝에 있었다 출력 파일은 다음과 같이 초기화되어 있어야합니다 :

std::ofstream jpegfile("screenshot.jpg", std::ios_base::out | std::ios_base::binary); 
관련 문제