Merge branch 'nikitakit_master'
[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 #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                                          const synfig::TargetParam& /* params */)
63 {
64         file=NULL;
65         filename=Filename;
66         buffer=NULL;
67         ready=false;
68         quality=95;
69         color_buffer=0;
70         set_remove_alpha();
71 }
72
73 jpeg_trgt::~jpeg_trgt()
74 {
75         if(ready)
76         {
77                 jpeg_finish_compress(&cinfo);
78                 jpeg_destroy_compress(&cinfo);
79                 ready=false;
80         }
81         if(file)
82                 fclose(file);
83         file=NULL;
84         delete [] buffer;
85         delete [] color_buffer;
86 }
87
88 bool
89 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
90 {
91         desc=*given_desc;
92         imagecount=desc.get_frame_start();
93         if(desc.get_frame_end()-desc.get_frame_start()>0)
94                 multi_image=true;
95         else
96                 multi_image=false;
97         return true;
98 }
99
100 bool
101 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
102 {
103         int w=desc.get_w(),h=desc.get_h();
104
105         if(file && file!=stdout)
106                 fclose(file);
107         if(filename=="-")
108         {
109                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
110                 file=stdout;
111         }
112         else if(multi_image)
113         {
114                 String newfilename(filename_sans_extension(filename) +
115                                                    etl::strprintf(".%04d",imagecount) +
116                                                    filename_extension(filename));
117                 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
118                 if(callback)callback->task(newfilename);
119         }
120         else
121         {
122                 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
123                 if(callback)callback->task(filename);
124         }
125
126         if(!file)
127                 return false;
128
129         delete [] buffer;
130         buffer=new unsigned char[3*w];
131
132         delete [] color_buffer;
133         color_buffer=new Color[w];
134
135
136         cinfo.err = jpeg_std_error(&jerr);
137         jpeg_create_compress(&cinfo);
138         jpeg_stdio_dest(&cinfo, file);
139
140         cinfo.image_width = w;  /* image width and height, in pixels */
141         cinfo.image_height = h;
142         cinfo.input_components = 3;             /* # of color components per pixel */
143         cinfo.in_color_space = JCS_RGB;         /* colorspace of input image */
144         /* Now use the library's routine to set default compression parameters.
145         * (You must set at least cinfo.in_color_space before calling this,
146         * since the defaults depend on the source color space.)
147         */
148         jpeg_set_defaults(&cinfo);
149         /* Now you can set any non-default parameters you wish to.
150         * Here we just illustrate the use of quality (quantization table) scaling:
151         */
152         jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
153
154         /* Step 4: Start compressor */
155
156         /* TRUE ensures that we will write a complete interchange-JPEG file.
157         * Pass TRUE unless you are very sure of what you're doing.
158         */
159         jpeg_start_compress(&cinfo, TRUE);
160
161         ready=true;
162         return true;
163 }
164
165 void
166 jpeg_trgt::end_frame()
167 {
168         if(ready)
169         {
170                 jpeg_finish_compress(&cinfo);
171                 jpeg_destroy_compress(&cinfo);
172                 ready=false;
173         }
174
175         if(file && file!=stdout)
176                 fclose(file);
177         file=NULL;
178         imagecount++;
179 }
180
181 Color *
182 jpeg_trgt::start_scanline(int /*scanline*/)
183 {
184         return color_buffer;
185 }
186
187 bool
188 jpeg_trgt::end_scanline()
189 {
190         if(!file || !ready)
191                 return false;
192
193         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
194         JSAMPROW *row_pointer(&buffer);
195         jpeg_write_scanlines(&cinfo, row_pointer, 1);
196
197         return true;
198 }