xref: /haiku/src/add-ons/translators/jpeg/JPEGTranslator.h (revision 8a990d5228b2d1099e3062180532ba709dfeef6d)
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 <Catalog.h>
38 #include <CheckBox.h>
39 #include <FindDirectory.h>
40 #include <Path.h>
41 #include <Slider.h>
42 #include <StringView.h>
43 #include <TabView.h>
44 #include <TranslationKit.h>
45 #include <TranslatorAddOn.h>
46 
47 #include <setjmp.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include <jpeglib.h>
53 
54 #include "BaseTranslator.h"
55 
56 #undef B_TRANSLATE_CONTEXT
57 #define B_TRANSLATE_CONTEXT "JPEGTranslator"
58 
59 // Settings
60 #define SETTINGS_FILE	"JPEGTranslator"
61 
62 // View messages
63 #define VIEW_MSG_SET_QUALITY 'JSCQ'
64 #define VIEW_MSG_SET_SMOOTHING 'JSCS'
65 #define VIEW_MSG_SET_PROGRESSIVE 'JSCP'
66 #define VIEW_MSG_SET_OPTIMIZECOLORS 'JSBQ'
67 #define	VIEW_MSG_SET_SMALLERFILE 'JSSF'
68 #define	VIEW_MSG_SET_GRAY1ASRGB24 'JSGR'
69 #define	VIEW_MSG_SET_ALWAYSRGB32 'JSAC'
70 #define	VIEW_MSG_SET_PHOTOSHOPCMYK 'JSPC'
71 #define	VIEW_MSG_SET_SHOWREADERRORBOX 'JSEB'
72 
73 // View labels
74 #define VIEW_LABEL_QUALITY B_TRANSLATE_MARK("Output quality")
75 #define VIEW_LABEL_SMOOTHING B_TRANSLATE_MARK("Output smoothing strength")
76 #define VIEW_LABEL_PROGRESSIVE B_TRANSLATE_MARK("Use progressive compression")
77 #define VIEW_LABEL_OPTIMIZECOLORS B_TRANSLATE_MARK("Prevent colors 'washing out'")
78 #define VIEW_LABEL_SMALLERFILE B_TRANSLATE_MARK("Make file smaller (sligthtly worse quality)")
79 #define VIEW_LABEL_GRAY1ASRGB24 B_TRANSLATE_MARK("Write black-and-white images as RGB24")
80 #define VIEW_LABEL_ALWAYSRGB32 B_TRANSLATE_MARK("Read greyscale images as RGB32")
81 #define VIEW_LABEL_PHOTOSHOPCMYK B_TRANSLATE_MARK("Use CMYK code with 0 for 100% ink coverage")
82 #define VIEW_LABEL_SHOWREADERRORBOX B_TRANSLATE_MARK("Show warning messages")
83 
84 // strings for use in TranslatorSettings
85 #define JPEG_SET_SMOOTHING "smoothing"
86 #define JPEG_SET_QUALITY "quality"
87 #define JPEG_SET_PROGRESSIVE "progressive"
88 #define JPEG_SET_OPT_COLORS "optimize"
89 #define JPEG_SET_SMALL_FILES "filesSmaller"
90 #define JPEG_SET_GRAY1_AS_RGB24 "gray"
91 #define JPEG_SET_ALWAYS_RGB32 "always"
92 #define JPEG_SET_PHOTOSHOP_CMYK "cmyk"
93 #define JPEG_SET_SHOWREADWARNING "readWarning"
94 
95 
96 
97 /*!
98 	Slider used in TranslatorView
99 	With status showing actual value
100 */
101 class SSlider : public BSlider {
102 	public:
103 		SSlider(const char* name, const char* label,
104 			BMessage* message, int32 minValue, int32 maxValue,
105 			orientation posture = B_HORIZONTAL,
106 			thumb_style thumbType = B_BLOCK_THUMB,
107 			uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
108 	virtual const char* UpdateText() const;
109 
110 	private:
111 		mutable char fStatusLabel[12];
112 };
113 
114 
115 class JPEGTranslator : public BaseTranslator {
116 	public:
117 		JPEGTranslator();
118 
119 		virtual status_t DerivedIdentify(BPositionIO* inSource,
120 			const translation_format* inFormat, BMessage* ioExtension,
121 			translator_info* outInfo, uint32 outType);
122 
123 		virtual status_t DerivedTranslate(BPositionIO* inSource,
124 			const translator_info* inInfo, BMessage* ioExtension,
125 			uint32 outType, BPositionIO* outDestination, int32 baseType);
126 
127 		virtual BView* NewConfigView(TranslatorSettings* settings);
128 
129 	private:
130 
131 		status_t Copy(BPositionIO* in, BPositionIO* out);
132 		status_t Compress(BPositionIO* in, BPositionIO* out,
133 			const jmp_buf* longJumpBuffer);
134 		status_t Decompress(BPositionIO* in, BPositionIO* out,
135 			BMessage* ioExtension, const jmp_buf* longJumpBuffer);
136 		status_t Error(j_common_ptr cinfo, status_t error = B_ERROR);
137 
138 		status_t PopulateInfoFromFormat(translator_info* info,
139 			uint32 formatType, translator_id id = 0);
140 		status_t PopulateInfoFromFormat(translator_info* info,
141 			uint32 formatType, const translation_format* formats,
142 			int32 formatCount);
143 };
144 
145 
146 class TranslatorReadView : public BView {
147 	public:
148 		TranslatorReadView(const char* name, TranslatorSettings* settings);
149 		virtual ~TranslatorReadView();
150 
151 		virtual void	AttachedToWindow();
152 		virtual void	MessageReceived(BMessage* message);
153 
154 	private:
155 		TranslatorSettings* fSettings;
156 		BCheckBox*		fAlwaysRGB32;
157 		BCheckBox*		fPhotoshopCMYK;
158 		BCheckBox*		fShowErrorBox;
159 };
160 
161 
162 class TranslatorWriteView : public BView {
163 	public:
164 		TranslatorWriteView(const char* name, TranslatorSettings* settings);
165 		virtual ~TranslatorWriteView();
166 
167 		virtual void	AttachedToWindow();
168 		virtual void	MessageReceived(BMessage* message);
169 
170 	private:
171 		TranslatorSettings* fSettings;
172 		SSlider*		fQualitySlider;
173 		SSlider*		fSmoothingSlider;
174 		BCheckBox*		fProgress;
175 		BCheckBox*		fOptimizeColors;
176 		BCheckBox*		fSmallerFile;
177 		BCheckBox*		fGrayAsRGB24;
178 };
179 
180 
181 class TranslatorAboutView : public BView {
182 	public:
183 		TranslatorAboutView(const char* name);
184 };
185 
186 
187 class TranslatorView : public BTabView {
188 	public:
189 		TranslatorView(const char* name, TranslatorSettings* settings);
190 
191 	private:
192 		BView* fAboutView;
193 		BView* fReadView;
194 		BView* fWriteView;
195 };
196 
197 
198 //---------------------------------------------------
199 //	"Initializers" for jpeglib
200 //	based on default ones,
201 //	modified to work on BPositionIO instead of FILE
202 //---------------------------------------------------
203 EXTERN(void) be_jpeg_stdio_src(j_decompress_ptr cinfo, BPositionIO *infile);	// from "be_jdatasrc.cpp"
204 EXTERN(void) be_jpeg_stdio_dest(j_compress_ptr cinfo, BPositionIO *outfile);	// from "be_jdatadst.cpp"
205 
206 //---------------------------------------------------
207 //	Error output functions
208 //	based on the one from jerror.c
209 //	modified to use settings
210 //	(so user can decide to show dialog-boxes or not)
211 //---------------------------------------------------
212 EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err,
213 	TranslatorSettings * settings, const jmp_buf* longJumpBuffer);
214 	// implemented in "be_jerror.cpp"
215 
216 #endif // _JPEGTRANSLATOR_H_
217