xref: /haiku/src/add-ons/translators/psd/PSDTranslator.cpp (revision f8da8f3477d3c18142e59d17d05a545982faa5a8)
1 /*
2  * Copyright 2013, Gerasim Troeglazov, 3dEyes@gmail.com. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include "ConfigView.h"
12 #include "PSDTranslator.h"
13 #include "PSDLoader.h"
14 #include "PSDWriter.h"
15 
16 const char *kDocumentCount = "/documentCount";
17 const char *kDocumentIndex = "/documentIndex";
18 
19 #define kPSDMimeType "image/vnd.adobe.photoshop"
20 #define kPSDName "Photoshop image"
21 
22 static const translation_format sInputFormats[] = {
23 	{
24 		B_TRANSLATOR_BITMAP,
25 		B_TRANSLATOR_BITMAP,
26 		BITS_IN_QUALITY,
27 		BITS_IN_CAPABILITY,
28 		"image/x-be-bitmap",
29 		"Be Bitmap Format (PSDTranslator)"
30 	},
31 	{
32 		PSD_IMAGE_FORMAT,
33 		B_TRANSLATOR_BITMAP,
34 		PSD_IN_QUALITY,
35 		PSD_IN_CAPABILITY,
36 		kPSDMimeType,
37 		kPSDName
38 	}
39 };
40 
41 static const translation_format sOutputFormats[] = {
42 	{
43 		B_TRANSLATOR_BITMAP,
44 		B_TRANSLATOR_BITMAP,
45 		BITS_OUT_QUALITY,
46 		BITS_OUT_CAPABILITY,
47 		"image/x-be-bitmap",
48 		"Be Bitmap Format (PSDTranslator)"
49 	},
50 	{
51 		PSD_IMAGE_FORMAT,
52 		B_TRANSLATOR_BITMAP,
53 		PSD_OUT_QUALITY,
54 		PSD_OUT_CAPABILITY,
55 		kPSDMimeType,
56 		kPSDName
57 	}
58 };
59 
60 
61 static const TranSetting sDefaultSettings[] = {
62 	{B_TRANSLATOR_EXT_HEADER_ONLY, TRAN_SETTING_BOOL, false},
63 	{B_TRANSLATOR_EXT_DATA_ONLY, TRAN_SETTING_BOOL, false},
64 	{PSD_SETTING_COMPRESSION, TRAN_SETTING_INT32, PSD_COMPRESSED_RLE},
65 	{PSD_SETTING_VERSION, TRAN_SETTING_INT32, PSD_FILE}
66 };
67 
68 const uint32 kNumInputFormats = sizeof(sInputFormats)
69 	/ sizeof(translation_format);
70 const uint32 kNumOutputFormats = sizeof(sOutputFormats)
71 	/ sizeof(translation_format);
72 const uint32 kNumDefaultSettings = sizeof(sDefaultSettings)
73 	/ sizeof(TranSetting);
74 
75 
76 PSDTranslator::PSDTranslator()
77 	: BaseTranslator(kPSDName,
78 		"Photoshop image translator",
79 		PSD_TRANSLATOR_VERSION,
80 		sInputFormats, kNumInputFormats,
81 		sOutputFormats, kNumOutputFormats,
82 		"PSDTranslator",
83 		sDefaultSettings, kNumDefaultSettings,
84 		B_TRANSLATOR_BITMAP, PSD_IMAGE_FORMAT)
85 {
86 }
87 
88 
89 PSDTranslator::~PSDTranslator()
90 {
91 }
92 
93 
94 status_t
95 PSDTranslator::DerivedIdentify(BPositionIO *stream,
96 	const translation_format *format, BMessage *ioExtension,
97 	translator_info *info, uint32 outType)
98 {
99 	if (!outType)
100 		outType = B_TRANSLATOR_BITMAP;
101 	if (outType != B_TRANSLATOR_BITMAP && outType != PSD_IMAGE_FORMAT)
102 		return B_NO_TRANSLATOR;
103 
104 	PSDLoader psdFile(stream);
105 	if (!psdFile.IsSupported())
106 		return B_ILLEGAL_DATA;
107 
108 	info->type = PSD_IMAGE_FORMAT;
109 	info->group = B_TRANSLATOR_BITMAP;
110 	info->quality = PSD_IN_QUALITY;
111 	info->capability = PSD_IN_CAPABILITY;
112 	BString name(kPSDName);
113 	name << " (" << psdFile.ColorFormatName() << ")";
114 	strcpy(info->name, name.String());
115 	strcpy(info->MIME, kPSDMimeType);
116 
117 	return B_OK;
118 }
119 
120 
121 status_t
122 PSDTranslator::DerivedTranslate(BPositionIO *source,
123 	const translator_info *info, BMessage *ioExtension,
124 	uint32 outType, BPositionIO *target, int32 baseType)
125 {
126 	if (outType != B_TRANSLATOR_BITMAP
127 		&& outType != PSD_IMAGE_FORMAT) {
128 		return B_NO_TRANSLATOR;
129 	}
130 
131 	switch (baseType) {
132 		case 0:
133 		{
134 			if (outType != B_TRANSLATOR_BITMAP)
135 				return B_NO_TRANSLATOR;
136 
137 			PSDLoader psdFile(source);
138 			if (!psdFile.IsLoaded())
139 				return B_NO_TRANSLATOR;
140 
141 			return psdFile.Decode(target);
142 		}
143 		case 1:
144 		{
145 			if (outType == PSD_IMAGE_FORMAT) {
146 				PSDWriter psdFile(source);
147 
148 				uint32 compression =
149 					fSettings->SetGetInt32(PSD_SETTING_COMPRESSION);
150 				uint32 version =
151 					fSettings->SetGetInt32(PSD_SETTING_VERSION);
152 
153 				psdFile.SetCompression(compression);
154 				psdFile.SetVersion(version);
155 
156 				if (psdFile.IsReady())
157 					return psdFile.Encode(target);
158 			}
159 			return B_NO_TRANSLATOR;
160 		}
161 		default:
162 			return B_NO_TRANSLATOR;
163 	}
164 }
165 
166 
167 status_t
168 PSDTranslator::DerivedCanHandleImageSize(float width, float height) const
169 {
170 	return B_OK;
171 }
172 
173 
174 BView *
175 PSDTranslator::NewConfigView(TranslatorSettings *settings)
176 {
177 	return new ConfigView(settings);
178 }
179 
180 
181 BTranslator *
182 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
183 {
184 	if (n != 0)
185 		return NULL;
186 
187 	return new PSDTranslator();
188 }
189 
190