]> git.saurik.com Git - apple/boot.git/blob - i386/util/BooterBitmap.m
boot-111.tar.gz
[apple/boot.git] / i386 / util / BooterBitmap.m
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #import <stdio.h>
26 #import <sys/param.h>
27 #import <appkit/NXBitmapImageRep.h>
28 #import "BooterBitmap.h"
29 #import "bitmap.h"
30
31 @implementation BooterBitmap
32
33 - init
34 {
35 bg_color = BG_COLOR;
36 return [super init];
37 }
38
39 - free
40 {
41 if (bitmapImageRep) [bitmapImageRep free];
42 if (packed_planes[0]) free(packed_planes[0]);
43 if (packed_planes[1]) free(packed_planes[1]);
44 return [super free];
45 }
46
47 - initFromTiffFile: (char *)inputFileName;
48 {
49 [self init];
50 filename = inputFileName;
51 bitmapImageRep = [[NXBitmapImageRep alloc]
52 initFromFile:inputFileName];
53 if (bitmapImageRep == nil) {
54 fprintf(stderr, "BooterBitmap: couldn't load tiff file %s\n",filename);
55 return nil;
56 }
57 if ([bitmapImageRep numPlanes] - [bitmapImageRep hasAlpha] != 1) {
58 fprintf(stderr,
59 "BooterBitmap: can't deal with more than one input plane (excluding alpha)\n");
60 return nil;
61 }
62 if ([bitmapImageRep bitsPerPixel] != BITS_PER_PIXEL) {
63 fprintf(stderr,
64 "BooterBitmap: can't deal with anything but %d bits per pixel\n",
65 BITS_PER_PIXEL);
66 return nil;
67 }
68 [bitmapImageRep getDataPlanes:planes];
69 width = [bitmapImageRep pixelsWide];
70 height = [bitmapImageRep pixelsHigh];
71 bytes_per_plane = [bitmapImageRep bytesPerPlane];
72 return self;
73 }
74
75 - (BOOL)_allocPlanes
76 {
77 if (packed_planes[0]) free(packed_planes[0]);
78 if (packed_planes[1]) free(packed_planes[1]);
79 packed_planes[0] = (unsigned char *)malloc(bytes_per_plane / NPLANES);
80 packed_planes[1] = (unsigned char *)malloc(bytes_per_plane / NPLANES);
81 if (packed_planes[0] == 0 || packed_planes[1] == 0)
82 return NO;
83 else
84 return YES;
85 }
86
87 - (BOOL)_convertPlanes
88 {
89 int plane, i, j, outbit, index;
90 unsigned char *pp, *alpha, *plane_tmp, *data;
91 unsigned char alphabyte, inbyte, outbyte;
92 int new_bytes_per_plane, bytes_per_row;
93 TIFF tif;
94 int doPack = 1;
95
96 startOver:
97 if ([self _allocPlanes] == NO)
98 return NO;
99 plane_tmp = (unsigned char *)malloc(bytes_per_plane / NPLANES);
100 for (plane = 0; plane < NPLANES; plane++) {
101 int col;
102
103 data = planes[0];
104 alpha = planes[1];
105 pp = plane_tmp;
106
107 bytes_per_row = bytes_per_plane / height;
108
109 for(i=0; i < height; i++) {
110 for(j = outbyte = col = 0, outbit = 7; j < width; j++) {
111 if ((j % (8 / NPLANES)) == 0) {
112 index = (i * bytes_per_row) + (j / (8 / NPLANES));
113 inbyte = data[index];
114 if (alpha)
115 alphabyte = alpha[index];
116 }
117 if (alpha && ((alphabyte & 0xC0) == 0)) {
118 outbyte |=
119 ((bg_color & (1 << plane)) >> plane) << outbit;
120 } else {
121 outbyte |=
122 ((((inbyte & 0xC0) >> (8 - NPLANES)) & (1 << plane))
123 >> plane) << outbit;
124 }
125 if (outbit-- == 0) {
126 *pp++ = outbyte;
127 outbyte = 0;
128 outbit = 7;
129 }
130 inbyte <<= NPLANES;
131 alphabyte <<= NPLANES;
132 }
133 if (outbit < 7)
134 *pp++ = outbyte;
135 }
136 bytes_per_row = (width + 7) / 8;
137 new_bytes_per_plane = pp - plane_tmp;
138
139 tif.tif_rawdata = tif.tif_rawcp = packed_planes[plane];
140 tif.tif_rawdatasize = new_bytes_per_plane;
141 tif.tif_rawcc = 0;
142 tif.tif_row = 0;
143 pp = plane_tmp;
144 if (doPack) {
145 for (i=0; i < height; i++) {
146 if (PackBitsEncode(&tif, pp, bytes_per_row, 0) == -1) {
147 // packed data is bigger than raw data!
148 doPack = 0;
149 free(plane_tmp);
150 goto startOver;
151 }
152 pp += bytes_per_row;
153 }
154 } else {
155 bcopy(plane_tmp, packed_planes[plane], new_bytes_per_plane);
156 tif.tif_rawcc = new_bytes_per_plane;
157 }
158 plane_len[plane] = tif.tif_rawcc;
159 }
160 free(plane_tmp);
161 packed = doPack;
162 return YES;
163 }
164
165
166 - (BOOL) writeAsCFile: (char *)output_name
167 {
168 char buf[MAXPATHLEN], oname_buf[MAXPATHLEN];
169 char *name;
170 FILE *bhfile, *hfile;
171 int i, plane;
172
173 if (output_name) {
174 strcpy(oname_buf, output_name);
175 } else if (filename) {
176 strcpy(oname_buf, filename);
177 buf[strlen(buf) - strlen(".tiff")] = '\0';
178 } else {
179 fprintf(stderr,"BooterBitmap writeAsCFile: no filename\n");
180 return NO;
181 }
182 output_name = oname_buf;
183 if ([self _convertPlanes] == NO) {
184 fprintf(stderr,"_convertPlanes failed\n");
185 return NO;
186 }
187
188 name = (char *)strrchr(output_name, '/');
189 if (name == NULL)
190 name = output_name;
191 else
192 name++;
193 sprintf(buf, "%s_bitmap.h",output_name);
194 bhfile = fopen(buf,"w");
195 if (bhfile == 0) {
196 fprintf(stderr,"Couldn't open %s for writing\n",buf);
197 perror("open");
198 return NO;
199 }
200 sprintf(buf,"%s.h",output_name);
201 hfile = fopen(buf,"w");
202 if (hfile == 0) {
203 fprintf(stderr,"Couldn't open %s for writing\n",buf);
204 perror("open");
205 return NO;
206 }
207
208 for(plane = 0; plane < NPLANES; plane++) {
209 int col = 0;
210 unsigned char *pp;
211 fprintf(bhfile,"unsigned char %s_bitmap_plane_%d[] =\n",name,plane);
212 fprintf(bhfile," {\n");
213 fprintf(bhfile,"// plane %d\n",plane);
214 pp = packed_planes[plane];
215 for (i=0; i < plane_len[plane]; i++) {
216 fprintf(bhfile,"0x%02x, ",*pp++);
217 if ((col += 7) > 70) {
218 col = 0;
219 fprintf(bhfile,"\n");
220 }
221 }
222 fprintf(bhfile,"};\n");
223 }
224
225 fprintf(bhfile,"struct bitmap %s_bitmap = {\n",name);
226 fprintf(bhfile,"%d,\t// packed\n",packed);
227 fprintf(bhfile,"%d,\t// bytes_per_plane\n",bytes_per_plane / NPLANES);
228 fprintf(bhfile,"%d,\t// bytes_per_row\n", (width + 7) / 8);
229 fprintf(bhfile,"%d,\t// bits per pixel\n", 1);
230 fprintf(bhfile,"%d,\t// width\n", width);
231 fprintf(bhfile,"%d,\t// height\n", height);
232 fprintf(bhfile,"{\n");
233 fprintf(bhfile," %d,\n", plane_len[0]);
234 fprintf(bhfile," %d,\n", plane_len[1]);
235 fprintf(bhfile,"},\n");
236 fprintf(bhfile,"{\n");
237 fprintf(bhfile," %s_bitmap_plane_0,\n", name);
238 fprintf(bhfile," %s_bitmap_plane_1\n", name);
239 fprintf(bhfile,"}\n");
240 fprintf(bhfile,"};\n");
241 fprintf(bhfile,"\n#define %s_bitmap_WIDTH\t%d\n", name, width);
242 fprintf(bhfile,"#define %s_bitmap_HEIGHT\t%d\n", name, height);
243 fclose(bhfile);
244 fprintf(hfile,"extern struct bitmap %s_bitmap;\n",name);
245 fprintf(hfile,"\n#define %s_bitmap_WIDTH\t%d\n", name, width);
246 fprintf(hfile,"#define %s_bitmap_HEIGHT\t%d\n", name, height);
247 fclose(bhfile);
248 return YES;
249 }
250
251 - (BOOL) writeAsBinaryFile: (char *)outputFile
252 {
253 struct bitmap bd;
254 char buf[MAXPATHLEN];
255 FILE *file;
256
257 if (outputFile) {
258 strcpy(buf, outputFile);
259 } else if (filename) {
260 strcpy(buf, filename);
261 } else {
262 fprintf(stderr,"writeAsBinaryFile: no filename\n");
263 return NO;
264 }
265 strcat(buf, ".image");
266 file = fopen(buf, "w");
267 if (file == NULL) {
268 fprintf(stderr, "writeAsBinaryFile: couldn't open output file %s\n",
269 buf);
270 return NO;
271 }
272 if ([self _convertPlanes] == NO) {
273 fprintf(stderr,"_convertPlanes failed\n");
274 return NO;
275 }
276 bd.packed = packed;
277 bd.bytes_per_plane = bytes_per_plane / NPLANES;
278 bd.bytes_per_row = (width + 7) / 8;
279 bd.bits_per_pixel = 1;
280 bd.width = width;
281 bd.height = height;
282 bd.plane_len[0] = plane_len[0];
283 bd.plane_len[1] = plane_len[1];
284 bd.plane_data[0] = bd.plane_data[1] = 0;
285 if (fwrite(&bd, sizeof(bd), 1, file) < 1) goto error;
286 if (fwrite(packed_planes[0], plane_len[0], 1, file) < 1) goto error;
287 if (fwrite(packed_planes[1], plane_len[1], 1, file) < 1) goto error;
288 fclose(file);
289 return YES;
290 error:
291 perror("fwrite");
292 return NO;
293 }
294
295
296 - (int) width
297 {
298 return width;
299 }
300
301 - (int) height
302 {
303 return height;
304 }
305
306 - (int) setWidth: (int)newWidth
307 {
308 return width = newWidth;
309 }
310
311 - (int) setHeight: (int)newHeight
312 {
313 return height = newHeight;
314 }
315
316 - (int) bgColor
317 {
318 return bg_color;
319 }
320
321 - (int) setBgColor: (int)newColor
322 {
323 return bg_color = newColor;
324 }
325
326 - (BOOL) setTwoBitsPerPixelColorData: (unsigned char *)bits;
327 {
328 planes[0] = bits;
329 return YES;
330 }
331
332 - (BOOL) setTwoBitsPerPixelAlphaData: (unsigned char *)bits;
333 {
334 planes[1] = bits;
335 return YES;
336 }
337
338 - (unsigned char *) twoBitsPerPixelColorData
339 {
340 return planes[0];
341 }
342
343 - (unsigned char *) twoBitsPerPixelAlphaData
344 {
345 return planes[1];
346 }
347
348 - (int) colorDataBytes
349 {
350 return bytes_per_plane;
351 }
352
353 - (int) setColorDataBytes: (int)bpp
354 {
355 return bytes_per_plane = bpp;
356 }
357
358 - (char *)filename
359 {
360 return filename;
361 }
362
363
364 @end