xref: /haiku/src/tools/translation/bitsinfo/bitsinfo.cpp (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
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 #include <StorageDefs.h>
44 
45 struct ColorSpaceName {
46 	color_space id;
47 	const char *name;
48 };
49 #define COLORSPACENAME(id) {id, #id}
50 
51 const char *kpixels = "-pixels";
52 
53 void
54 PrintBitsInfo(const char *filepath, bool bdumppixels)
55 {
56 	BFile file(filepath, B_READ_ONLY);
57 
58 	if (file.InitCheck() == B_OK) {
59 		TranslatorBitmap header;
60 		memset(&header, 0, sizeof(TranslatorBitmap));
61 
62 		// read in the rest of the header
63 		ssize_t size = sizeof(TranslatorBitmap);
64 		if (file.Read(reinterpret_cast<uint8 *> (&header), size) != size) {
65 			printf("\nError: Unable to read the Be bitmap header.\n");
66 			return;
67 		}
68 		if (!bdumppixels)
69 			// I don't need the file anymore
70 			file.Unset();
71 
72 		// convert to host byte order
73 		if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
74 			B_SWAP_BENDIAN_TO_HOST) != B_OK) {
75 			printf("\nError: Unable to swap byte order\n");
76 			return;
77 		}
78 
79 		printf("\nBe bitmap (\"bits\") header for: %s\n\n", filepath);
80 
81 		const uint32 kbitsmagic = 0x62697473UL;
82 			// in ASCII, this number looks like "bits"
83 		printf("magic number: 0x%.8lx ", header.magic);
84 		if (header.magic == kbitsmagic)
85 			printf("(valid)\n");
86 		else
87 			printf("(INVALID, should be: 0x%.8lx)\n",
88 				kbitsmagic);
89 		printf("bounds: (%f, %f, %f, %f)\n",
90 			header.bounds.left, header.bounds.top,
91 			header.bounds.right, header.bounds.bottom);
92 		printf("dimensions: %d x %d\n",
93 			static_cast<int>(header.bounds.Width() + 1),
94 			static_cast<int>(header.bounds.Height() + 1));
95 
96 		printf("bytes per row: %u\n",
97 			static_cast<unsigned int>(header.rowBytes));
98 
99 		// print out colorspace if it matches an item in the list
100 		ColorSpaceName colorspaces[] = {
101 			COLORSPACENAME(B_NO_COLOR_SPACE),
102 			COLORSPACENAME(B_RGB32),
103 			COLORSPACENAME(B_RGBA32),
104 			COLORSPACENAME(B_RGB24),
105 			COLORSPACENAME(B_RGB16),
106 			COLORSPACENAME(B_RGB15),
107 			COLORSPACENAME(B_RGBA15),
108 			COLORSPACENAME(B_CMAP8),
109 			COLORSPACENAME(B_GRAY8),
110 			COLORSPACENAME(B_GRAY1),
111 			COLORSPACENAME(B_RGB32_BIG),
112 			COLORSPACENAME(B_RGBA32_BIG),
113 			COLORSPACENAME(B_RGB24_BIG),
114 			COLORSPACENAME(B_RGB16_BIG),
115 			COLORSPACENAME(B_RGB15_BIG),
116 			COLORSPACENAME(B_RGBA15_BIG),
117 			COLORSPACENAME(B_YCbCr422),
118 			COLORSPACENAME(B_YCbCr411),
119 			COLORSPACENAME(B_YCbCr444),
120 			COLORSPACENAME(B_YCbCr420),
121 			COLORSPACENAME(B_YUV422),
122 			COLORSPACENAME(B_YUV411),
123 			COLORSPACENAME(B_YUV444),
124 			COLORSPACENAME(B_YUV420),
125 			COLORSPACENAME(B_YUV9),
126 			COLORSPACENAME(B_YUV12),
127 			COLORSPACENAME(B_UVL24),
128 			COLORSPACENAME(B_UVL32),
129 			COLORSPACENAME(B_UVLA32),
130 			COLORSPACENAME(B_LAB24),
131 			COLORSPACENAME(B_LAB32),
132 			COLORSPACENAME(B_LABA32),
133 			COLORSPACENAME(B_HSI24),
134 			COLORSPACENAME(B_HSI32),
135 			COLORSPACENAME(B_HSIA32),
136 			COLORSPACENAME(B_HSV24),
137 			COLORSPACENAME(B_HSV32),
138 			COLORSPACENAME(B_HSVA32),
139 			COLORSPACENAME(B_HLS24),
140 			COLORSPACENAME(B_HLS32),
141 			COLORSPACENAME(B_HLSA32),
142 			COLORSPACENAME(B_CMY24),
143 			COLORSPACENAME(B_CMY32),
144 			COLORSPACENAME(B_CMYA32),
145 			COLORSPACENAME(B_CMYK32)
146 		};
147 		const int32 kncolorspaces =  sizeof(colorspaces) /
148 			sizeof(ColorSpaceName);
149 		int32 i;
150 		for (i = 0; i < kncolorspaces; i++) {
151 			if (header.colors == colorspaces[i].id) {
152 				printf("color space: %s\n", colorspaces[i].name);
153 				break;
154 			}
155 		}
156 		if (i == kncolorspaces)
157 			printf("color space: Unknown (0x%.8lx)\n",
158 				static_cast<unsigned long>(header.colors));
159 
160 		printf("data size: %u\n",
161 			static_cast<unsigned int>(header.dataSize));
162 
163 		if (bdumppixels) {
164 			const char *components = NULL;
165 			int32 ncomponents = 0;
166 			switch (header.colors) {
167 				case B_RGB24:
168 					components = "BGR";
169 					ncomponents = 3;
170 					break;
171 				case B_RGB32:
172 					components = "BGR-";
173 					ncomponents = 4;
174 					break;
175 				case B_RGBA32:
176 					components = "BGRA";
177 					ncomponents = 4;
178 					break;
179 
180 				default:
181 					printf("Sorry, %s isn't supported yet"
182 						" for this color space\n", kpixels);
183 					return;
184 			}
185 			uint8 *prow = new uint8[header.rowBytes];
186 			if (!prow) {
187 				printf("Error: Not enough memory for row buffer\n");
188 				return;
189 			}
190 			ssize_t ret, n;
191 			uint32 totalbytes = 0;
192 			printf("pixel data (%s):\n", components);
193 			while ((ret = file.Read(prow, header.rowBytes)) > 0) {
194 				n = 0;
195 				while (n < ret) {
196 					if (n && !(n % ncomponents))
197 						printf(" ");
198 					printf("%.2X", prow[n]);
199 					n++;
200 					totalbytes++;
201 					if (!(totalbytes % (uint32) ret))
202 						printf("\n\n");
203 				}
204 			}
205 		}
206 
207 	} else
208 		printf("Error opening %s\n", filepath);
209 }
210 
211 int
212 main(int argc, char **argv)
213 {
214 	if (argc == 1) {
215 		printf("\nbitsinfo - reports information about a Be bitmap (\"bits\") image\n");
216 		printf("\nUsage:\n");
217 		printf("bitsinfo [options] filename.bits\n\n");
218 		printf("Options:\n\n");
219 		printf("\t%s \t print RGB color for each pixel\n", kpixels);
220 	}
221 	else {
222 		int32 first = 1;
223 		bool bdumppixels = false;
224 		if (strcmp(argv[1], kpixels) == 0) {
225 			bdumppixels = true;
226 			first = 2;
227 		}
228 
229 		for (int32 i = first; i < argc; i++)
230 			PrintBitsInfo(argv[i], bdumppixels);
231 	}
232 
233 	printf("\n");
234 
235 	return 0;
236 }
237 
238