edit group selection and cut/copy/paste/del shortcuts, interview mode tweaks
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / jdatadst.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "jpeg.h"
4 #include "jpegwrapper.h"
5 #include "jerror.h"
6 #include "filemov.h"
7
8 /* Expanded data destination object for stdio output */
9
10 typedef struct {
11   struct jpeg_destination_mgr pub; /* public fields */
12
13   JOCTET *buffer;               // Pointer to buffer
14   thread_struct *buffer_struct;
15 } bcast_destination_mgr;
16
17 typedef bcast_destination_mgr *bcast_dest_ptr;
18
19
20 /*
21  * Initialize destination --- called by jpeg_start_compress
22  * before any data is actually written.
23  */
24
25 METHODDEF(void)
26 bcast_init_destination (j_compress_ptr cinfo)
27 {
28   bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest;
29
30 // Set the pointer to the preallocated buffer
31   dest->buffer = dest->buffer_struct->output_buffer;
32
33   dest->pub.next_output_byte = dest->buffer;
34   dest->pub.free_in_buffer = dest->buffer_struct->output_allocated;
35 }
36
37
38 /*
39  * Empty the output buffer --- called whenever buffer fills up.
40  *
41  * In typical applications, this should write the entire output buffer
42  * (ignoring the current state of next_output_byte & free_in_buffer),
43  * reset the pointer & count to the start of the buffer, and return TRUE
44  * indicating that the buffer has been dumped.
45  *
46  * In applications that need to be able to suspend compression due to output
47  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
48  * In this situation, the compressor will return to its caller (possibly with
49  * an indication that it has not accepted all the supplied scanlines).  The
50  * application should resume compression after it has made more room in the
51  * output buffer.  Note that there are substantial restrictions on the use of
52  * suspension --- see the documentation.
53  *
54  * When suspending, the compressor will back up to a convenient restart point
55  * (typically the start of the current MCU). next_output_byte & free_in_buffer
56  * indicate where the restart point will be if the current call returns FALSE.
57  * Data beyond this point will be regenerated after resumption, so do not
58  * write it out when emptying the buffer externally.
59  */
60
61 METHODDEF(boolean)
62 bcast_empty_output_buffer (j_compress_ptr cinfo)
63 {
64 // Allocate a bigger buffer.
65         bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest;
66         unsigned char *new_buffer;
67         int new_size, i;
68         int offset = dest->buffer_struct->output_allocated;
69
70         new_size = dest->buffer_struct->output_allocated * 2;
71         new_buffer = (unsigned char*)malloc(new_size);
72         for(i = 0; i < offset; i++)
73                 new_buffer[i] = dest->buffer_struct->output_buffer[i];
74         free(dest->buffer_struct->output_buffer);
75         dest->buffer_struct->output_buffer = new_buffer;
76         dest->buffer_struct->output_allocated = new_size;
77         dest->buffer = dest->buffer_struct->output_buffer;
78
79         dest->pub.next_output_byte = dest->buffer + offset;
80         dest->pub.free_in_buffer = dest->buffer_struct->output_allocated - offset;
81
82         return TRUE;
83 }
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)
96 bcast_term_destination (j_compress_ptr cinfo)
97 {
98 // Just get the length
99         bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest;
100         dest->buffer_struct->output_size = dest->buffer_struct->output_allocated - dest->pub.free_in_buffer;
101 }
102
103
104 /*
105  * Prepare for output to a stdio stream.
106  * The caller must have already opened the stream, and is responsible
107  * for closing it after finishing compression.
108  */
109
110 GLOBAL(void)
111 bcast_jpeg_buffer_dest (j_compress_ptr cinfo, thread_struct *buffer_struct)
112 {
113   bcast_dest_ptr dest;
114
115   /* The destination object is made permanent so that multiple JPEG images
116    * can be written to the same file without re-executing jpeg_stdio_dest.
117    * This makes it dangerous to use this manager and a different destination
118    * manager serially with the same JPEG object, because their private object
119    * sizes may be different.  Caveat programmer.
120    */
121   if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
122     cinfo->dest = (struct jpeg_destination_mgr *)
123       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
124                                   sizeof(bcast_destination_mgr));
125   }
126
127   dest = (bcast_dest_ptr) cinfo->dest;
128   dest->pub.init_destination = bcast_init_destination;
129   dest->pub.empty_output_buffer = bcast_empty_output_buffer;
130   dest->pub.term_destination = bcast_term_destination;
131   dest->buffer_struct = buffer_struct;
132 }