Main MRPT website > C++ reference for MRPT 1.9.9
jdatadst.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
11 #include "jinclude.h"
12 #include "mrpt_jpeglib.h"
13 #include "jerror.h"
14 
15 /* Expanded data destination object for stdio output */
16 
17 typedef struct
18 {
19  struct jpeg_destination_mgr pub; /* public fields */
20 
21  FILE* outfile; /* target stream */
22  JOCTET* buffer; /* start of buffer */
24 
26 
27 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
28 
29 /*
30  * Initialize destination --- called by jpeg_start_compress
31  * before any data is actually written.
32  */
33 
34 METHODDEF(void)
36 {
37  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
38 
39  /* Allocate the output buffer --- it will be released when done with image
40  */
41  dest->buffer = (JOCTET*)(*cinfo->mem->alloc_small)(
43 
44  dest->pub.next_output_byte = dest->buffer;
46 }
47 
48 /*
49  * Empty the output buffer --- called whenever buffer fills up.
50  *
51  * In typical applications, this should write the entire output buffer
52  * (ignoring the current state of next_output_byte & free_in_buffer),
53  * reset the pointer & count to the start of the buffer, and return TRUE
54  * indicating that the buffer has been dumped.
55  *
56  * In applications that need to be able to suspend compression due to output
57  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
58  * In this situation, the compressor will return to its caller (possibly with
59  * an indication that it has not accepted all the supplied scanlines). The
60  * application should resume compression after it has made more room in the
61  * output buffer. Note that there are substantial restrictions on the use of
62  * suspension --- see the documentation.
63  *
64  * When suspending, the compressor will back up to a convenient restart point
65  * (typically the start of the current MCU). next_output_byte & free_in_buffer
66  * indicate where the restart point will be if the current call returns FALSE.
67  * Data beyond this point will be regenerated after resumption, so do not
68  * write it out when emptying the buffer externally.
69  */
70 
71 METHODDEF(boolean)
73 {
74  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
75 
76  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
77  (size_t)OUTPUT_BUF_SIZE)
78  ERREXIT(cinfo, JERR_FILE_WRITE);
79 
80  dest->pub.next_output_byte = dest->buffer;
82 
83  return TRUE;
84 }
85 
86 /*
87  * Terminate destination --- called by jpeg_finish_compress
88  * after all data has been written. Usually needs to flush buffer.
89  *
90  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
91  * application must deal with any cleanup that should happen even
92  * for error exit.
93  */
94 
95 METHODDEF(void)
97 {
98  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
99  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
100 
101  /* Write any data remaining in the buffer */
102  if (datacount > 0)
103  {
104  if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
105  ERREXIT(cinfo, JERR_FILE_WRITE);
106  }
107  fflush(dest->outfile);
108  /* Make sure we wrote the output file OK */
109  if (ferror(dest->outfile)) ERREXIT(cinfo, JERR_FILE_WRITE);
110 }
111 
112 /*
113  * Prepare for output to a stdio stream.
114  * The caller must have already opened the stream, and is responsible
115  * for closing it after finishing compression.
116  */
117 
118 GLOBAL(void)
120 {
121  my_dest_ptr dest;
122 
123  /* The destination object is made permanent so that multiple JPEG images
124  * can be written to the same file without re-executing jpeg_stdio_dest.
125  * This makes it dangerous to use this manager and a different destination
126  * manager serially with the same JPEG object, because their private object
127  * sizes may be different. Caveat programmer.
128  */
129  if (cinfo->dest == nullptr)
130  { /* first time for this JPEG object? */
131  cinfo->dest = (struct jpeg_destination_mgr*)(*cinfo->mem->alloc_small)(
133  }
134 
135  dest = (my_dest_ptr)cinfo->dest;
136  dest->pub.init_destination = init_destination;
137  dest->pub.empty_output_buffer = empty_output_buffer;
138  dest->pub.term_destination = term_destination;
139  dest->outfile = outfile;
140 }
FILE * outfile
Definition: mrpt_jpeglib.h:908
#define JPOOL_PERMANENT
Definition: mrpt_jpeglib.h:749
term_destination(j_compress_ptr cinfo)
Definition: jdatadst.cpp:96
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define ERREXIT(cinfo, code)
Definition: jerror.h:451
#define SIZEOF(object)
Definition: jinclude.h:74
init_destination(j_compress_ptr cinfo)
Definition: jdatadst.cpp:35
#define JFWRITE(file, buf, sizeofbuf)
Definition: jinclude.h:84
struct jpeg_destination_mgr pub
Definition: jdatadst.cpp:19
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
my_destination_mgr * my_dest_ptr
Definition: jdatadst.cpp:25
#define OUTPUT_BUF_SIZE
Definition: jdatadst.cpp:27
#define TRUE
Definition: jmorecfg.h:219
jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
Definition: jdatadst.cpp:119
empty_output_buffer(j_compress_ptr cinfo)
Definition: jdatadst.cpp:72
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define METHODDEF(type)
Definition: jmorecfg.h:173
char JOCTET
Definition: jmorecfg.h:106



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019