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