xref: /haiku/src/add-ons/translators/pcx/PCXTranslator.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
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_TRANSLATION_CONTEXT
18 #define B_TRANSLATION_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"),
76 		B_TRANSLATE("PCX image translator"),
77 		PCX_TRANSLATOR_VERSION,
78 		sInputFormats, kNumInputFormats,
79 		sOutputFormats, kNumOutputFormats,
80 		"PCXTranslator_Settings",
81 		sDefaultSettings, kNumDefaultSettings,
82 		B_TRANSLATOR_BITMAP, PCX_IMAGE_FORMAT)
83 {
84 }
85 
86 
87 PCXTranslator::~PCXTranslator()
88 {
89 }
90 
91 
92 status_t
93 PCXTranslator::DerivedIdentify(BPositionIO *stream,
94 	const translation_format *format, BMessage *ioExtension,
95 	translator_info *info, uint32 outType)
96 {
97 	if (!outType)
98 		outType = B_TRANSLATOR_BITMAP;
99 	if (outType != B_TRANSLATOR_BITMAP && outType != PCX_IMAGE_FORMAT)
100 		return B_NO_TRANSLATOR;
101 
102 	int32 bitsPerPixel;
103 	uint8 type;
104 	if (PCX::identify(ioExtension, *stream, type, bitsPerPixel) != B_OK)
105 		return B_NO_TRANSLATOR;
106 
107 	info->type = PCX_IMAGE_FORMAT;
108 	info->group = B_TRANSLATOR_BITMAP;
109 	info->quality = PCX_IN_QUALITY;
110 	info->capability = PCX_IN_CAPABILITY;
111 	snprintf(info->name, sizeof(info->name), B_TRANSLATE("PCX %lu bit image"),
112 		bitsPerPixel);
113 	strcpy(info->MIME, kPCXMimeType);
114 
115 	return B_OK;
116 }
117 
118 
119 status_t
120 PCXTranslator::DerivedTranslate(BPositionIO *source,
121 	const translator_info *info, BMessage *ioExtension,
122 	uint32 outType, BPositionIO *target, int32 baseType)
123 {
124 	/*if (!outType)
125 		outType = B_TRANSLATOR_BITMAP;
126 	if (outType != B_TRANSLATOR_BITMAP && outType != PCX_IMAGE_FORMAT)
127 		return B_NO_TRANSLATOR;*/
128 
129 	switch (baseType) {
130 		/*case 1:
131 		{
132 			if (outType != PCX_IMAGE_FORMAT)
133 				return B_NO_TRANSLATOR;
134 
135 			// Source is in bits format - this has to be done here, because
136 			// identify_bits_header() is a member of the BaseTranslator class...
137 			TranslatorBitmap bitsHeader;
138 			status_t status = identify_bits_header(source, NULL, &bitsHeader);
139 			if (status != B_OK)
140 				return status;
141 
142 			return PCX::convert_bits_to_pcx(ioExtension, *source, bitsHeader, *target);
143 		}*/
144 
145 		case 0:
146 		{
147 			// source is NOT in bits format
148 			if (outType != B_TRANSLATOR_BITMAP)
149 				return B_NO_TRANSLATOR;
150 
151 			return PCX::convert_pcx_to_bits(ioExtension, *source, *target);
152 		}
153 
154 		default:
155 			return B_NO_TRANSLATOR;
156 	}
157 }
158 
159 
160 BView *
161 PCXTranslator::NewConfigView(TranslatorSettings *settings)
162 {
163 	return new ConfigView(BRect(0, 0, 225, 175));
164 }
165 
166 
167 //	#pragma mark -
168 
169 
170 BTranslator *
171 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
172 {
173 	if (n != 0)
174 		return NULL;
175 
176 	return new PCXTranslator();
177 }
178 
179