Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- FUNCTIONS=0
- increment() { FUNCTIONS=$((FUNCTIONS+1)); }
- die() { echo "$*" >&2; exit 2; } # Call a usage function from here as well.
- while getopts abcdhw:i:z:o: opt
- do
- case $opt in
- a) CASE_A=1; increment;;
- b) CASE_B=1; increment;;
- c) CASE_C=1; increment;;
- d) CASE_D=1; increment;;
- h) CASE_H=1;;
- w) WIDTH="$OPTARG";; # Width of image (for -b)
- i) IMAGE="$OPTARG";; # Image file path (for -a and -c)
- z) ARCHIVE="$OPTARG";; # Archive/PNG file path i.e. input
- o) OUTPUT="$OPTARG";; # Output PNG/Archive file path
- ?) die "Error: incorrect flag was given";;
- esac
- done
- # Note: I will use -eq and -gt to replace = and > for integers
- # Note: Integers in formulas does not require doiuble quotes
- # Note: -z is for checking if a variable is zero or not
- # Note: -r is for checking if a file or directory exists
- if [ ${CASE_H} -eq 1 ]; then
- echo "This is the Cornelia Archive Embedding Program (CAEP)"
- echo "It takes an archive (bzip2/gzip/xzip/lzip/lzop/lzma/7zip/zip/rar)"
- echo "and an image if using -a or -c (tif/tiff/jpe/jpg/jpeg/gif/bmp/png)"
- echo "or the width of the photo as specified for -b to create a PNG file"
- echo "Or you can use -d to convert PNG file to a usable BMP format"
- echo "To use this, enter in either of these commands"
- echo "A. Append Space Format (Archive data under iamge)"
- echo "Cornelia -a -i IMAGE -z ARCHIVE -o PNG_OUT"
- echo "B. Blank canvas Format (Archive directly as image)"
- echo "Cornelia -b -w WIDTH -z ARCHIVE -o PNG_OUT"
- echo "C. Cornlia Old Format (Archive overwrite image bottom)"
- echo "Cornelia -c -i IMAGE -z ARCHIVE -o PNG_OUT"
- echo "D. Decompressing PNG (Decompress PNG image to BMP)"
- echo "Cornelia -d -z PNG -o ARCHIVE_OUT"
- echo "H. Help Manual for Cornelia (explaining what this program does)"
- echo "Cornelia -h"
- exit
- elif [ ${FUNCTIONS} -eq 0 ]; then
- die "Error: need a function flag"
- elif [ ${FUNCTIONS} -gt 1 ]; then
- die "Error: Multiple function flags were given"
- elif [ ${CASE_B} -eq 1 ] != ; then
- if [ -z "${WIDTH}" ]; then
- die "Error: Width not given for -b mode"
- elif [ "${WIDTH}" -gt 0 ]; then
- die "Error: Width is not a positive integer"
- fi
- elif [ $((CASE_A+CASE_C)) -eq 1 ]; then
- shopt -s extglob
- if [ -z "${IMAGE}" ]; then
- die "Error: Image file path not given for -a or -c mode"
- elif [ ! -r "${IMAGE}" ]; then
- die "Error: Image file path '${IMAGE}' is not readable"
- elif [[ ${IMAGE} != *.@(tif?f|jp([eg]|eg)|gif|bmp|png) ]]; then
- die "Error: Image file path '${IMAGE}' is not a valid input"
- fi
- elif [ ${CASE_D} -eq 1 ]; then # Decompression
- if [ -z "${ARCHIVE}" ]; then
- die "Error: PNG file path not given for -d mode"
- elif [ ! -r "${ARCHIVE}" ]; then
- die "Error: PNG file path '${ARCHIVE}' is not readable"
- elif [[ ${ARCHIVE} != *.png ]]; then # does not function
- die "Error: PNG file path '${OUTPUT}' does not end in .png"
- elif [ -z "${OUTPUT}" ]; then
- die "Error: Output BMP file path not given for -d mode"
- elif [ ! -r "$(dirname "${OUTPUT}")" ]; then
- die "Error: Output BMP file path '${OUTPUT}' does not exist"
- elif [[ ${OUTPUT} != *.bmp ]]; then # does not function
- die "Error: Output BMP file path '${OUTPUT}' does not end in .bmp"
- fi
- else # Compression
- shopt -s extglob
- if [ -z "${ARCHIVE}" ]; then
- die "Error: Archive file path not given for -a, -b or -c mode"
- elif [ ! -r "${ARCHIVE}" ]; then
- die "Error: Archive file path '${ARCHIVE}' is not readable"
- elif [[ ${ARCHIVE} != *.@(t?(bz2|[gl]z)|[7x]z|lzma|rar|[lt]zo|zip) ]]; then # does not function
- die "Error: Archive file path '${ARCHIVE}' is not a valid input"
- elif [ -z "${OUTPUT}" ]; then
- die "Error: Output PNG file path not given for -a, -b or -c mode"
- elif [ ! -r "$(dirname "$OUTPUT")" ]; then
- die "Error: Output PNG file path '${OUTPUT}' does not exist"
- elif [[ ${OUTPUT} != *.png ]]; then # does not function
- die "Error: Output PNG file path '${OUTPUT}' does not end in .png"
- fi
- fi
- # width_check checks the width of the image
- # height_check checks the height needed for the image
- # image_gen1 generates a blank image based on width and height
- # image_gen2 converts image to BMP
- # Note: I will not use identify, magick or mogrify from Imagemagick
- # Note: I will always add a space between last colon and curly brackets
- width_check() { WIDTH=$(convert -print "%w" "${IMAGE}" /dev/null); }
- height_check() { HEIGHT=$(((SIZE+4*WIDTH-1)/(4*WIDTH))); }
- image_gen1() { convert -size "${WIDTH}"x"${HEIGHT}" xc:transparent cornelia_gen/blank.bmp; }
- image_gen2() { convert "${IMAGE}" -type truecolormatte cornelia_gen/tmp1.bmp; }
- append_format() { width_check; height_check; image_gen1; image_gen2;
- convert -append cornelia_gen/tmp1.bmp cornelia_gen/blank.bmp cornelia_gen/tmp1.bmp; }
- blank_format() { height_check; image_gen1; mv cornelia_gen/blank.bmp cornelia_gen/tmp1.bmp; }
- cornelia_format() { width_check; height_check;
- if [ "$(convert -print "%h" "${IMAGE}" /dev/null)" -gt "${HEIGHT}" ]; then image_gen2
- else echo "Error: Image is too small for archive"; exit 1
- fi; }
- convert_image() {
- head -c 138 cornelia_gen/tmp1.bmp > cornelia_gen/tmp2 # takes the header
- cat "${ARCHIVE}" >> cornelia_gen/tmp2 # concatenate archive to image
- dd if=cornelia_gen/tmp2 of=cornelia_gen/tmp1.bmp conv=notrunc # overwrite bmp
- convert cornelia_gen/tmp1.bmp "${OUTPUT}"; } # output image as png
- if [ ${CASE_D} -eq 1 ]; then convert "${ARCHIVE}" -type truecolormatte "${OUTPUT}" # decompress png
- else # creates png file
- mkdir cornelia_gen; SIZE=$(stat -c %s "${ARCHIVE}")
- if [ ${CASE_A} -eq 1 ]; then append_format
- elif [ ${CASE_B} -eq 1 ]; then blank_format
- elif [ ${CASE_C} -eq 1 ]; then cornelia_format
- fi; convert_image; rm -r cornelia_gen # deletes temporary file
- fi; exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement