Blame Functions/Render/render_convertGplToPpm.sh

913568
#!/bin/bash
913568
#
913568
# render_convertGplToPpm.sh -- This function takes one palette
913568
# produced by Gimp (e.g., syslinux.gpl) as input and outputs one PPM
913568
# file based on it (e.g., syslinux.ppm).
913568
#
913568
# Copyright 2009-2011 Alain Reguera Delgado
913568
#
913568
# This program is free software; you can redistribute it and/or
913568
# modify it under the terms of the GNU General Public License as
913568
# published by the Free Software Foundation; either version 2 of the
913568
# License, or (at your option) any later version.
913568
# 
913568
# This program is distributed in the hope that it will be useful, but
913568
# WITHOUT ANY WARRANTY; without even the implied warranty of
913568
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
913568
# General Public License for more details.
913568
#
913568
# You should have received a copy of the GNU General Public License
913568
# along with this program; if not, write to the Free Software
913568
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
913568
# USA.
913568
# 
913568
# ----------------------------------------------------------------------
913568
# $Id$
913568
# ----------------------------------------------------------------------
913568
913568
function render_convertGplToPpm {
913568
913568
    local COLOR=''
913568
    local COUNT=0
913568
    local -a FILES
913568
913568
    # Define path to GPL palette. This is the .gpl file we use to
913568
    # retrive color information from.
913568
    local PALETTE_GPL="$1"
913568
913568
    # Define path to PPM palette. This is the .ppm file we'll save
913568
    # color information to.
913568
    local PALETTE_PPM="$2"
913568
913568
    # Define the number of colors this function should return.
913568
    local COLOR_NUMBER="$3"
913568
913568
    # Verify the number of colors this function should return. As
913568
    # convenction, we are producing images in 14 and 16 colors only to
913568
    # cover Grub and Syslinux images need respectively.
913568
    if [[ ! $COLOR_NUMBER =~ '^(14|16)$' ]];then
913568
        cli_printMessage "`eval_gettext "Reducing image to \\\`\\\$COLOR_NUMBER' colors is not supported."`" 'AsErrorLine'
913568
        cli_printMessage "$(caller)" 'AsToKnowMoreLine'
913568
    fi
913568
913568
    # Define list of colors from GPL palette.
913568
    local COLORS=$(render_getColors "$PALETTE_GPL")
913568
913568
    # Verify number of colors returned in the list.
913568
    if [[ ! $(echo "$COLORS" |  wc -l) =~ $COLOR_NUMBER ]];then
913568
        cli_printMessage "`gettext "The palette doesn't have the correct number of colors."`" 'AsErrorLine'
913568
        cli_printMessage "$(caller)" 'AsToKnowMoreLine'
913568
    fi
913568
913568
    # Verify format of colors inside the list.
913568
    for COLOR in $COLORS;do
913568
        if [[ ! $COLOR =~ '^[0-9a-f]{6}$' ]];then
913568
            cli_printMessage "`eval_gettext "The \\\`\\\$COLOR' string isn't a valid color code."`" 'AsErrorLine'
913568
            cli_printMessage "$(caller)" 'AsToKnowMoreLine'
913568
        fi
913568
    done
913568
913568
    # Create temporal images (of 1x1 pixel each) for each color
913568
    # retrived from Gimp's palette.
913568
    for COLOR in $COLORS;do
913568
        FILES[$COUNT]=$(cli_getTemporalFile "color-${COUNT}.ppm")
913568
        ppmmake $(echo "$COLOR" \
913568
            | sed -r 's!(.{2})(.{2})(.{2})!rgb:\1/\2/\3!') 1 1 \
913568
            > ${FILES[$COUNT]}
913568
        COUNT=$(($COUNT + 1))
913568
    done
913568
913568
    # Concatenate temporal images from left to right to create the PPM
913568
    # file.
913568
    pnmcat -lr ${FILES[*]} > $PALETTE_PPM
913568
913568
    # Remove temporal images.
913568
    rm ${FILES[*]}
913568
913568
    # Verify PPM palette existence.
913568
    cli_checkFiles "$PALETTE_PPM" 'f'
913568
913568
}