OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJpegMemDest.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // License: See top level LICENSE.txt file
4 //
5 // Most of code and comments below are from jpeg-6b "example.c" file. See
6 // http://www4.cs.fau.de/Services/Doc/graphics/doc/jpeg/libjpeg.html
7 //
8 // Author: Oscar Kramer (From example by Thomas Lane)
9 //----------------------------------------------------------------------------
10 // $Id$
11 
13 #include <cstdlib> /* free, malloc, size_t */
14 #include <cstdio> /* FILE* */
15 #include <csetjmp>
16 #include <ostream>
17 #include <jpeglib.h>
20 /* *** Custom destination manager for JPEG writer *** */
21 
22 typedef struct
23 {
24  struct jpeg_destination_mgr pub; /* public fields */
25  std::ostream* stream; /* target stream */
26  JOCTET* buffer; /* start of buffer */
27 } cpp_dest_mgr;
28 
29 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
30 
31 void init_destination (j_compress_ptr cinfo)
32 {
33  cpp_dest_mgr* dest = (cpp_dest_mgr*) cinfo->dest;
34 
35  /* Allocate the output buffer --- it will be released when done with image */
36  dest->buffer = (JOCTET *)
37  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, OUTPUT_BUF_SIZE * sizeof(JOCTET));
38 
39  dest->pub.next_output_byte = dest->buffer;
40  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
41 }
42 
43 boolean empty_output_buffer (j_compress_ptr cinfo)
44 {
45  cpp_dest_mgr* dest = (cpp_dest_mgr*) cinfo->dest;
46  dest->stream->write ((char*)dest->buffer, OUTPUT_BUF_SIZE);
47 
48  dest->pub.next_output_byte = dest->buffer;
49  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
50 
51  return TRUE;
52 }
53 
54 
55 void term_destination (j_compress_ptr cinfo)
56 {
57  cpp_dest_mgr* dest = (cpp_dest_mgr*) cinfo->dest;
58  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
59 
60  /* Write any data remaining in the buffer */
61  if (datacount > 0)
62  dest->stream->write ((char*)dest->buffer, datacount);
63 
64  dest->stream->flush();
65  free (cinfo->dest);
66 }
67 
68 // void jpeg_cpp_stream_dest (j_compress_ptr cinfo, std::ostream& stream)
69 void jpeg_cpp_stream_dest(jpeg_compress_struct* cinfo, std::ostream& stream)
70 {
71  cpp_dest_mgr* dest;
72 
73  /* first time for this JPEG object? */
74  if (cinfo->dest == NULL)
75  cinfo->dest = (struct jpeg_destination_mgr *) malloc (sizeof(cpp_dest_mgr));
76 
77  dest = (cpp_dest_mgr*) cinfo->dest;
78  dest->pub.init_destination = init_destination;
79  dest->pub.empty_output_buffer = empty_output_buffer;
80  dest->pub.term_destination = term_destination;
81  dest->stream = &stream;
82 }
void jpeg_cpp_stream_dest(jpeg_compress_struct *cinfo, std::ostream &stream)
Method which uses memory instead of a FILE* to write to.
if(yy_init)
void init_destination(j_compress_ptr cinfo)
#define OUTPUT_BUF_SIZE
struct jpeg_destination_mgr pub
boolean empty_output_buffer(j_compress_ptr cinfo)
void term_destination(j_compress_ptr cinfo)
for jmp_buf
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
std::ostream * stream