Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / 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 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "trgt_jpeg.h"
36 #include <jpeglib.h>
37 #include <ETL/stringf>
38 #include <cstdio>
39 #include <algorithm>
40 #include <functional>
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_TARGET_INIT(jpeg_trgt);
52 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
53 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
54 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id$");
56
57 /* === M E T H O D S ======================================================= */
58
59 jpeg_trgt::jpeg_trgt(const char *Filename,
60                                          const synfig::TargetParam& /* params */)
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 newfilename(filename_sans_extension(filename) +
113                                                    etl::strprintf(".%04d",imagecount) +
114                                                    filename_extension(filename));
115                 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
116                 if(callback)callback->task(newfilename);
117         }
118         else
119         {
120                 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
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 }