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