Advertisement
Guest User

Cornelia Archive Embedding Program

a guest
Dec 19th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. FUNCTIONS=0
  4. increment() { FUNCTIONS=$((FUNCTIONS+1)); }
  5. die() { echo "$*" >&2;  exit 2; } # Call a usage function from here as well.
  6.  
  7. while getopts abcdhw:i:z:o: opt
  8. do
  9.     case $opt in
  10.         a) CASE_A=1; increment;;
  11.         b) CASE_B=1; increment;;
  12.         c) CASE_C=1; increment;;
  13.         d) CASE_D=1; increment;;
  14.         h) CASE_H=1;;
  15.         w) WIDTH="$OPTARG";; # Width of image (for -b)
  16.         i) IMAGE="$OPTARG";; # Image file path (for -a and -c)
  17.         z) ARCHIVE="$OPTARG";; # Archive/PNG file path i.e. input
  18.         o) OUTPUT="$OPTARG";; # Output PNG/Archive file path
  19.         ?) die "Error: incorrect flag was given";;
  20.     esac
  21. done
  22.  
  23. # Note: I will use -eq and -gt to replace = and > for integers
  24. # Note: Integers in formulas does not require doiuble quotes
  25. # Note: -z is for checking if a variable is zero or not
  26. # Note: -r is for checking if a file or directory exists
  27.  
  28. if [ ${CASE_H} -eq 1 ]; then
  29.     echo "This is the Cornelia Archive Embedding Program (CAEP)"
  30.     echo "It takes an archive (bzip2/gzip/xzip/lzip/lzop/lzma/7zip/zip/rar)"
  31.     echo "and an image if using -a or -c (tif/tiff/jpe/jpg/jpeg/gif/bmp/png)"
  32.     echo "or the width of the photo as specified for -b to create a PNG file"
  33.     echo "Or you can use -d to convert PNG file to a usable BMP format"
  34.     echo "To use this, enter in either of these commands"
  35.     echo "A. Append Space Format (Archive data under iamge)"
  36.     echo "Cornelia -a -i IMAGE -z ARCHIVE -o PNG_OUT"
  37.     echo "B. Blank canvas Format (Archive directly as image)"
  38.     echo "Cornelia -b -w WIDTH -z ARCHIVE -o PNG_OUT"
  39.     echo "C. Cornlia Old Format (Archive overwrite image bottom)"
  40.     echo "Cornelia -c -i IMAGE -z ARCHIVE -o PNG_OUT"
  41.     echo "D. Decompressing PNG (Decompress PNG image to BMP)"
  42.     echo "Cornelia -d -z PNG -o ARCHIVE_OUT"
  43.     echo "H. Help Manual for Cornelia (explaining what this program does)"
  44.     echo "Cornelia -h"
  45.     exit
  46. elif [ ${FUNCTIONS} -eq 0 ]; then
  47.     die "Error: need a function flag"
  48. elif [ ${FUNCTIONS} -gt 1 ]; then
  49.     die "Error: Multiple function flags were given"
  50. elif [ ${CASE_B} -eq 1 ] != ; then
  51.     if [ -z "${WIDTH}" ]; then
  52.         die "Error: Width not given for -b mode"
  53.     elif [ "${WIDTH}" -gt 0 ]; then
  54.         die "Error: Width is not a positive integer"
  55.     fi
  56. elif [ $((CASE_A+CASE_C)) -eq 1 ]; then
  57.     shopt -s extglob
  58.     if [ -z "${IMAGE}" ]; then
  59.         die "Error: Image file path not given for -a or -c mode"
  60.     elif [ ! -r "${IMAGE}" ]; then
  61.         die "Error: Image file path '${IMAGE}' is not readable"
  62.     elif [[ ${IMAGE} != *.@(tif?f|jp([eg]|eg)|gif|bmp|png) ]]; then
  63.         die "Error: Image file path '${IMAGE}' is not a valid input"
  64.     fi
  65. elif [ ${CASE_D} -eq 1 ]; then # Decompression
  66.     if [ -z "${ARCHIVE}" ]; then
  67.         die "Error: PNG file path not given for -d mode"
  68.     elif [ ! -r "${ARCHIVE}" ]; then
  69.         die "Error: PNG file path '${ARCHIVE}' is not readable"
  70.     elif [[ ${ARCHIVE} != *.png ]]; then # does not function
  71.         die "Error: PNG file path '${OUTPUT}' does not end in .png"
  72.     elif [ -z "${OUTPUT}" ]; then
  73.         die "Error: Output BMP file path not given for -d mode"
  74.     elif [ ! -r "$(dirname "${OUTPUT}")" ]; then
  75.         die "Error: Output BMP file path '${OUTPUT}' does not exist"
  76.     elif [[ ${OUTPUT} != *.bmp ]]; then # does not function
  77.         die "Error: Output BMP file path '${OUTPUT}' does not end in .bmp"
  78.     fi
  79. else # Compression
  80.     shopt -s extglob
  81.     if [ -z "${ARCHIVE}" ]; then
  82.         die "Error: Archive file path not given for -a, -b or -c mode"
  83.     elif [ ! -r "${ARCHIVE}" ]; then
  84.         die "Error: Archive file path '${ARCHIVE}' is not readable"
  85.     elif [[ ${ARCHIVE} != *.@(t?(bz2|[gl]z)|[7x]z|lzma|rar|[lt]zo|zip) ]]; then # does not function
  86.         die "Error: Archive file path '${ARCHIVE}' is not a valid input"
  87.     elif [ -z "${OUTPUT}" ]; then
  88.         die "Error: Output PNG file path not given for -a, -b or -c mode"
  89.     elif [ ! -r "$(dirname "$OUTPUT")" ]; then
  90.         die "Error: Output PNG file path '${OUTPUT}' does not exist"
  91.     elif [[ ${OUTPUT} != *.png ]]; then # does not function
  92.         die "Error: Output PNG file path '${OUTPUT}' does not end in .png"
  93.     fi
  94. fi
  95.  
  96. # width_check checks the width of the image
  97. # height_check checks the height needed for the image
  98. # image_gen1 generates a blank image based on width and height
  99. # image_gen2 converts image to BMP
  100. # Note: I will not use identify, magick or mogrify from Imagemagick
  101. # Note: I will always add a space between last colon and curly brackets
  102.  
  103. width_check() { WIDTH=$(convert -print "%w" "${IMAGE}" /dev/null); }
  104. height_check() { HEIGHT=$(((SIZE+4*WIDTH-1)/(4*WIDTH))); }
  105. image_gen1() { convert -size "${WIDTH}"x"${HEIGHT}" xc:transparent cornelia_gen/blank.bmp; }
  106. image_gen2() { convert "${IMAGE}" -type truecolormatte cornelia_gen/tmp1.bmp; }
  107. append_format() { width_check; height_check; image_gen1; image_gen2;
  108.     convert -append cornelia_gen/tmp1.bmp cornelia_gen/blank.bmp cornelia_gen/tmp1.bmp; }
  109. blank_format() { height_check; image_gen1; mv cornelia_gen/blank.bmp cornelia_gen/tmp1.bmp; }
  110. cornelia_format() { width_check; height_check;
  111.     if [ "$(convert -print "%h" "${IMAGE}" /dev/null)" -gt "${HEIGHT}" ]; then image_gen2
  112.     else echo "Error: Image is too small for archive"; exit 1
  113.     fi; }
  114. convert_image() {
  115.     head -c 138 cornelia_gen/tmp1.bmp > cornelia_gen/tmp2 # takes the header
  116.     cat "${ARCHIVE}" >> cornelia_gen/tmp2 # concatenate archive to image
  117.     dd if=cornelia_gen/tmp2 of=cornelia_gen/tmp1.bmp conv=notrunc # overwrite bmp
  118.     convert cornelia_gen/tmp1.bmp "${OUTPUT}"; } # output image as png
  119.  
  120. if [ ${CASE_D} -eq 1 ]; then convert "${ARCHIVE}" -type truecolormatte "${OUTPUT}" # decompress png
  121. else # creates png file
  122.     mkdir cornelia_gen; SIZE=$(stat -c %s "${ARCHIVE}")
  123.     if [ ${CASE_A} -eq 1 ]; then append_format
  124.     elif [ ${CASE_B} -eq 1 ]; then blank_format
  125.     elif [ ${CASE_C} -eq 1 ]; then cornelia_format
  126.     fi; convert_image; rm -r cornelia_gen # deletes temporary file
  127. fi; exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement