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