xref: /haiku/src/add-ons/translators/jpeg/JPEGTranslator.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2 
3 Copyright (c) 2002-2003, Marcin 'Shard' Konicki
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation and/or
13       other materials provided with the distribution.
14     * Name "Marcin Konicki", "Shard" or any combination of them,
15       must not be used to endorse or promote products derived from this
16       software without specific prior written permission from Marcin Konicki.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 */
31 
32 #ifndef _JPEGTRANSLATOR_H_
33 #define _JPEGTRANSLATOR_H_
34 
35 
36 //----------------------------------------------------------------------------
37 //
38 //	Include
39 //
40 //----------------------------------------------------------------------------
41 
42 #include <Alert.h>
43 #include <Application.h>
44 #include <CheckBox.h>
45 #include <FindDirectory.h>
46 #include <Path.h>
47 #include <Slider.h>
48 #include <StringView.h>
49 #include <TranslationKit.h>
50 #include <TranslatorAddOn.h>
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 #include <jpeglib.h>
57 
58 
59 // Settings
60 #define SETTINGS_FILE	"OpenJPEGTranslator"
61 #define SETTINGS_PATH	"/boot/home/config/settings"
62 
63 // View messages
64 #define VIEW_MSG_SET_QUALITY 'JSCQ'
65 #define VIEW_MSG_SET_SMOOTHING 'JSCS'
66 #define VIEW_MSG_SET_PROGRESSIVE 'JSCP'
67 #define VIEW_MSG_SET_OPTIMIZECOLORS 'JSBQ'
68 #define	VIEW_MSG_SET_SMALLERFILE 'JSSF'
69 #define	VIEW_MSG_SET_GRAY1ASRGB24 'JSGR'
70 #define	VIEW_MSG_SET_ALWAYSRGB32 'JSAC'
71 #define	VIEW_MSG_SET_PHOTOSHOPCMYK 'JSPC'
72 #define	VIEW_MSG_SET_SHOWREADERRORBOX 'JSEB'
73 
74 // View labels
75 #define VIEW_LABEL_QUALITY "Output quality"
76 #define VIEW_LABEL_SMOOTHING "Output smoothing strength"
77 #define VIEW_LABEL_PROGRESSIVE "Use progressive compression"
78 #define VIEW_LABEL_OPTIMIZECOLORS "Prevent colors 'washing out'"
79 #define	VIEW_LABEL_SMALLERFILE "Make file smaller (sligthtly worse quality)"
80 #define	VIEW_LABEL_GRAY1ASRGB24 "Write Black&White images as RGB24"
81 #define	VIEW_LABEL_ALWAYSRGB32 "Read Greyscale images as RGB32"
82 #define	VIEW_LABEL_PHOTOSHOPCMYK "Use CMYK code with 0 for 100% ink coverage"
83 #define	VIEW_LABEL_SHOWREADERRORBOX "Show warning messages"
84 
85 
86 //---------------------------------------------------
87 //	Settings storage structure
88 //---------------------------------------------------
89 struct jpeg_settings {
90 	// compression
91 	uchar	Smoothing;			// default: 0
92 	uchar	Quality;			// default: 95
93 	bool	Progressive;		// default: true
94 	bool	OptimizeColors;		// default: true
95 	bool	SmallerFile;		// default: false	only used if (OptimizeColors == true)
96 	bool	B_GRAY1_as_B_RGB24;	// default: false	if false gray1 converted to gray8, else to rgb24
97 	// decompression
98 	bool	Always_B_RGB32;		// default: true
99 	bool	PhotoshopCMYK;		// default: true
100 	bool	ShowReadWarningBox;	// default: true
101 };
102 
103 //---------------------------------------------------
104 //	Slider used in TranslatorView
105 //	With status showing actual value
106 //---------------------------------------------------
107 class SSlider : public BSlider {
108 	public:
109 							SSlider(BRect frame, const char *name, const char *label, BMessage *message, int32 minValue, int32 maxValue, orientation posture = B_HORIZONTAL, thumb_style thumbType = B_BLOCK_THUMB, uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
110 		char*				UpdateText() const;
111 		void				ResizeToPreferred();
112 
113 	private:
114 		char				statusLabel[12];
115 };
116 
117 
118 //---------------------------------------------------
119 //	Basic view class with resizing to needed size
120 //---------------------------------------------------
121 class SView : public BView {
122 	public:
123 							SView(const char *name, float x = 0, float y = 0)
124 								:BView( BRect(x,y,x,y), name, B_FOLLOW_NONE, B_WILL_DRAW)
125 								{
126 									preferredWidth = 0;
127 									preferredHeight = 0;
128 									SetViewColor( ui_color(B_PANEL_BACKGROUND_COLOR));
129 									SetFont(be_plain_font);
130 								};
131 		void				GetPreferredSize(float *width, float *height)
132 								{
133 									*width = preferredWidth;
134 									*height = preferredHeight;
135 								}
136 		inline float		GetPreferredWidth() { return preferredWidth; };
137 		inline float		GetPreferredHeight() { return preferredHeight; };
138 		inline void			ResizePreferredBy(float width, float height) { preferredWidth += width; preferredHeight += height; };
139 		inline void			ResizeToPreferred() { ResizeTo(preferredWidth, preferredHeight); };
140 		void				AddChild(BView *child, BView *before = NULL)
141 								{
142 									BView::AddChild(child, before);
143 									child->ResizeToPreferred();
144 									BRect frame = child->Frame();
145 									if (frame.right > preferredWidth)
146 										preferredWidth = frame.right;
147 									if (frame.bottom > preferredHeight)
148 										preferredHeight = frame.bottom;
149 								}
150 
151 	private:
152 		float				preferredWidth;
153 		float				preferredHeight;
154 };
155 
156 //---------------------------------------------------
157 //	Configuration view for reading settings
158 //---------------------------------------------------
159 class TranslatorReadView : public SView {
160 	public:
161 							TranslatorReadView(const char *name, jpeg_settings *settings, float x = 0, float y = 0);
162 		void				AttachedToWindow();
163 		void				MessageReceived(BMessage *message);
164 
165 	private:
166 		jpeg_settings		*fSettings;
167 		BCheckBox			*alwaysrgb32;
168 		BCheckBox			*photoshopCMYK;
169 		BCheckBox			*showerrorbox;
170 };
171 
172 //---------------------------------------------------
173 //	Configuration view for writing settings
174 //---------------------------------------------------
175 class TranslatorWriteView : public SView {
176 	public:
177 							TranslatorWriteView(const char *name, jpeg_settings *settings, float x = 0, float y = 0);
178 		void				AttachedToWindow();
179 		void				MessageReceived(BMessage *message);
180 
181 	private:
182 		jpeg_settings			*fSettings;
183 		SSlider				*quality;
184 		SSlider				*smoothing;
185 		BCheckBox			*progress;
186 		BCheckBox			*optimizecolors;
187 		BCheckBox			*smallerfile;
188 		BCheckBox			*gray1asrgb24;
189 };
190 
191 //---------------------------------------------------
192 //	About view
193 //---------------------------------------------------
194 class TranslatorAboutView : public SView {
195 	public:
196 							TranslatorAboutView(const char *name, float x = 0, float y = 0);
197 };
198 
199 //---------------------------------------------------
200 //	Configuration view
201 //---------------------------------------------------
202 class TranslatorView : public SView {
203 	public:
204 							TranslatorView(const char *name);
205 							~TranslatorView();
206 
207 		void				AttachedToWindow();
208 		void				Draw(BRect updateRect);
209 		void				MouseDown(BPoint where);
210 
211 	private:
212 		jpeg_settings		fSettings;
213 		int32				fTabWidth;
214 		int32				fTabHeight;
215 		int32				fActiveChild;
216 };
217 
218 //---------------------------------------------------
219 //	Window used for configuration
220 //---------------------------------------------------
221 class TranslatorWindow : public BWindow {
222 	public:
223 		TranslatorWindow(bool quitOnClose = true);
224 };
225 
226 
227 //---------------------------------------------------
228 //	"Initializers" for jpeglib
229 //	based on default ones,
230 //	modified to work on BPositionIO instead of FILE
231 //---------------------------------------------------
232 EXTERN(void) be_jpeg_stdio_src(j_decompress_ptr cinfo, BPositionIO *infile);	// from "be_jdatasrc.cpp"
233 EXTERN(void) be_jpeg_stdio_dest(j_compress_ptr cinfo, BPositionIO *outfile);	// from "be_jdatadst.cpp"
234 
235 //---------------------------------------------------
236 //	Error output functions
237 //	based on the one from jerror.c
238 //	modified to use settings
239 //	(so user can decide to show dialog-boxes or not)
240 //---------------------------------------------------
241 EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err, jpeg_settings * settings); // from "be_jerror.cpp"
242 
243 //---------------------------------------------------
244 //	Main functions of translator :)
245 //---------------------------------------------------
246 status_t Copy(BPositionIO *in, BPositionIO *out);
247 status_t Compress(BPositionIO *in, BPositionIO *out);
248 status_t Decompress(BPositionIO *in, BPositionIO *out);
249 status_t Error(j_common_ptr cinfo, status_t error = B_ERROR);
250 
251 #endif // _JPEGTRANSLATOR_H_
252