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