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