xref: /haiku/src/add-ons/translators/pcx/PCXTranslator.cpp (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 /*
2  * Copyright 2008, Jérôme Duval, korli@users.berlios.de. All rights reserved.
3  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "PCXTranslator.h"
9 #include "ConfigView.h"
10 #include "PCX.h"
11 
12 #include <Catalog.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 #undef B_TRANSLATE_CONTEXT
18 #define B_TRANSLATE_CONTEXT "PCXTranslator"
19 
20 #define kPCXMimeType "image/x-pcx"
21 
22 
23 // The input formats that this translator supports.
24 static const translation_format sInputFormats[] = {
25 	{
26 		PCX_IMAGE_FORMAT,
27 		B_TRANSLATOR_BITMAP,
28 		PCX_IN_QUALITY,
29 		PCX_IN_CAPABILITY,
30 		kPCXMimeType,
31 		"PCX image"
32 	}/*,
33 	{
34 		B_TRANSLATOR_BITMAP,
35 		B_TRANSLATOR_BITMAP,
36 		BITS_IN_QUALITY,
37 		BITS_IN_CAPABILITY,
38 		"image/x-be-bitmap",
39 		"Be Bitmap Format (PCXTranslator)"
40 	},*/
41 };
42 
43 // The output formats that this translator supports.
44 static const translation_format sOutputFormats[] = {
45 	/*{
46 		PCX_IMAGE_FORMAT,
47 		B_TRANSLATOR_BITMAP,
48 		PCX_OUT_QUALITY,
49 		PCX_OUT_CAPABILITY,
50 		kPCXMimeType,
51 		"PCX image"
52 	},*/
53 	{
54 		B_TRANSLATOR_BITMAP,
55 		B_TRANSLATOR_BITMAP,
56 		BITS_OUT_QUALITY,
57 		BITS_OUT_CAPABILITY,
58 		"image/x-be-bitmap",
59 		"Be Bitmap Format (PCXTranslator)"
60 	},
61 };
62 
63 // Default settings for the Translator
64 static const TranSetting sDefaultSettings[] = {
65 	{B_TRANSLATOR_EXT_HEADER_ONLY, TRAN_SETTING_BOOL, false},
66 	{B_TRANSLATOR_EXT_DATA_ONLY, TRAN_SETTING_BOOL, false}
67 };
68 
69 const uint32 kNumInputFormats = sizeof(sInputFormats) / sizeof(translation_format);
70 const uint32 kNumOutputFormats = sizeof(sOutputFormats) / sizeof(translation_format);
71 const uint32 kNumDefaultSettings = sizeof(sDefaultSettings) / sizeof(TranSetting);
72 
73 
74 PCXTranslator::PCXTranslator()
75 	: BaseTranslator(B_TRANSLATE("PCX images"), B_TRANSLATE("PCX translator"),
76 		PCX_TRANSLATOR_VERSION,
77 		sInputFormats, kNumInputFormats,
78 		sOutputFormats, kNumOutputFormats,
79 		"PCXTranslator_Settings",
80 		sDefaultSettings, kNumDefaultSettings,
81 		B_TRANSLATOR_BITMAP, PCX_IMAGE_FORMAT)
82 {
83 }
84 
85 
86 PCXTranslator::~PCXTranslator()
87 {
88 }
89 
90 
91 status_t
92 PCXTranslator::DerivedIdentify(BPositionIO *stream,
93 	const translation_format *format, BMessage *ioExtension,
94 	translator_info *info, uint32 outType)
95 {
96 	if (!outType)
97 		outType = B_TRANSLATOR_BITMAP;
98 	if (outType != B_TRANSLATOR_BITMAP && outType != PCX_IMAGE_FORMAT)
99 		return B_NO_TRANSLATOR;
100 
101 	int32 bitsPerPixel;
102 	uint8 type;
103 	if (PCX::identify(ioExtension, *stream, type, bitsPerPixel) != B_OK)
104 		return B_NO_TRANSLATOR;
105 
106 	info->type = PCX_IMAGE_FORMAT;
107 	info->group = B_TRANSLATOR_BITMAP;
108 	info->quality = PCX_IN_QUALITY;
109 	info->capability = PCX_IN_CAPABILITY;
110 	snprintf(info->name, sizeof(info->name), B_TRANSLATE("PCX %lu bit image"),
111 		bitsPerPixel);
112 	strcpy(info->MIME, kPCXMimeType);
113 
114 	return B_OK;
115 }
116 
117 
118 status_t
119 PCXTranslator::DerivedTranslate(BPositionIO *source,
120 	const translator_info *info, BMessage *ioExtension,
121 	uint32 outType, BPositionIO *target, int32 baseType)
122 {
123 	/*if (!outType)
124 		outType = B_TRANSLATOR_BITMAP;
125 	if (outType != B_TRANSLATOR_BITMAP && outType != PCX_IMAGE_FORMAT)
126 		return B_NO_TRANSLATOR;*/
127 
128 	switch (baseType) {
129 		/*case 1:
130 		{
131 			if (outType != PCX_IMAGE_FORMAT)
132 				return B_NO_TRANSLATOR;
133 
134 			// Source is in bits format - this has to be done here, because
135 			// identify_bits_header() is a member of the BaseTranslator class...
136 			TranslatorBitmap bitsHeader;
137 			status_t status = identify_bits_header(source, NULL, &bitsHeader);
138 			if (status != B_OK)
139 				return status;
140 
141 			return PCX::convert_bits_to_pcx(ioExtension, *source, bitsHeader, *target);
142 		}*/
143 
144 		case 0:
145 		{
146 			// source is NOT in bits format
147 			if (outType != B_TRANSLATOR_BITMAP)
148 				return B_NO_TRANSLATOR;
149 
150 			return PCX::convert_pcx_to_bits(ioExtension, *source, *target);
151 		}
152 
153 		default:
154 			return B_NO_TRANSLATOR;
155 	}
156 }
157 
158 
159 BView *
160 PCXTranslator::NewConfigView(TranslatorSettings *settings)
161 {
162 	return new ConfigView(BRect(0, 0, 225, 175));
163 }
164 
165 
166 //	#pragma mark -
167 
168 
169 BTranslator *
170 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
171 {
172 	if (n != 0)
173 		return NULL;
174 
175 	return new PCXTranslator();
176 }
177 
178