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

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