Permalink
Browse files

Program to convert back and forth between PPM and CHRP boot icon images.

  • Loading branch information...
1 parent a2bca40 commit 9727c191a8620119441dec7b55c9d66b77eb8ee3 lonhyn committed Nov 19, 1999
@@ -0,0 +1,5 @@
+# $NetBSD: Makefile,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $
+
+SUBDIR= chrpicontoppm ppmtochrpicon
+
+.include <bsd.subdir.mk>
@@ -0,0 +1,10 @@
+
+$NetBSD: README,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $
+
+Routines for converting back and forth between CHRP boot icons
+in an SGML type ASCII format and PPM files.
+
+These two programs depend on PPM support being installed. This is
+most easily accomplished by installing the graphics/netpbm package.
+
+The makefiles as written expect to find PBM under /usr/pkg
@@ -0,0 +1,74 @@
+/* $NetBSD: chrpicon.h,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $ */
+
+/*-
+ * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lonhyn T. Jasinskyj.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef CHRPICON_H
+#define CHRPICON_H
+
+#define MAX_LINE_LENGTH 8192
+
+#define ICON_TAG "<ICON"
+#define BITMAP_TAG "<BITMAP>"
+
+#define PLAIN_PPM 1
+
+
+typedef unsigned char chrpi_pixel;
+
+typedef
+struct chrpi_spec_s {
+ int width;
+ int height;
+ int rbits;
+ int gbits;
+ int bbits;
+ chrpi_pixel **pixels;
+} CHRPI_spec_rec;
+
+typedef CHRPI_spec_rec *CHRPI_spec;
+
+int CHRPI_getheader(FILE *, CHRPI_spec);
+int CHRPI_getbitmap(FILE *, CHRPI_spec);
+
+void CHRPI_writeicon(FILE *fp, pixel **pixels, CHRPI_spec img);
+
+void CHRPI_putbitmap(FILE *, pixel** pixels, CHRPI_spec img);
+void CHRPI_putheader(FILE *, CHRPI_spec img);
+void CHRPI_putfooter(FILE *, CHRPI_spec img);
+
+
+#endif /* CHRPICON_H */
@@ -0,0 +1,12 @@
+# $NetBSD: Makefile,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $
+
+PROG= chrpicontoppm
+MKMAN=no
+
+PBMROOT=/usr/pkg
+
+CPPFLAGS+= -I${.CURDIR}/.. -I${PBMROOT}/include
+LDADD+=-L${PBMROOT}/lib -lpnm -lppm -lpgm -lpbm
+
+.include <bsd.prog.mk>
+
@@ -0,0 +1,212 @@
+/* $NetBSD: chrpicontoppm.c,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $ */
+
+/*-
+ * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lonhyn T. Jasinskyj.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * chrpicontoppm.c - read a CHRP style boot icon file and convert
+ * it to a PPM.
+ */
+
+/*
+ * Usage:
+ *
+ * chrpicontoppm [chrpiconfile]
+ *
+ * This programs reads from either a single file given as an argument
+ * or from stdin if no args are given. It expects a true color
+ * PPM file as the input. The image should be 64x64, otherwise it
+ * is cropped to that size.
+ *
+ * It then produces a CHRP style boot icon file on stdout.
+ */
+
+#include <stdlib.h>
+
+#include <pbm.h>
+#include <ppm.h>
+
+#include "chrpicon.h"
+
+
+int
+main(int argc, char *argv[])
+{
+ FILE *ifp;
+ CHRPI_spec_rec img_rec;
+ CHRPI_spec img = &img_rec;
+ pixel *pixelrow;
+ pixel *pP;
+ chrpi_pixel *imgP;
+ int row, col;
+ pixval maxval = 255;
+
+
+ ppm_init(&argc, argv);
+
+ if (argc > 2)
+ pm_usage("[chrpiconfile]");
+
+ /* either use stdin or open a file */
+ if (argc > 1) {
+ if ((ifp = fopen(argv[1], "r")) == NULL) {
+ perror("ppmfile open");
+ exit(1);
+ }
+ }
+ else
+ ifp = stdin;
+
+ if (CHRPI_getheader(ifp, img))
+ pm_error("can't find <ICON...> header in boot icon file");
+
+ if (CHRPI_getbitmap(ifp, img))
+ pm_error("can't read <BITMAP...> section in boot icon file");
+
+ if (img->rbits != 3 || img->gbits != 3 || img->bbits != 2)
+ pm_error("can only handle RGB 3:3:2 colorspace icon files");
+
+ ppm_writeppminit(stdout, img->width, img->height, maxval, PLAIN_PPM);
+ pixelrow = ppm_allocrow(img->width);
+
+ for (row = 0; row < img->height; row++) {
+
+ pixval r, g, b;
+
+ pP = pixelrow;
+ imgP = img->pixels[row];
+
+ for (col = 0; col < img->width; col++) {
+
+ r = ((*imgP >> 5) & 7);
+ g = ((*imgP >> 2) & 7);
+ b = (*imgP & 3);
+
+ r = (r << 5) | (r << 2) | (r >> 1);
+ g = (g << 5) | (g << 2) | (g >> 1);
+ b = (b << 6) | (b << 4) | (b >> 4) | b;
+
+ PPM_ASSIGN(*pP, r, g, b);
+
+ pP++;
+ imgP++;
+ }
+
+ ppm_writeppmrow(stdout, pixelrow, img->width, maxval, PLAIN_PPM);
+ }
+
+ ppm_freerow(pixelrow);
+
+ pm_close(ifp);
+ pm_close(stdout);
+ exit(0);
+}
+
+
+chrpi_pixel *
+CHRPI_allocrow(int cols)
+{
+ return calloc(cols, sizeof(chrpi_pixel));
+}
+
+int
+CHRPI_getheader(FILE *fp, CHRPI_spec img)
+{
+ char line[MAX_LINE_LENGTH + 1];
+
+ while (fgets(line, MAX_LINE_LENGTH, fp)) {
+ if (strstr(line, ICON_TAG)) {
+ /* found the ICON identifier, primitively parse it */
+ if (sscanf(line, " %*s SIZE=%d,%d COLOR-SPACE=%d,%d,%d",
+ &img->height, &img->width,
+ &img->rbits, &img->gbits, &img->bbits
+ ) != 5)
+ return -1;
+
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+
+int
+CHRPI_getbitmap(FILE *fp, CHRPI_spec img)
+{
+ char line[MAX_LINE_LENGTH + 1];
+ int foundtag = 0;
+ char hexstr[3] = { 0, 0, 0 };
+ char *p;
+ int r, c;
+
+
+ /* first find the BITMAP tag */
+ while (fgets(line, MAX_LINE_LENGTH, fp)) {
+ if (strncmp(line, BITMAP_TAG, strlen(BITMAP_TAG)) == 0) {
+ foundtag++;
+ break;
+ }
+ }
+
+ if (!foundtag)
+ return -1;
+
+ if ((img->pixels = calloc(img->height, sizeof(chrpi_pixel *))) == NULL)
+ return -1;
+
+ for (r = 0; r < img->height; r++)
+ if ((img->pixels[r] = CHRPI_allocrow(img->width)) == NULL)
+ return -1;
+
+ for (r = 0; r < img->height; r++) {
+
+ /* get a row */
+ if ((p = fgets(line, MAX_LINE_LENGTH, fp)) == NULL) {
+ return -1;
+ }
+
+ /* go down the pixels and convert them */
+ for (c = 0; c < img->width; c++) {
+ hexstr[0] = *p++;
+ hexstr[1] = *p++;
+
+ img->pixels[r][c] = (chrpi_pixel)(strtoul(hexstr, NULL, 16));
+ }
+ }
+
+ return 0;
+}
@@ -0,0 +1,12 @@
+# $NetBSD: Makefile,v 1.1.1.1 1999/11/19 00:43:20 lonhyn Exp $
+
+PROG= ppmtochrpicon
+MKMAN=no
+
+PBMROOT=/usr/pkg
+
+CPPFLAGS+= -I${.CURDIR}/.. -I${PBMROOT}/include
+LDADD+=-L${PBMROOT}/lib -lpnm -lppm -lpgm -lpbm
+
+.include <bsd.prog.mk>
+
Oops, something went wrong.

0 comments on commit 9727c19

Please sign in to comment.