Re: JPEG compression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Tue, Mar 04, 2003 at 11:45:34AM +0200, Turos Laszlo wrote:
> 
> Hi 
> 
> I have installed the The Independent JPEG Group's JPEG library to my 
> linux to use it for compressing captured video to JPEG format.
> Version:release 6b of 26-Mar-1998

Good.

> 
> It seems to me that it has this function jpeg_stdio_dest which takes a 
> FILE* pointer as argument for selecting destination.
> My problem is that I don't want to use a file as a destination for the 
> compressed image.
> Is it possible to set somehow a buffer for the JPEG compressor to use it 
> instead a FILE* stream?

Yes - it's possible.
Look at this code snippet, it compresses to dynamically allocated memory 
buffer:



static void memory_init_destination (j_compress_ptr cinfo);
static boolean memory_empty_output_buffer (j_compress_ptr cinfo);
static void memory_term_destination (j_compress_ptr cinfo);

struct my_destination_manager {
  struct jpeg_destination_mgr base;

  JOCTET *buffer;
  size_t currentBufferSize;
};

...

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

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

struct my_destination_manager destmgr;
destmgr.base.init_destination = memory_init_destination;
destmgr.base.empty_output_buffer = memory_empty_output_buffer;
destmgr.base.term_destination = memory_term_destination;
destmgr.buffer = 0;
cinfo.dest = (jpeg_destination_mgr *)&destmgr;

cinfo.image_width = ...;
cinfo.image_height = ...;

cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;

jpeg_set_defaults (&cinfo);

if (quality < 5) quality = 5;
if (quality > 100) quality = 100;
jpeg_set_quality (&cinfo, quality, TRUE);

jpeg_start_compress (&cinfo, TRUE);

jpeg_write_marker (&cinfo, JPEG_APP0 + 7,
                   (const JOCTET *)markertext,
                   strlen (markertext));

while (cinfo.next_scanline < cinfo.image_height) {

  ...

  JSAMPROW row_pointer [1];
  row_pointer[0] = (JSAMPLE *)linebuf; // output a line of pixels to codec
  jpeg_write_scanlines (&cinfo, row_pointer, 1);
}

jpeg_finish_compress (&cinfo);

// Get compressed image
*jpgstorage = ((my_destination_manager *)(cinfo.dest))->buffer;
*jpgstorsize = cinfo.dest->next_output_byte -
               ((my_destination_manager *)(cinfo.dest))->buffer;

jpeg_destroy_compress (&cinfo);

...



static void memory_init_destination (j_compress_ptr cinfo) {

  struct my_destination_manager *mydstmgr;
  mydstmgr = (struct my_destination_manager *)(cinfo->dest);

  if (mydstmgr->buffer != 0) {
    delete [] (mydstmgr->buffer);
  }

  mydstmgr->buffer = new JOCTET [INITIAL_BUFSIZE];
  mydstmgr->currentBufferSize = INITIAL_BUFSIZE;
    
  cinfo->dest->next_output_byte = mydstmgr->buffer;
  cinfo->dest->free_in_buffer = INITIAL_BUFSIZE;
}

static boolean memory_empty_output_buffer (j_compress_ptr cinfo) {

  // Allocate some more memory - it's dumb, but simple

  struct my_destination_manager *mydstmgr;
  mydstmgr = (struct my_destination_manager *)(cinfo->dest);

  JOCTET *oldbuffer;
  oldbuffer = mydstmgr->buffer;
  size_t delta = mydstmgr->currentBufferSize / 2;
  if (delta == 0) delta = 1;
  mydstmgr->buffer = new JOCTET [mydstmgr->currentBufferSize + delta];
  memcpy (mydstmgr->buffer, oldbuffer, mydstmgr->currentBufferSize);
  delete [] oldbuffer;
  cinfo->dest->next_output_byte =
    mydstmgr->buffer + mydstmgr->currentBufferSize;
  cinfo->dest->free_in_buffer = delta;
  mydstmgr->currentBufferSize = mydstmgr->currentBufferSize + delta;

  return TRUE;
}

static void memory_term_destination (j_compress_ptr cinfo) {
  // The buffer now contains full compressed JPEG image
}






> Or do I have an old library?

No - I'm using it too :)

-- 
Andrey Vasilyev





[Index of Archives]     [Linux DVB]     [Video Disk Recorder]     [Asterisk]     [Photo]     [DCCP]     [Netdev]     [Xorg]     [Util Linux NG]     [Xfree86]     [Free Photo Albums]     [Fedora Users]     [Fedora Women]     [ALSA Users]     [ALSA Devel]     [Linux USB]

Powered by Linux