Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / src / modules / mod_jpeg / trgt_jpeg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_jpeg.cpp
3 **      \brief jpeg_trgt Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "trgt_jpeg.h"
38 #include <jpeglib.h>
39 #include <ETL/stringf>
40 #include <cstdio>
41 #include <algorithm>
42 #include <functional>
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_TARGET_INIT(jpeg_trgt);
54 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
55 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
56 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
57 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id$");
58
59 /* === M E T H O D S ======================================================= */
60
61 jpeg_trgt::jpeg_trgt(const char *Filename)
62 {
63         file=NULL;
64         filename=Filename;
65         buffer=NULL;
66         ready=false;
67         quality=95;
68         color_buffer=0;
69         set_remove_alpha();
70 }
71
72 jpeg_trgt::~jpeg_trgt()
73 {
74         if(ready)
75         {
76                 jpeg_finish_compress(&cinfo);
77                 jpeg_destroy_compress(&cinfo);
78                 ready=false;
79         }
80         if(file)
81                 fclose(file);
82         file=NULL;
83         delete [] buffer;
84         delete [] color_buffer;
85 }
86
87 bool
88 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
89 {
90         desc=*given_desc;
91         imagecount=desc.get_frame_start();
92         if(desc.get_frame_end()-desc.get_frame_start()>0)
93                 multi_image=true;
94         else
95                 multi_image=false;
96         return true;
97 }
98
99 bool
100 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
101 {
102         int w=desc.get_w(),h=desc.get_h();
103
104         if(file && file!=stdout)
105                 fclose(file);
106         if(filename=="-")
107         {
108                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
109                 file=stdout;
110         }
111         else if(multi_image)
112         {
113                 String newfilename(filename_sans_extension(filename) +
114                                                    etl::strprintf(".%04d",imagecount) +
115                                                    filename_extension(filename));
116                 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
117                 if(callback)callback->task(newfilename);
118         }
119         else
120         {
121                 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
122                 if(callback)callback->task(filename);
123         }
124
125         if(!file)
126                 return false;
127
128         delete [] buffer;
129         buffer=new unsigned char[3*w];
130
131         delete [] color_buffer;
132         color_buffer=new Color[w];
133
134
135         cinfo.err = jpeg_std_error(&jerr);
136         jpeg_create_compress(&cinfo);
137         jpeg_stdio_dest(&cinfo, file);
138
139         cinfo.image_width = w;  /* image width and height, in pixels */
140         cinfo.image_height = h;
141         cinfo.input_components = 3;             /* # of color components per pixel */
142         cinfo.in_color_space = JCS_RGB;         /* colorspace of input image */
143         /* Now use the library's routine to set default compression parameters.
144         * (You must set at least cinfo.in_color_space before calling this,
145         * since the defaults depend on the source color space.)
146         */
147         jpeg_set_defaults(&cinfo);
148         /* Now you can set any non-default parameters you wish to.
149         * Here we just illustrate the use of quality (quantization table) scaling:
150         */
151         jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
152
153         /* Step 4: Start compressor */
154
155         /* TRUE ensures that we will write a complete interchange-JPEG file.
156         * Pass TRUE unless you are very sure of what you're doing.
157         */
158         jpeg_start_compress(&cinfo, TRUE);
159
160         ready=true;
161         return true;
162 }
163
164 void
165 jpeg_trgt::end_frame()
166 {
167         if(ready)
168         {
169                 jpeg_finish_compress(&cinfo);
170                 jpeg_destroy_compress(&cinfo);
171                 ready=false;
172         }
173
174         if(file && file!=stdout)
175                 fclose(file);
176         file=NULL;
177         imagecount++;
178 }
179
180 Color *
181 jpeg_trgt::start_scanline(int /*scanline*/)
182 {
183         return color_buffer;
184 }
185
186 bool
187 jpeg_trgt::end_scanline()
188 {
189         if(!file || !ready)
190                 return false;
191
192         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
193         JSAMPROW *row_pointer(&buffer);
194         jpeg_write_scanlines(&cinfo, row_pointer, 1);
195
196         return true;
197 }