2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 #import <appkit/NXBitmapImageRep.h>
27 #import "BooterBitmap.h"
30 @implementation BooterBitmap
40 if (bitmapImageRep) [bitmapImageRep free];
41 if (packed_planes[0]) free(packed_planes[0]);
42 if (packed_planes[1]) free(packed_planes[1]);
46 - initFromTiffFile: (char *)inputFileName;
49 filename = inputFileName;
50 bitmapImageRep = [[NXBitmapImageRep alloc]
51 initFromFile:inputFileName];
52 if (bitmapImageRep == nil) {
53 fprintf(stderr, "BooterBitmap: couldn't load tiff file %s\n",filename);
56 if ([bitmapImageRep numPlanes] - [bitmapImageRep hasAlpha] != 1) {
58 "BooterBitmap: can't deal with more than one input plane (excluding alpha)\n");
61 if ([bitmapImageRep bitsPerPixel] != BITS_PER_PIXEL) {
63 "BooterBitmap: can't deal with anything but %d bits per pixel\n",
67 [bitmapImageRep getDataPlanes:planes];
68 width = [bitmapImageRep pixelsWide];
69 height = [bitmapImageRep pixelsHigh];
70 bytes_per_plane = [bitmapImageRep bytesPerPlane];
76 if (packed_planes[0]) free(packed_planes[0]);
77 if (packed_planes[1]) free(packed_planes[1]);
78 packed_planes[0] = (unsigned char *)malloc(bytes_per_plane / NPLANES);
79 packed_planes[1] = (unsigned char *)malloc(bytes_per_plane / NPLANES);
80 if (packed_planes[0] == 0 || packed_planes[1] == 0)
86 - (BOOL)_convertPlanes
88 int plane, i, j, outbit, index;
89 unsigned char *pp, *alpha, *plane_tmp, *data;
90 unsigned char alphabyte, inbyte, outbyte;
91 int new_bytes_per_plane, bytes_per_row;
96 if ([self _allocPlanes] == NO)
98 plane_tmp = (unsigned char *)malloc(bytes_per_plane / NPLANES);
99 for (plane = 0; plane < NPLANES; plane++) {
106 bytes_per_row = bytes_per_plane / height;
108 for(i=0; i < height; i++) {
109 for(j = outbyte = col = 0, outbit = 7; j < width; j++) {
110 if ((j % (8 / NPLANES)) == 0) {
111 index = (i * bytes_per_row) + (j / (8 / NPLANES));
112 inbyte = data[index];
114 alphabyte = alpha[index];
116 if (alpha && ((alphabyte & 0xC0) == 0)) {
118 ((bg_color & (1 << plane)) >> plane) << outbit;
121 ((((inbyte & 0xC0) >> (8 - NPLANES)) & (1 << plane))
130 alphabyte <<= NPLANES;
135 bytes_per_row = (width + 7) / 8;
136 new_bytes_per_plane = pp - plane_tmp;
138 tif.tif_rawdata = tif.tif_rawcp = packed_planes[plane];
139 tif.tif_rawdatasize = new_bytes_per_plane;
144 for (i=0; i < height; i++) {
145 if (PackBitsEncode(&tif, pp, bytes_per_row, 0) == -1) {
146 // packed data is bigger than raw data!
154 bcopy(plane_tmp, packed_planes[plane], new_bytes_per_plane);
155 tif.tif_rawcc = new_bytes_per_plane;
157 plane_len[plane] = tif.tif_rawcc;
165 - (BOOL) writeAsCFile: (char *)output_name
167 char buf[MAXPATHLEN], oname_buf[MAXPATHLEN];
169 FILE *bhfile, *hfile;
173 strcpy(oname_buf, output_name);
174 } else if (filename) {
175 strcpy(oname_buf, filename);
176 buf[strlen(buf) - strlen(".tiff")] = '\0';
178 fprintf(stderr,"BooterBitmap writeAsCFile: no filename\n");
181 output_name = oname_buf;
182 if ([self _convertPlanes] == NO) {
183 fprintf(stderr,"_convertPlanes failed\n");
187 name = (char *)strrchr(output_name, '/');
192 sprintf(buf, "%s_bitmap.h",output_name);
193 bhfile = fopen(buf,"w");
195 fprintf(stderr,"Couldn't open %s for writing\n",buf);
199 sprintf(buf,"%s.h",output_name);
200 hfile = fopen(buf,"w");
202 fprintf(stderr,"Couldn't open %s for writing\n",buf);
207 for(plane = 0; plane < NPLANES; plane++) {
210 fprintf(bhfile,"unsigned char %s_bitmap_plane_%d[] =\n",name,plane);
211 fprintf(bhfile," {\n");
212 fprintf(bhfile,"// plane %d\n",plane);
213 pp = packed_planes[plane];
214 for (i=0; i < plane_len[plane]; i++) {
215 fprintf(bhfile,"0x%02x, ",*pp++);
216 if ((col += 7) > 70) {
218 fprintf(bhfile,"\n");
221 fprintf(bhfile,"};\n");
224 fprintf(bhfile,"struct bitmap %s_bitmap = {\n",name);
225 fprintf(bhfile,"%d,\t// packed\n",packed);
226 fprintf(bhfile,"%d,\t// bytes_per_plane\n",bytes_per_plane / NPLANES);
227 fprintf(bhfile,"%d,\t// bytes_per_row\n", (width + 7) / 8);
228 fprintf(bhfile,"%d,\t// bits per pixel\n", 1);
229 fprintf(bhfile,"%d,\t// width\n", width);
230 fprintf(bhfile,"%d,\t// height\n", height);
231 fprintf(bhfile,"{\n");
232 fprintf(bhfile," %d,\n", plane_len[0]);
233 fprintf(bhfile," %d,\n", plane_len[1]);
234 fprintf(bhfile,"},\n");
235 fprintf(bhfile,"{\n");
236 fprintf(bhfile," %s_bitmap_plane_0,\n", name);
237 fprintf(bhfile," %s_bitmap_plane_1\n", name);
238 fprintf(bhfile,"}\n");
239 fprintf(bhfile,"};\n");
240 fprintf(bhfile,"\n#define %s_bitmap_WIDTH\t%d\n", name, width);
241 fprintf(bhfile,"#define %s_bitmap_HEIGHT\t%d\n", name, height);
243 fprintf(hfile,"extern struct bitmap %s_bitmap;\n",name);
244 fprintf(hfile,"\n#define %s_bitmap_WIDTH\t%d\n", name, width);
245 fprintf(hfile,"#define %s_bitmap_HEIGHT\t%d\n", name, height);
250 - (BOOL) writeAsBinaryFile: (char *)outputFile
253 char buf[MAXPATHLEN];
257 strcpy(buf, outputFile);
258 } else if (filename) {
259 strcpy(buf, filename);
261 fprintf(stderr,"writeAsBinaryFile: no filename\n");
264 strcat(buf, ".image");
265 file = fopen(buf, "w");
267 fprintf(stderr, "writeAsBinaryFile: couldn't open output file %s\n",
271 if ([self _convertPlanes] == NO) {
272 fprintf(stderr,"_convertPlanes failed\n");
276 bd.bytes_per_plane = bytes_per_plane / NPLANES;
277 bd.bytes_per_row = (width + 7) / 8;
278 bd.bits_per_pixel = 1;
281 bd.plane_len[0] = plane_len[0];
282 bd.plane_len[1] = plane_len[1];
283 bd.plane_data[0] = bd.plane_data[1] = 0;
284 if (fwrite(&bd, sizeof(bd), 1, file) < 1) goto error;
285 if (fwrite(packed_planes[0], plane_len[0], 1, file) < 1) goto error;
286 if (fwrite(packed_planes[1], plane_len[1], 1, file) < 1) goto error;
305 - (int) setWidth: (int)newWidth
307 return width = newWidth;
310 - (int) setHeight: (int)newHeight
312 return height = newHeight;
320 - (int) setBgColor: (int)newColor
322 return bg_color = newColor;
325 - (BOOL) setTwoBitsPerPixelColorData: (unsigned char *)bits;
331 - (BOOL) setTwoBitsPerPixelAlphaData: (unsigned char *)bits;
337 - (unsigned char *) twoBitsPerPixelColorData
342 - (unsigned char *) twoBitsPerPixelAlphaData
347 - (int) colorDataBytes
349 return bytes_per_plane;
352 - (int) setColorDataBytes: (int)bpp
354 return bytes_per_plane = bpp;