Skip to content

Latest commit

 

History

History

ConvertBitmapToSourceCode

NAME

Convert Bitmap to Source Code 2.02

USAGE

perl ConvertBitmapToSourceCode.pl --output-format fmt [--invert-color] [--invert-bit-order] input.ppm output.h

The input file must be in Portable Pixmap format (PPM), in text format (the type is then P3). This format that can be generated by GIMP or by the ImageMagick tools.

The only output format currently supported is the C/C++ array syntax.

The main reason this tool exists is that ImageMagick does not currently support the RGB565 format (as of May 2011), but this is a very popular display format among small hardware devices.

OPTIONS

  • -h, --help

    Prints this help text.

  • --output-format fmt

    Available output formats are: monochrome, rgb565

    For the monochrome format, the input pixels must be completely white or completely black, as this tool will not attempt to guess what colours or color shades should be made white or black.

  • --license

    Prints the license.

  • --invert-color

    For the monochrome format, swap the black and white colours.

  • --invert-bit-order

    For the monochrome format, invert the bit order at each byte.

PREREQUISITES

Install the Image::PBMlib Perl module in advance. No other graphic or bitmap libraries are needed, that's the reason why this tool insists on the PPM text format for the input bitmap data.

If you are using ActiveState's Perl on Windows, you can use the PPM tool to manually install that module, or run the PPM tool like this for automatic installation:

ppm install Image::PBMlib

If you are using some other Perl version or running on some other operating system, you can install that module manually with CPAN, or run the following command for automatic installation:

cpan -i Image::PBMlib

Alternatively, manually download file PBMlib.pm and place it next to this script, in a subdirectory called "Image". If you call this tool from another directory, make sure that the "Image" subdirectory is listed in the Perl module include path, see perl's command-line switch -I for more information.

USAGE EXAMPLE

Say you have a MyBitmap.png file that you wish to convert to C source code in RGB565 format. The output file should be called MyBitmap.h, and there will be an intermediate file called MyBitmap.ppm . The following bash script does the trick. Note that you need to install ImageMagick's 'convert' tool in advance.

#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail

# This 'convert' tool comes with the ImageMagick library, you'll have to install it beforehand.
CONV_TOOL="convert"

CONV_BM_SCRIPT="/some/path/ConvertBitmapToSourceCode.pl"

FILENAME="MyBitmap.png"
"$CONV_TOOL" "$FILENAME" -format PPM -compress none "${FILENAME%.*}.ppm"
"$CONV_BM_SCRIPT" --output-format rgb565 "${FILENAME%.*}.ppm" "${FILENAME%.*}.h"

EXIT CODE

Exit code: 0 on success, some other value on error.

TO-DO LIST

This tool could read and write many other image and source formats. Help is always welcome.

VERSION HISTORY

  • Version 2.02: All Perl scripts use Getopt::Long: option 'no_ignore_case' now.

  • Version 2.01 has just a tiny change in an error message.

  • Version 2.00, released in September 2014.

    The author of Image::PBMlib has broken interface compatibility in version 2.00, so I had to update this script as a result.

  • Version 1.03, released in August 2011.

    Added options --invert-color and --invert-bit-order. Minor fixes.

  • Version 1.00, released in May 2011.

FEEDBACK

Please send feedback to rdiezmail-tools at yahoo.de

LICENSE

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.

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 Affero General Public License version 3 for more details.

You should have received a copy of the GNU Affero General Public License version 3 along with this program. If not, see http://www.gnu.org/licenses/.