Update documentation - increase Doxygen usage
[geeqie.git] / plugins / image-crop / geeqie-image-crop
1 #!/bin/bash
2
3 ## @file
4 ## @brief Crop image
5 ##
6 ## Requires ImageMagick and exiftool  
7 ## Crops the image to the size set by the Draw Rectangle menu item
8 ##
9
10 process_raw ()
11 {
12         tmpdir=$(mktemp --tmpdir --directory geeqie_crop_image_XXXXXX)
13
14         list=$(exiv2 -pp "$1")
15         if [[ ! -z "$list" ]]
16         then
17                 readarray -t split_list <<<"$list"
18
19                 array_length="${#split_list[@]}" 
20                 exiv2 --location "$tmpdir" -ep"$array_length" "$1"
21
22                 src_filename=$(ls "$tmpdir/")
23                 filename="${src_filename%.*}"
24                 extension="${src_filename##*.}"
25                 rotation=$(exiftool -Orientation -n "$1" | cut -d':' -f2 | xargs)
26                 convert "$tmpdir/$src_filename" -crop "$2" "$tmpdir/$filename-crop.$extension"
27
28                 exiftool -Orientation=$rotation -n "$tmpdir/$filename-crop.$extension"
29
30                 rm "$tmpdir/$src_filename"
31
32                 geeqie --remote view:"$tmpdir/$filename-crop.$extension"
33                 res=0
34         else
35                 res=1
36         fi
37
38         return $res
39 }
40
41 process_plain ()
42 {
43         tmpdir=$(mktemp --tmpdir --directory geeqie_crop_image_XXXXXX)
44
45         src_filename=$(basename -- "$1")
46         filename="${src_filename%.*}"
47         extension="${src_filename##*.}"
48         convert "$1" -crop "$2" "$tmpdir/$filename-crop.$extension"
49         if [ $? = 1 ]
50         then
51                 zenity --error --title="$title" --text="Cannot process this file format" --width="$width" --window-icon="$window_icon"
52         else
53                 geeqie --remote view:"$tmpdir/$filename-crop.$extension"
54         fi
55 }
56
57 export window_icon="/usr/local/share/pixmaps/geeqie.png"
58 export title="Geeqie crop image"
59 export width="250"
60
61 if [ -x "$(command -v convert)" ]
62 then
63         if [ -x "$(command -v exiftool)" ]
64         then
65
66                 coords=$(geeqie --remote --get-rectangle)
67
68                 if [ -z "$coords" ]
69                 then
70                         zenity --error --title="$title" --text="Rectangle coordinates have not been set" --width="$width" --window-icon="$window_icon" 2>/dev/null
71                         exit 0
72                 fi
73
74                 filename=$(basename -- "$1")
75                 extension="${filename##*.}"
76
77                 if [ "${extension,,}" = "jpeg" ]
78                 then
79                         source_file="$1"
80                         process_plain "$1" $coords
81                 elif [ "${extension,,}" = "jpg" ]
82                 then
83                         source_file="$1"
84                         process_plain "$1" $coords
85                 elif [ "${extension,,}" = "png" ]
86                 then
87                         source_file="$1"
88                         process_plain "$1" $coords
89                 elif [ "${extension,,}" = "tif" ]
90                 then
91                         source_file="$1"
92                         process_plain "$1" $coords
93                 elif [ "${extension,,}" = "tiff" ]
94                 then
95                         source_file="$1"
96                         process_plain "$1" $coords
97                 else
98                         process_raw "$1" $coords
99                         if [ $? = 1 ]
100                         then
101                                 process_plain "$1" $coords
102                         fi
103                 fi
104         else
105                 zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nexiftool is not installed" --title="$title" --window-icon="$window_icon" 2>/dev/null
106                 exit 0
107         fi
108 else
109         zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nImageMagick is not installed" --title="$title" --window-icon="$window_icon" 2>/dev/null
110         exit 0
111 fi