]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[pnm] Add support for PNM images
authorMichael Brown <mcb30@ipxe.org>
Tue, 26 Nov 2013 16:29:30 +0000 (16:29 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 27 Nov 2013 11:27:51 +0000 (11:27 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/config/config.c
src/config/general.h
src/image/pnm.c [new file with mode: 0644]
src/include/ipxe/errfile.h
src/include/ipxe/pnm.h [new file with mode: 0644]
src/tests/pnm_test.c [new file with mode: 0644]
src/tests/tests.c

index 6d19370ab9fffd91d1737414aff650f5f359f329..89af88acdb600119579380470f1e3946dccd1737 100644 (file)
@@ -194,6 +194,9 @@ REQUIRE_OBJECT ( efi_image );
 #ifdef IMAGE_SDI
 REQUIRE_OBJECT ( sdi );
 #endif
+#ifdef IMAGE_PNM
+REQUIRE_OBJECT ( pnm );
+#endif
 
 /*
  * Drag in all requested commands
index 2af521e2feadd6baf66f47032e6dd0c9a7bb4685..0b1196c5c22b3cb4ae9387b4cf0c003ba0956e3d 100644 (file)
@@ -103,6 +103,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 //#define      IMAGE_COMBOOT           /* SYSLINUX COMBOOT image support */
 //#define      IMAGE_EFI               /* EFI image support */
 //#define      IMAGE_SDI               /* SDI image support */
+//#define      IMAGE_PNM               /* PNM image support */
 
 /*
  * Command-line commands to include
diff --git a/src/image/pnm.c b/src/image/pnm.c
new file mode 100644 (file)
index 0000000..af9e571
--- /dev/null
@@ -0,0 +1,415 @@
+/*
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * Portable anymap format (PNM)
+ *
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <ctype.h>
+#include <ipxe/image.h>
+#include <ipxe/pixbuf.h>
+#include <ipxe/pnm.h>
+
+/**
+ * Extract PNM ASCII value
+ *
+ * @v image            PNM image
+ * @v pnm              PNM context
+ * @ret value          Value, or negative error
+ */
+static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
+       char buf[ pnm->ascii_len + 1 /* NUL */ ];
+       char *endp;
+       size_t len;
+       int value;
+       int in_comment = 0;
+
+       /* Skip any leading whitespace and comments */
+       for ( ; pnm->offset < image->len ; pnm->offset++ ) {
+               copy_from_user ( &buf[0], image->data, pnm->offset,
+                                sizeof ( buf[0] ) );
+               if ( in_comment ) {
+                       if ( buf[0] == '\n' )
+                               in_comment = 0;
+               } else {
+                       if ( buf[0] == '#' ) {
+                               in_comment = 1;
+                       } else if ( ! isspace ( buf[0] ) ) {
+                               break;
+                       }
+               }
+       }
+
+       /* Fail if no value is present */
+       len = ( image->len - pnm->offset );
+       if ( len == 0 ) {
+               DBGC ( image, "PNM %s ran out of ASCII data\n", image->name );
+               return -EINVAL;
+       }
+
+       /* Copy ASCII value to buffer and ensure string is NUL-terminated */
+       if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
+               len = ( sizeof ( buf ) - 1 /* NUL */ );
+       copy_from_user ( buf, image->data, pnm->offset, len );
+       buf[len] = '\0';
+
+       /* Parse value and update offset */
+       value = strtoul ( buf, &endp, 0 );
+       pnm->offset += ( endp - buf );
+
+       /* Check and skip terminating whitespace character, if present */
+       if ( ( pnm->offset != image->len ) && ( *endp != '\0' ) ) {
+               if ( ! isspace ( *endp ) ) {
+                       DBGC ( image, "PNM %s invalid ASCII integer\n",
+                              image->name );
+                       return -EINVAL;
+               }
+               pnm->offset++;
+       }
+
+       return value;
+}
+
+/**
+ * Extract PNM binary value
+ *
+ * @v image            PNM image
+ * @v pnm              PNM context
+ * @ret value          Value, or negative error
+ */
+static int pnm_binary ( struct image *image, struct pnm_context *pnm ) {
+       uint8_t value;
+
+       /* Sanity check */
+       if ( pnm->offset == image->len ) {
+               DBGC ( image, "PNM %s ran out of binary data\n",
+                      image->name );
+               return -EINVAL;
+       }
+
+       /* Extract value */
+       copy_from_user ( &value, image->data, pnm->offset, sizeof ( value ) );
+       pnm->offset++;
+
+       return value;
+}
+
+/**
+ * Scale PNM scalar value
+ *
+ * @v image            PNM image
+ * @v pnm              PNM context
+ * @v value            Raw value
+ * @ret value          Scaled value (in range 0-255)
+ */
+static int pnm_scale ( struct image *image, struct pnm_context *pnm,
+                      unsigned int value ) {
+
+       if ( value > pnm->max ) {
+               DBGC ( image, "PNM %s has out-of-range value %d (max %d)\n",
+                      image->name, value, pnm->max );
+               return -EINVAL;
+       }
+       return ( ( 255 * value ) / pnm->max );
+}
+
+/**
+ * Convert PNM bitmap composite value to RGB
+ *
+ * @v composite                Composite value
+ * @v index            Pixel index within this composite value
+ * @ret rgb            24-bit RGB value
+ */
+static uint32_t pnm_bitmap ( uint32_t composite, unsigned int index ) {
+
+       /* Composite value is an 8-bit bitmask */
+       return ( ( ( composite << index ) & 0x80 ) ? 0x000000 : 0xffffff );
+}
+
+/**
+ * Convert PNM greymap composite value to RGB
+ *
+ * @v composite                Composite value
+ * @v index            Pixel index within this composite value
+ * @ret rgb            24-bit RGB value
+ */
+static uint32_t pnm_greymap ( uint32_t composite, unsigned int index __unused ){
+
+       /* Composite value is an 8-bit greyscale value */
+       return ( ( composite << 16 ) | ( composite << 8 ) | composite );
+}
+
+/**
+ * Convert PNM pixmap composite value to RGB
+ *
+ * @v composite                Composite value
+ * @v index            Pixel index within this composite value
+ * @ret rgb            24-bit RGB value
+ */
+static uint32_t pnm_pixmap ( uint32_t composite, unsigned int index __unused ) {
+
+       /* Composite value is already an RGB value */
+       return composite;
+}
+
+/**
+ * Extract PNM pixel data
+ *
+ * @v image            PNM image
+ * @v pnm              PNM context
+ * @v pixbuf           Pixel buffer
+ * @ret rc             Return status code
+ */
+static int pnm_data ( struct image *image, struct pnm_context *pnm,
+                     struct pixel_buffer *pixbuf ) {
+       struct pnm_type *type = pnm->type;
+       size_t offset = 0;
+       unsigned int xpos = 0;
+       int scalar;
+       uint32_t composite;
+       uint32_t rgb;
+       unsigned int i;
+
+       /* Fill pixel buffer */
+       while ( offset < pixbuf->len ) {
+
+               /* Extract a scaled composite scalar value from the file */
+               composite = 0;
+               for ( i = 0 ; i < type->depth ; i++ ) {
+                       scalar = type->scalar ( image, pnm );
+                       if ( scalar < 0 )
+                               return scalar;
+                       scalar = pnm_scale ( image, pnm, scalar );
+                       if ( scalar < 0 )
+                               return scalar;
+                       composite = ( ( composite << 8 ) | scalar );
+               }
+
+               /* Extract 24-bit RGB values from composite value */
+               for ( i = 0 ; i < type->packing ; i++ ) {
+                       if ( offset >= pixbuf->len ) {
+                               DBGC ( image, "PNM %s has too many pixels\n",
+                                      image->name );
+                               return -EINVAL;
+                       }
+                       rgb = type->rgb ( composite, i );
+                       copy_to_user ( pixbuf->data, offset, &rgb,
+                                      sizeof ( rgb ) );
+                       offset += sizeof ( rgb );
+                       if ( ++xpos == pixbuf->width ) {
+                               xpos = 0;
+                               break;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+/** PNM image types */
+static struct pnm_type pnm_types[] = {
+       {
+               .type = '1',
+               .depth = 1,
+               .packing = 1,
+               .flags = PNM_BITMAP,
+               .scalar = pnm_ascii,
+               .rgb = pnm_bitmap,
+       },
+       {
+               .type = '2',
+               .depth = 1,
+               .packing = 1,
+               .scalar = pnm_ascii,
+               .rgb = pnm_greymap,
+       },
+       {
+               .type = '3',
+               .depth = 3,
+               .packing = 1,
+               .scalar = pnm_ascii,
+               .rgb = pnm_pixmap,
+       },
+       {
+               .type = '4',
+               .depth = 1,
+               .packing = 8,
+               .flags = PNM_BITMAP,
+               .scalar = pnm_binary,
+               .rgb = pnm_bitmap,
+       },
+       {
+               .type = '5',
+               .depth = 1,
+               .packing = 1,
+               .scalar = pnm_binary,
+               .rgb = pnm_greymap,
+       },
+       {
+               .type = '6',
+               .depth = 3,
+               .packing = 1,
+               .scalar = pnm_binary,
+               .rgb = pnm_pixmap,
+       },
+};
+
+/**
+ * Determine PNM image type
+ *
+ * @v image            PNM image
+ * @ret type           PNM image type, or NULL if not found
+ */
+static struct pnm_type * pnm_type ( struct image *image ) {
+       struct pnm_signature signature;
+       struct pnm_type *type;
+       unsigned int i;
+
+       /* Extract signature */
+       assert ( image->len >= sizeof ( signature ) );
+       copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
+
+       /* Check for supported types */
+       for ( i = 0 ; i < ( sizeof ( pnm_types ) /
+                           sizeof ( pnm_types[0] ) ) ; i++ ) {
+               type = &pnm_types[i];
+               if ( type->type == signature.type )
+                       return type;
+       }
+       return NULL;
+}
+
+/**
+ * Convert PNM image to pixel buffer
+ *
+ * @v image            PNM image
+ * @v pixbuf           Pixel buffer to fill in
+ * @ret rc             Return status code
+ */
+static int pnm_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
+       struct pnm_context pnm;
+       int width;
+       int height;
+       int max;
+       int rc;
+
+       /* Initialise PNM context */
+       pnm.type = pnm_type ( image );
+       if ( ! pnm.type ) {
+               rc = -ENOTSUP;
+               goto err_type;
+       }
+       pnm.offset = sizeof ( struct pnm_signature );
+       pnm.ascii_len = PNM_ASCII_LEN;
+
+       /* Extract width */
+       if ( ( width = pnm_ascii ( image, &pnm ) ) < 0 ) {
+               rc = width;
+               goto err_width;
+       }
+
+       /* Extract height */
+       if ( ( height = pnm_ascii ( image, &pnm ) ) < 0 ) {
+               rc = height;
+               goto err_height;
+       }
+
+       /* Extract maximum scalar value, if not predefined */
+       if ( pnm.type->flags & PNM_BITMAP ) {
+               pnm.max = ( ( 1 << pnm.type->packing ) - 1 );
+               pnm.ascii_len = 1;
+       } else {
+               if ( ( max = pnm_ascii ( image, &pnm ) ) < 0 ) {
+                       rc = max;
+                       goto err_max;
+               }
+               pnm.max = max;
+       }
+       if ( pnm.max == 0 ) {
+               DBGC ( image, "PNM %s has invalid maximum value 0\n",
+                      image->name );
+               rc = -EINVAL;
+               goto err_max;
+       }
+       DBGC ( image, "PNM %s is type %c width %d height %d max %d\n",
+              image->name, pnm.type->type, width, height, pnm.max );
+
+       /* Allocate pixel buffer */
+       *pixbuf = alloc_pixbuf ( width, height );
+       if ( ! *pixbuf ) {
+               rc = -ENOMEM;
+               goto err_alloc_pixbuf;
+       }
+
+       /* Extract pixel data */
+       if ( ( rc = pnm_data ( image, &pnm, *pixbuf ) ) != 0 )
+               goto err_data;
+
+       return 0;
+
+ err_data:
+       pixbuf_put ( *pixbuf );
+ err_alloc_pixbuf:
+ err_max:
+ err_height:
+ err_width:
+ err_type:
+       return rc;
+}
+
+/**
+ * Probe PNM image
+ *
+ * @v image            PNM image
+ * @ret rc             Return status code
+ */
+static int pnm_probe ( struct image *image ) {
+       struct pnm_signature signature;
+
+       /* Sanity check */
+       if ( image->len < sizeof ( signature ) ) {
+               DBGC ( image, "PNM %s is too short\n", image->name );
+               return -ENOEXEC;
+       }
+
+       /* Check signature */
+       copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
+       if ( ! ( ( signature.magic == PNM_MAGIC ) &&
+                ( isdigit ( signature.type ) ) &&
+                ( isspace ( signature.space ) ) ) ) {
+               DBGC ( image, "PNM %s has invalid signature\n", image->name );
+               return -ENOEXEC;
+       }
+       DBGC ( image, "PNM %s is type %c\n", image->name, signature.type );
+
+       return 0;
+}
+
+/** PNM image type */
+struct image_type pnm_image_type __image_type ( PROBE_NORMAL ) = {
+       .name = "PNM",
+       .probe = pnm_probe,
+       .pixbuf = pnm_pixbuf,
+};
index 8e5e30f0a58a4b3df3b97025813031ae68c87c70..4d10ec9811de28fb1483a764f06926dd2a274a8f 100644 (file)
@@ -227,6 +227,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_segment                      ( ERRFILE_IMAGE | 0x00030000 )
 #define ERRFILE_efi_image            ( ERRFILE_IMAGE | 0x00040000 )
 #define ERRFILE_embedded             ( ERRFILE_IMAGE | 0x00050000 )
+#define ERRFILE_pnm                  ( ERRFILE_IMAGE | 0x00060000 )
 
 #define ERRFILE_asn1                 ( ERRFILE_OTHER | 0x00000000 )
 #define ERRFILE_chap                 ( ERRFILE_OTHER | 0x00010000 )
diff --git a/src/include/ipxe/pnm.h b/src/include/ipxe/pnm.h
new file mode 100644 (file)
index 0000000..536c14d
--- /dev/null
@@ -0,0 +1,86 @@
+#ifndef _IPXE_PNM_H
+#define _IPXE_PNM_H
+
+/** @file
+ *
+ * Portable anymap format (PNM)
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <stdint.h>
+#include <ipxe/uaccess.h>
+#include <ipxe/image.h>
+
+/** PNM signature */
+struct pnm_signature {
+       /** Magic byte ('P') */
+       char magic;
+       /** PNM type */
+       char type;
+       /** Whitespace */
+       char space;
+} __attribute__ (( packed ));
+
+/** PNM magic byte */
+#define PNM_MAGIC 'P'
+
+/** PNM context */
+struct pnm_context {
+       /** PNM type */
+       struct pnm_type *type;
+       /** Current byte offset */
+       size_t offset;
+       /** Maximum length of ASCII values */
+       size_t ascii_len;
+       /** Maximum pixel value */
+       unsigned int max;
+};
+
+/** Default maximum length of ASCII values */
+#define PNM_ASCII_LEN 16
+
+/** PNM type */
+struct pnm_type {
+       /** PNM type */
+       char type;
+       /** Number of scalar values per pixel */
+       uint8_t depth;
+       /** Number of pixels per composite value */
+       uint8_t packing;
+       /** Flags */
+       uint8_t flags;
+       /** Extract scalar value
+        *
+        * @v image             PNM image
+        * @v pnm               PNM context
+        * @ret value           Value, or negative error
+        */
+       int ( * scalar ) ( struct image *image, struct pnm_context *pnm );
+       /** Convert composite value to 24-bit RGB
+        *
+        * @v composite         Composite value
+        * @v index             Pixel index within this composite value
+        * @ret rgb             24-bit RGB value
+        */
+       uint32_t ( * rgb ) ( uint32_t composite, unsigned int index );
+};
+
+/** PNM flags */
+enum pnm_flags {
+       /** Bitmap format
+        *
+        * If set, this flag indicates that:
+        *
+        * - the maximum scalar value is predefined as being equal to
+        *   (2^packing-1), and is not present within the file, and
+        *
+        * - the maximum length of ASCII values is 1.
+        */
+       PNM_BITMAP = 0x01,
+};
+
+extern struct image_type pnm_image_type __image_type ( PROBE_NORMAL );
+
+#endif /* _IPXE_PNM_H */
diff --git a/src/tests/pnm_test.c b/src/tests/pnm_test.c
new file mode 100644 (file)
index 0000000..a7b6752
--- /dev/null
@@ -0,0 +1,296 @@
+/*
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * PNM self-tests
+ *
+ */
+
+/* Forcibly enable assertions */
+#undef NDEBUG
+
+#include <string.h>
+#include <assert.h>
+#include <ipxe/pixbuf.h>
+#include <ipxe/pnm.h>
+#include <ipxe/test.h>
+
+/** Define inline pixel data */
+#define DATA(...) { __VA_ARGS__ }
+
+/** A PNM test */
+struct pnm_test {
+       /** Source image */
+       struct image *image;
+       /** Pixel data */
+       const uint32_t *data;
+       /** Length of pixel data */
+       size_t len;
+       /** Width */
+       unsigned int width;
+       /** Height */
+       unsigned int height;
+};
+
+/** Define a PNM test */
+#define PNM( NAME, FILE, WIDTH, HEIGHT, DATA )                         \
+       static const char NAME ## _file[] = FILE;                       \
+       static const uint32_t NAME ## _data[] = DATA;                   \
+       static struct image NAME ## _image = {                          \
+               .refcnt = REF_INIT ( ref_no_free ),                     \
+               .name = #NAME,                                          \
+               .data = ( userptr_t ) ( NAME ## _file ),                \
+               .len = sizeof ( NAME ## _file ),                        \
+       };                                                              \
+       static struct pnm_test NAME = {                                 \
+               .image = & NAME ## _image,                              \
+               .data = NAME ## _data,                                  \
+               .len = sizeof ( NAME ## _data ),                        \
+               .width = WIDTH,                                         \
+               .height = HEIGHT,                                       \
+       };
+
+/** PBM ASCII example (from Wikipedia) */
+PNM ( pbm_ascii,
+      "P1\n"
+      "# This is an example bitmap of the letter \"J\"\n"
+      "6 10\n"
+      "0 0 0 0 1 0\n"
+      "0 0 0 0 1 0\n"
+      "0 0 0 0 1 0\n"
+      "0 0 0 0 1 0\n"
+      "0 0 0 0 1 0\n"
+      "0 0 0 0 1 0\n"
+      "1 0 0 0 1 0\n"
+      "0 1 1 1 0 0\n"
+      "0 0 0 0 0 0\n"
+      "0 0 0 0 0 0\n",
+      6, 10,
+      DATA ( 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0x000000, 0x000000, 0x000000, 0xffffff, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
+
+/** PGM ASCII example (from Wikipedia) */
+PNM ( pgm_ascii,
+      "P2\n"
+      "# Shows the word \"FEEP\" (example from Netpbm man page on PGM)\n"
+      "24 7\n"
+      "15\n"
+      "0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n"
+      "0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0\n"
+      "0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0\n"
+      "0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0\n"
+      "0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0\n"
+      "0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0\n"
+      "0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n"
+      , 24, 7,
+      DATA ( 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x333333, 0x333333, 0x333333, 0x333333, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x333333, 0x333333, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
+
+/** PPM ASCII example (from Wikipedia) */
+PNM ( ppm_ascii,
+      "P3\n"
+      "# The P3 means colors are in ASCII, then 3 columns and 2 rows,\n"
+      "# then 255 for max color, then RGB triplets\n"
+      "3 2\n"
+      "255\n"
+      "255   0   0     0 255   0     0   0 255\n"
+      "255 255   0   255 255 255     0   0   0\n",
+      3, 2,
+      DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
+
+/** PBM ASCII with no space between pixel values */
+PNM ( pbm_ascii_no_space,
+      "P1\n"
+      "3 3\n"
+      "001\n"
+      "010\n"
+      "111\n",
+      3, 3,
+      DATA ( 0xffffff, 0xffffff, 0x000000, 0xffffff, 0x000000, 0xffffff,
+            0x000000, 0x000000, 0x000000 ) );
+
+/** PBM binary example (converted from Wikipedia) */
+PNM ( pbm_binary,
+      DATA ( 0x50, 0x34, 0x0a, 0x23, 0x20, 0x43, 0x52, 0x45, 0x41, 0x54, 0x4f,
+            0x52, 0x3a, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x20, 0x50, 0x4e, 0x4d,
+            0x20, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x20, 0x56, 0x65, 0x72,
+            0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x2e, 0x31, 0x0a, 0x36, 0x20,
+            0x31, 0x30, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x88, 0x70,
+            0x00, 0x00 ),
+      6, 10,
+      DATA ( 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0x000000, 0xffffff,
+            0xffffff, 0x000000, 0x000000, 0x000000, 0xffffff, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
+            0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
+
+/** PGM binary example (converted from Wikipedia) */
+PNM ( pgm_binary,
+      DATA ( 0x50, 0x35, 0x0a, 0x32, 0x34, 0x20, 0x37, 0x0a, 0x31, 0x35, 0x0a,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x07, 0x07,
+            0x07, 0x07, 0x00, 0x00, 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x00, 0x0f,
+            0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x03, 0x03, 0x03, 0x00,
+            0x00, 0x00, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x0b, 0x0b, 0x0b,
+            0x00, 0x00, 0x00, 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x03, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x07, 0x07, 0x00,
+            0x00, 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00 ),
+      24, 7,
+      DATA ( 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x333333, 0x333333, 0x333333, 0x333333, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x333333, 0x333333, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xbbbbbb, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x333333, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x777777, 0x777777, 0x777777, 0x777777, 0x000000,
+            0x000000, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0xbbbbbb, 0x000000,
+            0x000000, 0xffffff, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
+            0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
+
+/** PPM binary example (converted from Wikipedia) */
+PNM ( ppm_binary,
+      DATA ( 0x50, 0x36, 0x0a, 0x33, 0x20, 0x32, 0x0a, 0x32, 0x35, 0x35, 0x0a,
+            0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
+            0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 ),
+      3, 2,
+      DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
+
+/**
+ * Report PNM test result
+ *
+ * @v test             PNM test
+ */
+#define pnm_ok( test ) do {                                            \
+       struct pixel_buffer *pixbuf;                                    \
+       uint8_t data[ (test)->len ];                                    \
+       int rc;                                                         \
+                                                                       \
+       /* Sanity check */                                              \
+       assert ( ( (test)->width * (test)->height *                     \
+                  sizeof ( (test)->data[0] ) ) == (test)->len );       \
+                                                                       \
+       /* Correct image data pointer */                                \
+       (test)->image->data =                                           \
+               virt_to_user ( ( void * ) (test)->image->data );        \
+                                                                       \
+       /* Perform tests */                                             \
+       ok ( image_probe ( (test)->image ) == 0 );                      \
+       ok ( (test)->image->type == &pnm_image_type );                  \
+       ok ( ( rc = image_pixbuf ( (test)->image, &pixbuf ) ) == 0 );   \
+       if ( rc == 0 ) {                                                \
+               ok ( pixbuf->width == (test)->width );                  \
+               ok ( pixbuf->height == (test)->height );                \
+               ok ( pixbuf->len == (test)->len );                      \
+               copy_from_user ( data, pixbuf->data, 0,                 \
+                                sizeof ( data ) );                     \
+               ok ( memcmp ( data, (test)->data,                       \
+                             sizeof ( data ) ) == 0 );                 \
+               DBGC_HDA ( (test)->image, 0, data, sizeof ( data ) );   \
+               pixbuf_put ( pixbuf );                                  \
+       }                                                               \
+       } while ( 0 )
+
+/**
+ * Perform PNM self-test
+ *
+ */
+static void pnm_test_exec ( void ) {
+
+       pnm_ok ( &pbm_ascii );
+       pnm_ok ( &pgm_ascii );
+       pnm_ok ( &ppm_ascii );
+       pnm_ok ( &pbm_ascii_no_space );
+       pnm_ok ( &pbm_binary );
+       pnm_ok ( &pgm_binary );
+       pnm_ok ( &ppm_binary );
+}
+
+/** PNM self-test */
+struct self_test pnm_test __self_test = {
+       .name = "pnm",
+       .exec = pnm_test_exec,
+};
index 17e22a3a56acdd16167bd354da0398a0ff32b67d..6224a3dcda8a6a8168217dcd49f7d8e110373104 100644 (file)
@@ -49,3 +49,4 @@ REQUIRE_OBJECT ( rsa_test );
 REQUIRE_OBJECT ( x509_test );
 REQUIRE_OBJECT ( ocsp_test );
 REQUIRE_OBJECT ( cms_test );
+REQUIRE_OBJECT ( pnm_test );