summaryrefslogtreecommitdiff
path: root/share/applications/gcstar-thumbnailer
blob: ff054fe5cbac929109e1bdd09925063647455d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/perl
use strict;

use XML::Simple;
use URI::file;
use Gtk2;

{
    # GCS file to load
    my $uriIn = URI->new($ARGV[0]);
    my $fileIn = $uriIn->file;

    my $maxIcons = 16;
    my $itemsPerRow = 4;
    my $skin;
    my $skinFile;

    # Find user selected skin
    # Fix - hardcoded path
    open (CONF, $ENV{'HOME'}.'/.config/gcstar/GCstar.conf');
    my $line;
    foreach $line (<CONF>) {
        if ($line =~ /listImgSkin=(.*)/)
        {
            $skinFile= $1;
        }
    }
    if (!$skinFile)
    {
        $skinFile = "Wood";
    }
    close (CONF);

    # Fix - hardcoded path
    $skin = "/usr/local/share/gcstar/list_bg/".$skinFile."/list_bg.png";

    # Load the collection file temporarily, to grab the model type
    my $collectionXML= XMLin($fileIn, forcearray => 1);

    # Load the collection model to determine id and cover fields
    # Fix - hardcoded path
    my $collectionModel = XMLin("/usr/local/lib/gcstar/GCModels/".$collectionXML->{type}.".gcm");
    my $idField = $collectionModel->{options}->{fields}->{id};
    my $picField = $collectionModel->{options}->{fields}->{cover};

    # Now that we now the id field, reopen collection xml using proper id
    $collectionXML= XMLin($fileIn, forcearray => 1, keyattr=>$idField);
    my $collectionItems=$collectionXML->{item};
    
    my $count;
    my $favCount;
    my $nonFavCount;
    my @images;
    my @favourites;
    my @nonFavourites;

    # Hacky part, but for some reason items won't load as an array, so just jump through collection grabbing the first few covers
    # (Prefer favourites - so I don't see romantic comedies as the thumbnail for my films!!)

    foreach my $collectionItem (values %$collectionItems)
    {
        if ($collectionItem->{$picField})
        {
            if ($collectionItem->{favourite})
            {
                if ($favCount < $maxIcons)
                {
                     @favourites[$favCount] = $collectionItem->{$picField};              
                     $favCount++;
                }
            }
            else
            {
                     @nonFavourites[$nonFavCount] = $collectionItem->{$picField};
                     $nonFavCount++; 
            }
            
        }
    }

    # Load a few pixbufs
    my $gcstarIconPixBuf;
    my $backgroundPixBuf;

    # Fix - hardcoded path
    $gcstarIconPixBuf = Gtk2::Gdk::Pixbuf->new_from_file("/usr/local/share/gcstar/icons/gcstar_24x24.png");
    $backgroundPixBuf = Gtk2::Gdk::Pixbuf->new_from_file($skin);


    my $tempPixbuf;
    my @pixBuf;
    my $bgWidth = $backgroundPixBuf->get_width;
    my $bgHeight = $backgroundPixBuf->get_height;
    my $maxHeight;

    # Load covers to pixbufs, scale them to fit in background
    for (my $imgCount = 0; $imgCount < $maxIcons; $imgCount++)
    {
        if (@favourites[$imgCount])
        {
            @pixBuf[$imgCount] = Gtk2::Gdk::Pixbuf->new_from_file(@favourites[$imgCount]);
            # Scale to 80% of background size (seems like a good size)
            @pixBuf[$imgCount] = scaleMaxPixbuf(@pixBuf[$imgCount], $bgWidth*.80, $bgHeight*.80);
            if (@pixBuf[$imgCount]->get_height > $maxHeight)
            {
                $maxHeight = @pixBuf[$imgCount]->get_height;
            }
        } 
        elsif (@nonFavourites[$imgCount - $favCount])
        {
            @pixBuf[$imgCount] = Gtk2::Gdk::Pixbuf->new_from_file(@nonFavourites[$imgCount - $favCount]);
            # Scale to 80% of background size (seems like a good size)
            @pixBuf[$imgCount] = scaleMaxPixbuf(@pixBuf[$imgCount], $bgWidth*.80, $bgHeight*.80);
            if (@pixBuf[$imgCount]->get_height > $maxHeight)
            {
                $maxHeight = @pixBuf[$imgCount]->get_height;
            }
        }
        else
        {
            # If we run out of items in the collection with covers, just make empty pixbufs for simplicity
            @pixBuf[$imgCount] = Gtk2::Gdk::Pixbuf->new('rgb',1,8,$bgWidth * .80,$bgHeight * .80);
            @pixBuf[$imgCount]->fill(0x00000000);
        }
    }

    my $factor;
    my $rows;
    my $destHeight;
    
    # Change the positioning slightly if it's a reflection type skin (items sit higher on shelf)
    # Work out number of rows of pictures, and desired height of rows (not much logic to these numbers,
    # it's just what looks good)
    if ($skinFile =~ /Glass/)
    {
        $factor = 40;
        if ($maxHeight < 70)
        {
            $rows = 4;
            $destHeight = $bgHeight - 145;
        }
        elsif ($maxHeight < 130)
        {
            $rows = 3;
            $destHeight = $bgHeight - 100;
        }
        else
        {
            $rows = 2;
            $destHeight = $bgHeight;
        }
    }
    else
    {
        $factor = 10;
        if ($maxHeight < 70)
        {
            $rows = 4;
            $destHeight = $bgHeight - 60;
        }
        elsif ($maxHeight < 130)
        {
            $rows = 3;
            $destHeight = $bgHeight - 15;
        }
        else
        {
            $rows = 3;
            $destHeight = $bgHeight;
        }
    }

    # Put covers on background
    for (my $imgCount = 0; $imgCount < $itemsPerRow * $rows; $imgCount++)
    {
        $tempPixbuf = $backgroundPixBuf->copy;
        @pixBuf[$imgCount]->composite($tempPixbuf,
                                        ( $bgWidth - @pixBuf[$imgCount]->get_width ) / 2,
                                        $bgHeight - @pixBuf[$imgCount]->get_height - $factor,
                                        @pixBuf[$imgCount]->get_width, @pixBuf[$imgCount]->get_height,
                                        ( $bgWidth - @pixBuf[$imgCount]->get_width ) / 2,
                                        $bgHeight - @pixBuf[$imgCount]->get_height - $factor,
                                        1, 1,
                                        'nearest', 255);

        if ($rows > 2)
        {
            # If needed, crop image to avoid wasted space
            $tempPixbuf = $tempPixbuf->new_subpixbuf(0,$bgHeight-$destHeight,$bgWidth,$destHeight);
        }
        @pixBuf[$imgCount] = $tempPixbuf;   
    }

    # Get pixbuf ready for final image
    $tempPixbuf = Gtk2::Gdk::Pixbuf->new('rgb',1,8,$bgWidth * $itemsPerRow,$destHeight * $rows);
    $tempPixbuf->fill(0x00000000);

    # Put each cover in place
    for (my $imgCount = 0; $imgCount < $itemsPerRow * $rows; $imgCount++)
    {
        @pixBuf[$imgCount]->composite($tempPixbuf,
                                      @pixBuf[0]->get_width * ($imgCount % $itemsPerRow),
                                      @pixBuf[0]->get_height * int($imgCount / $itemsPerRow),
                                      @pixBuf[5]->get_width,
                                      @pixBuf[5]->get_height,
                                      @pixBuf[0]->get_width * ($imgCount % $itemsPerRow),
                                      @pixBuf[0]->get_height * int($imgCount / $itemsPerRow),
                                      1, 1, 'nearest', 255);
    }

    # Scale to specified size, or else to max 128 pixels wide/high
    if ($ARGV[2])
    {
        $tempPixbuf = scaleMaxPixbuf($tempPixbuf,$ARGV[2],$ARGV[2]);
    }
    else
    {
        $tempPixbuf = scaleMaxPixbuf($tempPixbuf,128,128);
    }

    # Place little gcstar icon in corner
    $gcstarIconPixBuf->composite($tempPixbuf,
                                  $tempPixbuf->get_width - $gcstarIconPixBuf->get_width - 4,
                                  $tempPixbuf->get_height - $gcstarIconPixBuf->get_height - 5,
                                  $gcstarIconPixBuf->get_width,
                                  $gcstarIconPixBuf->get_height, 
                                  $tempPixbuf->get_width - $gcstarIconPixBuf->get_width - 4,
                                  $tempPixbuf->get_height - $gcstarIconPixBuf->get_height - 5,
                                  1,1,'nearest',255);

    # Save pixbuf
    $tempPixbuf->save($ARGV[1],"png");

}

sub scaleMaxPixbuf
{
    my ($pixbuf, $maxWidth, $maxHeight) = @_;

    my ($width, $height) = ($pixbuf->get_width, $pixbuf->get_height);
    if (($height > $maxHeight) || ($width > $maxWidth)) 
    {
        my ($newWidth, $newHeight);
        my $ratio = $height / $width;
        if (($width) * ($maxHeight / $height) < $maxWidth)
        {
            $newHeight = $maxHeight;
            $newWidth = $newHeight / $ratio;
        }
        else
        {
            $newWidth = $maxWidth;
            $newHeight = $newWidth * $ratio;
        }

 	$pixbuf = $pixbuf->scale_simple($newWidth, $newHeight, 'bilinear');
    }

    return $pixbuf;
}