Fixing warnings from doxygen:
[synfig.git] / synfig-core / trunk / 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 **      \legal
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 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #define SYNFIG_TARGET
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "trgt_jpeg.h"
35 #include <jpeglib.h>
36 #include <ETL/stringf>
37 #include <cstdio>
38 #include <algorithm>
39 #include <functional>
40 #endif
41
42 /* === M A C R O S ========================================================= */
43
44 using namespace synfig;
45 using namespace std;
46 using namespace etl;
47
48 /* === G L O B A L S ======================================================= */
49
50 SYNFIG_TARGET_INIT(jpeg_trgt);
51 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
52 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
53 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
54 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id: trgt_jpeg.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
55
56 /* === M E T H O D S ======================================================= */
57
58 jpeg_trgt::jpeg_trgt(const char *Filename)
59 {
60         file=NULL;
61         filename=Filename;
62         buffer=NULL;
63         ready=false;
64         quality=95;
65         color_buffer=0;
66         set_remove_alpha();
67 }
68
69 jpeg_trgt::~jpeg_trgt()
70 {
71         if(ready)
72         {
73                 jpeg_finish_compress(&cinfo);
74                 jpeg_destroy_compress(&cinfo);
75                 ready=false;
76         }
77         if(file)
78                 fclose(file);
79         file=NULL;
80         delete [] buffer;
81         delete [] color_buffer;
82 }
83
84 bool
85 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
86 {
87         desc=*given_desc;
88         imagecount=desc.get_frame_start();
89         if(desc.get_frame_end()-desc.get_frame_start()>0)
90                 multi_image=true;
91         else
92                 multi_image=false;
93         return true;
94 }
95
96 bool
97 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
98 {
99         int w=desc.get_w(),h=desc.get_h();
100
101         if(file && file!=stdout)
102                 fclose(file);
103         if(filename=="-")
104         {
105                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
106                 file=stdout;
107         }
108         else if(multi_image)
109         {
110                 String
111                         newfilename(filename),
112                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
113                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
114
115                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
116                 file=fopen(newfilename.c_str(),"wb");
117                 if(callback)callback->task(newfilename);
118         }
119         else
120         {
121                 file=fopen(filename.c_str(),"wb");
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 }