xref: /haiku/src/tools/translation/bitsinfo/bitsinfo.cpp (revision 06b932a49d65e82cdfa7d28a04f48eef6de9ea49)
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 <StorageDefs.h>
43 #include <TranslatorFormats.h>
44 
45 struct ColorSpaceName {
46 	color_space id;
47 	const char *name;
48 };
49 #define COLORSPACENAME(id) {id, #id}
50 
51 void
52 PrintBitsInfo(const char *filepath)
53 {
54 	BFile file(filepath, B_READ_ONLY);
55 
56 	if (file.InitCheck() == B_OK) {
57 		TranslatorBitmap header;
58 		memset(&header, 0, sizeof(TranslatorBitmap));
59 
60 		// read in the rest of the header
61 		ssize_t size = sizeof(TranslatorBitmap);
62 		if (file.Read(reinterpret_cast<uint8 *> (&header), size) != size) {
63 			printf("Error: Unable to read the Be bitmap header.\n");
64 			return;
65 		}
66 		file.Unset();
67 			// I don't need the file anymore
68 
69 		// convert to host byte order
70 		if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
71 			B_SWAP_BENDIAN_TO_HOST) != B_OK) {
72 			printf("Error: Unable to swap byte order\n");
73 			return;
74 		}
75 
76 		printf("Be bitmap (\"bits\") header for: %s\n\n", filepath);
77 
78 		const uint32 kbitsmagic = 0x62697473UL;
79 			// in ASCII, this number looks like "bits"
80 		printf("magic number: 0x%.8lx ", header.magic);
81 		if (header.magic == kbitsmagic)
82 			printf("(valid)\n");
83 		else
84 			printf("(INVALID, should be: 0x%.8lx)\n",
85 				kbitsmagic);
86 		printf("bounds: (%f, %f, %f, %f)\n",
87 			header.bounds.left, header.bounds.top,
88 			header.bounds.right, header.bounds.bottom);
89 		printf("dimensions: %d x %d\n",
90 			static_cast<int>(header.bounds.Width() + 1),
91 			static_cast<int>(header.bounds.Height() + 1));
92 
93 		printf("bytes per row: %u\n",
94 			static_cast<unsigned int>(header.rowBytes));
95 
96 		// print out colorspace if it matches an item in the list
97 		ColorSpaceName colorspaces[] = {
98 			COLORSPACENAME(B_NO_COLOR_SPACE),
99 			COLORSPACENAME(B_RGB32),
100 			COLORSPACENAME(B_RGBA32),
101 			COLORSPACENAME(B_RGB24),
102 			COLORSPACENAME(B_RGB16),
103 			COLORSPACENAME(B_RGB15),
104 			COLORSPACENAME(B_RGBA15),
105 			COLORSPACENAME(B_CMAP8),
106 			COLORSPACENAME(B_GRAY8),
107 			COLORSPACENAME(B_GRAY1),
108 			COLORSPACENAME(B_RGB32_BIG),
109 			COLORSPACENAME(B_RGBA32_BIG),
110 			COLORSPACENAME(B_RGB24_BIG),
111 			COLORSPACENAME(B_RGB16_BIG),
112 			COLORSPACENAME(B_RGB15_BIG),
113 			COLORSPACENAME(B_RGBA15_BIG),
114 			COLORSPACENAME(B_YCbCr422),
115 			COLORSPACENAME(B_YCbCr411),
116 			COLORSPACENAME(B_YCbCr444),
117 			COLORSPACENAME(B_YCbCr420),
118 			COLORSPACENAME(B_YUV422),
119 			COLORSPACENAME(B_YUV411),
120 			COLORSPACENAME(B_YUV444),
121 			COLORSPACENAME(B_YUV420),
122 			COLORSPACENAME(B_YUV9),
123 			COLORSPACENAME(B_YUV12),
124 			COLORSPACENAME(B_UVL24),
125 			COLORSPACENAME(B_UVL32),
126 			COLORSPACENAME(B_UVLA32),
127 			COLORSPACENAME(B_LAB24),
128 			COLORSPACENAME(B_LAB32),
129 			COLORSPACENAME(B_LABA32),
130 			COLORSPACENAME(B_HSI24),
131 			COLORSPACENAME(B_HSI32),
132 			COLORSPACENAME(B_HSIA32),
133 			COLORSPACENAME(B_HSV24),
134 			COLORSPACENAME(B_HSV32),
135 			COLORSPACENAME(B_HSVA32),
136 			COLORSPACENAME(B_HLS24),
137 			COLORSPACENAME(B_HLS32),
138 			COLORSPACENAME(B_HLSA32),
139 			COLORSPACENAME(B_CMY24),
140 			COLORSPACENAME(B_CMY32),
141 			COLORSPACENAME(B_CMYA32),
142 			COLORSPACENAME(B_CMYK32)
143 		};
144 		const int32 kncolorspaces =  sizeof(colorspaces) /
145 			sizeof(ColorSpaceName);
146 		int32 i;
147 		for (i = 0; i < kncolorspaces; i++) {
148 			if (header.colors == colorspaces[i].id) {
149 				printf("color space: %s\n", colorspaces[i].name);
150 				break;
151 			}
152 		}
153 		if (i == kncolorspaces)
154 			printf("color space: Unknown (0x%.8lx)\n",
155 				static_cast<unsigned long>(header.colors));
156 
157 		printf("data size: %u\n",
158 			static_cast<unsigned int>(header.dataSize));
159 	} else
160 		printf("Error opening %s\n", filepath);
161 }
162 
163 int
164 main(int argc, char **argv)
165 {
166 	printf("\n");
167 
168 	if (argc == 2)
169 		PrintBitsInfo(argv[1]);
170 	else {
171 		printf("bitsinfo - reports information about a Be bitmap (\"bits\") image\n");
172 		printf("\nUsage:\n");
173 		printf("bitsinfo filename.bits\n\n");
174 	}
175 
176 	return 0;
177 }
178 
179