Blame tcar-scripts-render/modules/Files/Modules/Palette/palette.sh

Alain Reguera Delgado 2c7b25
#!/bin/bash
Alain Reguera Delgado 2c7b25
######################################################################
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
#   palette.sh -- This function standardizes the way palette of colors
Alain Reguera Delgado 2c7b25
#   are applied to image files in order to produce images with
Alain Reguera Delgado 2c7b25
#   specific number of colors.
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
#   Written by:
Alain Reguera Delgado 2c7b25
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 2c7b25
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 2c7b25
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 2c7b25
# your option) any later version.
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 2c7b25
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 2c7b25
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 2c7b25
# General Public License for more details.
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 2c7b25
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 2c7b25
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 2c7b25
#
Alain Reguera Delgado 2c7b25
######################################################################
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
function palette {
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    local FILENAME=$(tcar_getTemporalFile "$(echo ${SOURCES[0]} \
Alain Reguera Delgado 2c7b25
        | sed -r 's/\.[[:alpha:]]+$//')")
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    local LOGS=${RENDER_TARGET}.log
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Define absolute path to GPL palette. This file is the reference
Alain Reguera Delgado 2c7b25
    # taken to set the max number of colors the final image will be
Alain Reguera Delgado 2c7b25
    # produced for.
Alain Reguera Delgado 2c7b25
    local PALETTE_GPL=$(tcar_getConfigValue ${CONFIGURATION} ${SECTION} 'palette-gpl')
Alain Reguera Delgado 2c7b25
    tcar_checkFiles -ef ${PALETTE_GPL}
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    local PALETTE_GPL_COLORS=$(palette_getColors "${PALETTE_GPL}")
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Define absolute path to PPM palette. The PPM palette is built
Alain Reguera Delgado 2c7b25
    # from source palette (PALETTE_GPL) and provides the color
Alain Reguera Delgado 2c7b25
    # information understood by `ppmremap', the program used to
Alain Reguera Delgado 2c7b25
    # produce images in a specific amount of colors.
Alain Reguera Delgado 2c7b25
    local PALETTE_PPM=$(tcar_getTemporalFile 'palette.ppm')
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Verify format of colors returned in the list.
Alain Reguera Delgado 2c7b25
    palette_checkColorFormats "${PALETTE_GPL_COLORS}"
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Create image in Netpbm superformat (PNM). The PNM image file is
Alain Reguera Delgado 2c7b25
    # created from the PNG image rendered previously as centos-art
Alain Reguera Delgado 2c7b25
    # base-rendition output. The PNM image is an intermediate format
Alain Reguera Delgado 2c7b25
    # used to manipulate images through Netpbm tools.
Alain Reguera Delgado 2c7b25
    pngtopnm -verbose \
Alain Reguera Delgado 2c7b25
        < ${SOURCES[0]} 2>${LOGS} > ${FILENAME}.pnm
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Create PPM palette using GPL palette.
Alain Reguera Delgado 2c7b25
    palette_convertGplToPpm
Alain Reguera Delgado 2c7b25
    
Alain Reguera Delgado 2c7b25
    # Reduce colors as specified in PPM palette.  Here we use the PPM
Alain Reguera Delgado 2c7b25
    # palette to enforce the color position in the image index and the
Alain Reguera Delgado 2c7b25
    # Floyd-Steinberg dithering in order to improve color reduction.
Alain Reguera Delgado 2c7b25
    pnmremap -verbose -mapfile=${PALETTE_PPM} -floyd \
Alain Reguera Delgado 2c7b25
        < ${FILENAME}.pnm 2>>${LOGS} > ${FILENAME}.ppm
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    # Print action message.
Alain Reguera Delgado 2c7b25
    tcar_printMessage "${RENDER_TARGET}" --as-creating-line
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    if [[ ${RENDER_TARGET} =~ '\.lss$' ]];then
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
        # Define the HEX palette. The HEX palette is built from source
Alain Reguera Delgado 2c7b25
        # palette (PALETTE_GPL) and provides the color information in
Alain Reguera Delgado 2c7b25
        # the format understood by `ppmtolss16', the program used to
Alain Reguera Delgado 2c7b25
        # produce images in LSS16 format.  The HEX palette stores just
Alain Reguera Delgado 2c7b25
        # one line with the color information as described in isolinux
Alain Reguera Delgado 2c7b25
        # documentation (i.e #RRGGBB=0 #RRGGBB=1 ... [all values in
Alain Reguera Delgado 2c7b25
        # the same line])
Alain Reguera Delgado 2c7b25
        local PALETTE_HEX=$(tcar_getTemporalFile "palette.hex")
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
        # Create HEX palette using GPL palette.
Alain Reguera Delgado 2c7b25
        palette_convertGplToHex
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
        # Create LSS16 image. 
Alain Reguera Delgado 2c7b25
        ppmtolss16 $(cat ${PALETTE_HEX}) \
Alain Reguera Delgado 2c7b25
            < ${FILENAME}.ppm 2>>${LOGS} > ${RENDER_TARGET}
Alain Reguera Delgado 2c7b25
     
Alain Reguera Delgado 2c7b25
        # Create PPM image indexed to 16 colors. Also the colormap
Alain Reguera Delgado 2c7b25
        # used in the LSS16 image is saved on ${FILE}.log; this is
Alain Reguera Delgado 2c7b25
        # useful to verify the correct order of colors in the image
Alain Reguera Delgado 2c7b25
        # index.
Alain Reguera Delgado 2c7b25
        lss16toppm -map \
Alain Reguera Delgado 2c7b25
            < ${RENDER_TARGET} 2>>${LOGS} > ${RENDER_TARGET}.ppm
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    else
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
        # Create final file.
Alain Reguera Delgado 2c7b25
        /usr/bin/convert ${FILENAME}.ppm ${RENDER_TARGET}
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
    fi
Alain Reguera Delgado 2c7b25
Alain Reguera Delgado 2c7b25
}