xref: /haiku/src/tools/translation/bitsinfo/bitsinfo.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 /*****************************************************************************/
2 // bitsinfo
3 // Written by Michael Wilber, OBOS Translation Kit Team
4 //
5 // Version:
6 //
7 // bitsinfo is a command line program for displaying text information about
8 // Be bitmap format ("bits") images.  The Be bitmap format is used by
9 // the Translation Kit as the intermediate format for converting images.
10 // To make "bits" images, you can use the "translate" command line program or
11 // the BBitmapTranslator (available: http://www.bebits.com/app/647).
12 //
13 // This application and all source files used in its construction, except
14 // where noted, are licensed under the MIT License, and have been written
15 // and are:
16 //
17 // Copyright (c) 2003 OpenBeOS Project
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining a
20 // copy of this software and associated documentation files (the "Software"),
21 // to deal in the Software without restriction, including without limitation
22 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 // and/or sell copies of the Software, and to permit persons to whom the
24 // Software is furnished to do so, subject to the following conditions:
25 //
26 // The above copyright notice and this permission notice shall be included
27 // in all copies or substantial portions of the Software.
28 //
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35 // DEALINGS IN THE SOFTWARE.
36 /*****************************************************************************/
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ByteOrder.h>
41 #include <File.h>
42 #include <TranslatorFormats.h>
43 
44 struct ColorSpaceName {
45 	color_space id;
46 	const char *name;
47 };
48 #define COLORSPACENAME(id) {id, #id}
49 
50 const char *kpixels = "-pixels";
51 
52 void
53 PrintBitsInfo(const char *filepath, bool bdumppixels)
54 {
55 	BFile file(filepath, B_READ_ONLY);
56 
57 	if (file.InitCheck() == B_OK) {
58 		TranslatorBitmap header;
59 		memset(&header, 0, sizeof(TranslatorBitmap));
60 
61 		// read in the rest of the header
62 		ssize_t size = sizeof(TranslatorBitmap);
63 		if (file.Read(reinterpret_cast<uint8 *> (&header), size) != size) {
64 			printf("\nError: Unable to read the Be bitmap header.\n");
65 			return;
66 		}
67 		if (!bdumppixels)
68 			// I don't need the file anymore
69 			file.Unset();
70 
71 		// convert to host byte order
72 		if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
73 			B_SWAP_BENDIAN_TO_HOST) != B_OK) {
74 			printf("\nError: Unable to swap byte order\n");
75 			return;
76 		}
77 
78 		printf("\nBe bitmap (\"bits\") header for: %s\n\n", filepath);
79 
80 		const uint32 kbitsmagic = 0x62697473UL;
81 			// in ASCII, this number looks like "bits"
82 		printf("magic number: 0x%.8lx ", header.magic);
83 		if (header.magic == kbitsmagic)
84 			printf("(valid)\n");
85 		else
86 			printf("(INVALID, should be: 0x%.8lx)\n",
87 				kbitsmagic);
88 		printf("bounds: (%f, %f, %f, %f)\n",
89 			header.bounds.left, header.bounds.top,
90 			header.bounds.right, header.bounds.bottom);
91 		printf("dimensions: %d x %d\n",
92 			static_cast<int>(header.bounds.Width() + 1),
93 			static_cast<int>(header.bounds.Height() + 1));
94 
95 		printf("bytes per row: %u\n",
96 			static_cast<unsigned int>(header.rowBytes));
97 
98 		// print out colorspace if it matches an item in the list
99 		ColorSpaceName colorspaces[] = {
100 			COLORSPACENAME(B_NO_COLOR_SPACE),
101 			COLORSPACENAME(B_RGB32),
102 			COLORSPACENAME(B_RGBA32),
103 			COLORSPACENAME(B_RGB24),
104 			COLORSPACENAME(B_RGB16),
105 			COLORSPACENAME(B_RGB15),
106 			COLORSPACENAME(B_RGBA15),
107 			COLORSPACENAME(B_CMAP8),
108 			COLORSPACENAME(B_GRAY8),
109 			COLORSPACENAME(B_GRAY1),
110 			COLORSPACENAME(B_RGB32_BIG),
111 			COLORSPACENAME(B_RGBA32_BIG),
112 			COLORSPACENAME(B_RGB24_BIG),
113 			COLORSPACENAME(B_RGB16_BIG),
114 			COLORSPACENAME(B_RGB15_BIG),
115 			COLORSPACENAME(B_RGBA15_BIG),
116 			COLORSPACENAME(B_YCbCr422),
117 			COLORSPACENAME(B_YCbCr411),
118 			COLORSPACENAME(B_YCbCr444),
119 			COLORSPACENAME(B_YCbCr420),
120 			COLORSPACENAME(B_YUV422),
121 			COLORSPACENAME(B_YUV411),
122 			COLORSPACENAME(B_YUV444),
123 			COLORSPACENAME(B_YUV420),
124 			COLORSPACENAME(B_YUV9),
125 			COLORSPACENAME(B_YUV12),
126 			COLORSPACENAME(B_UVL24),
127 			COLORSPACENAME(B_UVL32),
128 			COLORSPACENAME(B_UVLA32),
129 			COLORSPACENAME(B_LAB24),
130 			COLORSPACENAME(B_LAB32),
131 			COLORSPACENAME(B_LABA32),
132 			COLORSPACENAME(B_HSI24),
133 			COLORSPACENAME(B_HSI32),
134 			COLORSPACENAME(B_HSIA32),
135 			COLORSPACENAME(B_HSV24),
136 			COLORSPACENAME(B_HSV32),
137 			COLORSPACENAME(B_HSVA32),
138 			COLORSPACENAME(B_HLS24),
139 			COLORSPACENAME(B_HLS32),
140 			COLORSPACENAME(B_HLSA32),
141 			COLORSPACENAME(B_CMY24),
142 			COLORSPACENAME(B_CMY32),
143 			COLORSPACENAME(B_CMYA32),
144 			COLORSPACENAME(B_CMYK32)
145 		};
146 		const int32 kncolorspaces =  sizeof(colorspaces) /
147 			sizeof(ColorSpaceName);
148 		int32 i;
149 		for (i = 0; i < kncolorspaces; i++) {
150 			if (header.colors == colorspaces[i].id) {
151 				printf("color space: %s\n", colorspaces[i].name);
152 				break;
153 			}
154 		}
155 		if (i == kncolorspaces)
156 			printf("color space: Unknown (0x%.8lx)\n",
157 				static_cast<unsigned long>(header.colors));
158 
159 		printf("data size: %u\n",
160 			static_cast<unsigned int>(header.dataSize));
161 
162 		if (bdumppixels) {
163 			const char *components = NULL;
164 			int32 ncomponents = 0;
165 			switch (header.colors) {
166 				case B_RGB24:
167 					components = "BGR";
168 					ncomponents = 3;
169 					break;
170 				case B_RGB32:
171 					components = "BGR-";
172 					ncomponents = 4;
173 					break;
174 				case B_RGBA32:
175 					components = "BGRA";
176 					ncomponents = 4;
177 					break;
178 
179 				default:
180 					printf("Sorry, %s isn't supported yet"
181 						" for this color space\n", kpixels);
182 					return;
183 			}
184 			uint8 *prow = new uint8[header.rowBytes];
185 			if (!prow) {
186 				printf("Error: Not enough memory for row buffer\n");
187 				return;
188 			}
189 			ssize_t ret, n;
190 			uint32 totalbytes = 0;
191 			printf("pixel data (%s):\n", components);
192 			while ((ret = file.Read(prow, header.rowBytes)) > 0) {
193 				n = 0;
194 				while (n < ret) {
195 					if (n && !(n % ncomponents))
196 						printf(" ");
197 					printf("%.2X", prow[n]);
198 					n++;
199 					totalbytes++;
200 					if (!(totalbytes % (uint32) ret))
201 						printf("\n\n");
202 				}
203 			}
204 		}
205 
206 	} else
207 		printf("Error opening %s\n", filepath);
208 }
209 
210 int
211 main(int argc, char **argv)
212 {
213 	if (argc == 1) {
214 		printf("\nbitsinfo - reports information about a Be bitmap (\"bits\") image\n");
215 		printf("\nUsage:\n");
216 		printf("bitsinfo [options] filename.bits\n\n");
217 		printf("Options:\n\n");
218 		printf("\t%s \t print RGB color for each pixel\n", kpixels);
219 	}
220 	else {
221 		int32 first = 1;
222 		bool bdumppixels = false;
223 		if (strcmp(argv[1], kpixels) == 0) {
224 			bdumppixels = true;
225 			first = 2;
226 		}
227 
228 		for (int32 i = first; i < argc; i++)
229 			PrintBitsInfo(argv[i], bdumppixels);
230 	}
231 
232 	printf("\n");
233 
234 	return 0;
235 }
236 
237