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