2011-06-13 4 views
4

libjpeg может считывать данные JPEG из FILE* или буфера. Мои данные поступают от std::istream. Я мог бы прочитать весь std::istream в буфер для использования с libjpeg, но я бы предпочел, чтобы libjpeg читал напрямую из std::istream, если это возможно. Как это может быть сделано?Как использовать libjpeg для чтения JPEG из std :: istream?

+2

Это библиотека C ... как он может использовать 'std :: istream'? – Marlon

+3

@Marlon: Мост может быть выполнен, если библиотека имеет правильные крючки. Например, [libpng can] (http://www.piko3d.com/tutorials/libpng-tutorial-loading-png-files-from-streams). – uckelman

ответ

10

Вам просто нужно предоставить обертки вокруг вашего istream. Определения структуры, например

struct JpegStream { 
    jpeg_source_mgr pub; 
    std::istream* stream; 
    byte buffer [4096]; 
} 

Тогда вам нужно четыре метода для работы на потоке:

void init_source (j_decompress_ptr cinfo) 
{ 
    auto src = (JpegStream*)(cinfo->src); 
    src->stream-> // seek to 0 here 
} 

boolean fill_buffer (j_decompress_ptr cinfo) 
{ 
    // Read to buffer 
    JpegStream* src = // as above 
    src->pub.next_input_byte = src->buffer; 
    src->pub.bytes_in_buffer = // How many yo could read 

    return eof() ? FALSE : TRUE; 
} 

void skip (j_decompress_ptr cinfo, long count) 
{ 
    // Seek by count bytes forward 
    // Make sure you know how much you have cached and subtract that 
    // set bytes_in_buffer and next_input_byte 
} 

void term (j_decompress_ptr cinfo) 
{ 
    // Close the stream, can be nop 
} 

и один способ, чтобы связать их с декомпрессионной информации JPEG структуры:

void make_stream (j_decompress_ptr cinfo, std::istream* in) 
{ 
    JpegStream * src; 

    /* The source object and input buffer are made permanent so that a series 
    * of JPEG images can be read from the same file by calling jpeg_stdio_src 
    * only before the first one. (If we discarded the buffer at the end of 
    * one image, we'd likely lose the start of the next one.) 
    * This makes it unsafe to use this manager and a different source 
    * manager serially with the same JPEG object. Caveat programmer. 
    */ 
    if (cinfo->src == NULL) 
    { 
      /* first time for this JPEG object? */ 
      cinfo->src = (struct jpeg_source_mgr *) 
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, POOL_PERMANENT, sizeof(JpegStream)); 
      src = reinterpret_cast<JpegStream*> (cinfo->src); 
    } 

    src = reinterpret_cast<JpegStream*> (cinfo->src); 
    src->pub.init_source = init_source; 
    src->pub.fill_input_buffer = fill_buffer; 
    src->pub.skip_input_data = skip; 
    src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 
    src->pub.term_source = term; 
    src->stream = in; 
    src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ 
    src->pub.next_input_byte = NULL; /* until buffer loaded */ 
} 

После звонка jpeg_create_decompress вызовите функцию make_stream.

+0

Действительно ли 'fill_buffer' должен возвращать' bool'? Он объявлен 'void', но возвращает значение. – uckelman

+0

yes, a bool, извините – Anteru

+1

POOL_PERMANENT - JPOOL_PERMANENT в новом libjpeg – Andy

 Смежные вопросы

  • Нет связанных вопросов^_^