2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
27 #import <appkit/NXBitmapImageRep.h>
28 #import "BooterBitmap.h"
31 @implementation BooterBitmap
41 if (bitmapImageRep) [bitmapImageRep free];
42 if (packed_planes[0]) free(packed_planes[0]);
43 if (packed_planes[1]) free(packed_planes[1]);
47 - initFromTiffFile: (char *)inputFileName;
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);
57 if ([bitmapImageRep numPlanes] - [bitmapImageRep hasAlpha] != 1) {
59 "BooterBitmap: can't deal with more than one input plane (excluding alpha)\n");
62 if ([bitmapImageRep bitsPerPixel] != BITS_PER_PIXEL) {
64 "BooterBitmap: can't deal with anything but %d bits per pixel\n",
68 [bitmapImageRep getDataPlanes:planes];
69 width = [bitmapImageRep pixelsWide];
70 height = [bitmapImageRep pixelsHigh];
71 bytes_per_plane = [bitmapImageRep bytesPerPlane];
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)
87 - (BOOL)_convertPlanes
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;
97 if ([self _allocPlanes] == NO)
99 plane_tmp = (unsigned char *)malloc(bytes_per_plane / NPLANES);
100 for (plane = 0; plane < NPLANES; plane++) {
107 bytes_per_row = bytes_per_plane / height;
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];
115 alphabyte = alpha[index];
117 if (alpha && ((alphabyte & 0xC0) == 0)) {
119 ((bg_color & (1 << plane)) >> plane) << outbit;
122 ((((inbyte & 0xC0) >> (8 - NPLANES)) & (1 << plane))
131 alphabyte <<= NPLANES;
136 bytes_per_row = (width + 7) / 8;
137 new_bytes_per_plane = pp - plane_tmp;
139 tif.tif_rawdata = tif.tif_rawcp = packed_planes[plane];
140 tif.tif_rawdatasize = new_bytes_per_plane;
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!
155 bcopy(plane_tmp, packed_planes[plane], new_bytes_per_plane);
156 tif.tif_rawcc = new_bytes_per_plane;
158 plane_len[plane] = tif.tif_rawcc;
166 - (BOOL) writeAsCFile: (char *)output_name
168 char buf[MAXPATHLEN], oname_buf[MAXPATHLEN];
170 FILE *bhfile, *hfile;
174 strcpy(oname_buf, output_name);
175 } else if (filename) {
176 strcpy(oname_buf, filename);
177 buf[strlen(buf) - strlen(".tiff")] = '\0';
179 fprintf(stderr,"BooterBitmap writeAsCFile: no filename\n");
182 output_name = oname_buf;
183 if ([self _convertPlanes] == NO) {
184 fprintf(stderr,"_convertPlanes failed\n");
188 name = (char *)strrchr(output_name, '/');
193 sprintf(buf, "%s_bitmap.h",output_name);
194 bhfile = fopen(buf,"w");
196 fprintf(stderr,"Couldn't open %s for writing\n",buf);
200 sprintf(buf,"%s.h",output_name);
201 hfile = fopen(buf,"w");
203 fprintf(stderr,"Couldn't open %s for writing\n",buf);
208 for(plane = 0; plane < NPLANES; plane++) {
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) {
219 fprintf(bhfile,"\n");
222 fprintf(bhfile,"};\n");
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);
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);
251 - (BOOL) writeAsBinaryFile: (char *)outputFile
254 char buf[MAXPATHLEN];
258 strcpy(buf, outputFile);
259 } else if (filename) {
260 strcpy(buf, filename);
262 fprintf(stderr,"writeAsBinaryFile: no filename\n");
265 strcat(buf, ".image");
266 file = fopen(buf, "w");
268 fprintf(stderr, "writeAsBinaryFile: couldn't open output file %s\n",
272 if ([self _convertPlanes] == NO) {
273 fprintf(stderr,"_convertPlanes failed\n");
277 bd.bytes_per_plane = bytes_per_plane / NPLANES;
278 bd.bytes_per_row = (width + 7) / 8;
279 bd.bits_per_pixel = 1;
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;
306 - (int) setWidth: (int)newWidth
308 return width = newWidth;
311 - (int) setHeight: (int)newHeight
313 return height = newHeight;
321 - (int) setBgColor: (int)newColor
323 return bg_color = newColor;
326 - (BOOL) setTwoBitsPerPixelColorData: (unsigned char *)bits;
332 - (BOOL) setTwoBitsPerPixelAlphaData: (unsigned char *)bits;
338 - (unsigned char *) twoBitsPerPixelColorData
343 - (unsigned char *) twoBitsPerPixelAlphaData
348 - (int) colorDataBytes
350 return bytes_per_plane;
353 - (int) setColorDataBytes: (int)bpp
355 return bytes_per_plane = bpp;