xref: /haiku/src/add-ons/translators/jpeg/JPEGTranslator.h (revision 020cbad9d40235a2c50a81a42d69912a5ff8fbc4)
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 #ifndef _JPEGTRANSLATOR_H_
32 #define _JPEGTRANSLATOR_H_
33 
34 
35 #include <Alert.h>
36 #include <Application.h>
37 #include <CheckBox.h>
38 #include <FindDirectory.h>
39 #include <Path.h>
40 #include <Slider.h>
41 #include <StringView.h>
42 #include <TranslationKit.h>
43 #include <TranslatorAddOn.h>
44 
45 #include <setjmp.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include <jpeglib.h>
51 
52 
53 // Settings
54 #define SETTINGS_FILE	"JPEGTranslator"
55 
56 // View messages
57 #define VIEW_MSG_SET_QUALITY 'JSCQ'
58 #define VIEW_MSG_SET_SMOOTHING 'JSCS'
59 #define VIEW_MSG_SET_PROGRESSIVE 'JSCP'
60 #define VIEW_MSG_SET_OPTIMIZECOLORS 'JSBQ'
61 #define	VIEW_MSG_SET_SMALLERFILE 'JSSF'
62 #define	VIEW_MSG_SET_GRAY1ASRGB24 'JSGR'
63 #define	VIEW_MSG_SET_ALWAYSRGB32 'JSAC'
64 #define	VIEW_MSG_SET_PHOTOSHOPCMYK 'JSPC'
65 #define	VIEW_MSG_SET_SHOWREADERRORBOX 'JSEB'
66 
67 // View labels
68 #define VIEW_LABEL_QUALITY "Output quality"
69 #define VIEW_LABEL_SMOOTHING "Output smoothing strength"
70 #define VIEW_LABEL_PROGRESSIVE "Use progressive compression"
71 #define VIEW_LABEL_OPTIMIZECOLORS "Prevent colors 'washing out'"
72 #define	VIEW_LABEL_SMALLERFILE "Make file smaller (sligthtly worse quality)"
73 #define	VIEW_LABEL_GRAY1ASRGB24 "Write Black&White images as RGB24"
74 #define	VIEW_LABEL_ALWAYSRGB32 "Read Greyscale images as RGB32"
75 #define	VIEW_LABEL_PHOTOSHOPCMYK "Use CMYK code with 0 for 100% ink coverage"
76 #define	VIEW_LABEL_SHOWREADERRORBOX "Show warning messages"
77 
78 
79 //!	Settings storage structure
80 struct jpeg_settings {
81 	// compression
82 	uchar	Smoothing;			// default: 0
83 	uchar	Quality;			// default: 95
84 	bool	Progressive;		// default: true
85 	bool	OptimizeColors;		// default: true
86 	bool	SmallerFile;		// default: false	only used if (OptimizeColors == true)
87 	bool	B_GRAY1_as_B_RGB24;	// default: false	if false gray1 converted to gray8, else to rgb24
88 	// decompression
89 	bool	Always_B_RGB32;		// default: true
90 	bool	PhotoshopCMYK;		// default: true
91 	bool	ShowReadWarningBox;	// default: true
92 };
93 
94 
95 /*!
96 	Slider used in TranslatorView
97 	With status showing actual value
98 */
99 class SSlider : public BSlider {
100 	public:
101 				SSlider(BRect frame, const char *name, const char *label,
102 					BMessage *message, int32 minValue, int32 maxValue,
103 					orientation posture = B_HORIZONTAL,
104 					thumb_style thumbType = B_BLOCK_THUMB,
105 					uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
106 					uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
107 		char*	UpdateText() const;
108 		void	ResizeToPreferred();
109 
110 	private:
111 		mutable char fStatusLabel[12];
112 };
113 
114 //!	Basic view class with resizing to needed size
115 class SView : public BView {
116 	public:
117 		SView(const char* name, float x = 0, float y = 0);
118 
119 		virtual void	GetPreferredSize(float* _width, float* _height);
120 		virtual void	ResizeToPreferred();
121 
122 		void			AddChild(BView* child, BView* before = NULL);
123 
124 		float			GetPreferredWidth()
125 							{ return fPreferredWidth; }
126 		float			GetPreferredHeight()
127 							{ return fPreferredHeight; }
128 		void			ResizePreferredBy(float width, float height);
129 
130 	private:
131 		float			fPreferredWidth;
132 		float			fPreferredHeight;
133 };
134 
135 //!	Configuration view for reading settings
136 class TranslatorReadView : public SView {
137 	public:
138 		TranslatorReadView(const char* name, jpeg_settings* settings,
139 			float x = 0, float y = 0);
140 
141 		virtual void	AttachedToWindow();
142 		virtual void	MessageReceived(BMessage* message);
143 
144 	private:
145 		jpeg_settings*	fSettings;
146 		BCheckBox*		fAlwaysRGB32;
147 		BCheckBox*		fPhotoshopCMYK;
148 		BCheckBox*		fShowErrorBox;
149 };
150 
151 //! Configuration view for writing settings
152 class TranslatorWriteView : public SView {
153 	public:
154 		TranslatorWriteView(const char* name, jpeg_settings* settings,
155 			float x = 0, float y = 0);
156 
157 		virtual void	AttachedToWindow();
158 		virtual void	MessageReceived(BMessage* message);
159 
160 	private:
161 		jpeg_settings*	fSettings;
162 		SSlider*		fQualitySlider;
163 		SSlider*		fSmoothingSlider;
164 		BCheckBox*		fProgress;
165 		BCheckBox*		fOptimizeColors;
166 		BCheckBox*		fSmallerFile;
167 		BCheckBox*		fGrayAsRGB24;
168 };
169 
170 class TranslatorAboutView : public SView {
171 	public:
172 		TranslatorAboutView(const char* name, float x = 0, float y = 0);
173 };
174 
175 //!	Configuration view
176 class TranslatorView : public SView {
177 	public:
178 		TranslatorView(const char *name);
179 		virtual ~TranslatorView();
180 
181 		virtual void	AttachedToWindow();
182 		virtual void	Draw(BRect updateRect);
183 		virtual void	MouseDown(BPoint where);
184 
185 	private:
186 		BRect			_TabFrame(int32 index) const;
187 
188 		jpeg_settings	fSettings;
189 		BList			fTabs;
190 		int32			fTabWidth;
191 		int32			fTabHeight;
192 		int32			fActiveChild;
193 };
194 
195 //!	Window used for configuration
196 class TranslatorWindow : public BWindow {
197 	public:
198 		TranslatorWindow(bool quitOnClose = true);
199 };
200 
201 
202 //---------------------------------------------------
203 //	"Initializers" for jpeglib
204 //	based on default ones,
205 //	modified to work on BPositionIO instead of FILE
206 //---------------------------------------------------
207 EXTERN(void) be_jpeg_stdio_src(j_decompress_ptr cinfo, BPositionIO *infile);	// from "be_jdatasrc.cpp"
208 EXTERN(void) be_jpeg_stdio_dest(j_compress_ptr cinfo, BPositionIO *outfile);	// from "be_jdatadst.cpp"
209 
210 //---------------------------------------------------
211 //	Error output functions
212 //	based on the one from jerror.c
213 //	modified to use settings
214 //	(so user can decide to show dialog-boxes or not)
215 //---------------------------------------------------
216 EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err,
217 	jpeg_settings * settings, const jmp_buf* longJumpBuffer);
218 	// implemented in "be_jerror.cpp"
219 
220 #endif // _JPEGTRANSLATOR_H_
221