3
3
# Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
5
# This file is part of add-copyright-to-images (hereafter referred to as "this
7
# See http://www.waxworlds.org/edam/software/add-copyright-to-images for more
5
# This file is part of prep-images (hereafter referred to as "this program").
6
# See http://www.waxworlds.org/edam/software/gtk/prep-images for more
10
9
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Lesser General Public License as published
12
# by the Free Software Foundation, either version 3 of the License, or
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
13
12
# (at your option) any later version.
15
14
# This program is distributed in the hope that it will be useful,
16
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU Lesser General Public License for more details.
17
# GNU General Public License for more details.
20
# You should have received a copy of the GNU Lesser General Public License
19
# You should have received a copy of the GNU General Public License
21
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
74
73
self.drawing_area = gtk.DrawingArea()
75
74
self.drawing_area.set_size_request( init_width, init_height )
76
75
self.drawing_area.add_events( gtk.gdk.POINTER_MOTION_MASK | \
77
gtk.gdk.POINTER_MOTION_HINT_MASK | \
78
gtk.gdk.BUTTON_PRESS_MASK )
76
gtk.gdk.POINTER_MOTION_HINT_MASK | \
77
gtk.gdk.BUTTON_PRESS_MASK )
79
78
self.drawing_area.connect( "configure_event", \
80
self.drawing_area_configure_event )
79
self.drawing_area_configure_event )
81
80
self.drawing_area.connect( "expose_event", \
82
self.drawing_area_expose_event )
81
self.drawing_area_expose_event )
83
82
self.drawing_area.connect( "motion_notify_event", \
84
self.motion_notify_event )
83
self.motion_notify_event )
85
84
self.drawing_area.connect( "button_press_event", \
86
self.drawing_area_button_press_event )
85
self.drawing_area_button_press_event )
87
86
vbox.pack_start( self.drawing_area, True, True, 0 )
146
146
# check if we've got a copyright filename set
147
147
if self.prefs.copyright_filename == '':
148
148
dialog = gtk.MessageDialog( self.window, gtk.DIALOG_MODAL, \
149
gtk.MESSAGE_INFO, gtk.BUTTONS_OK, \
150
"No copyright image has been set. " + \
151
"Please set one in the Preferences." )
149
gtk.MESSAGE_INFO, gtk.BUTTONS_OK, \
150
"No copyright image has been set. " + \
151
"Please set one in the Preferences." )
152
152
dialog.format_secondary_text( "While this issue persists, you " + \
153
"can not proceed with this operation" )
153
"can not proceed with this operation" )
158
158
# load copyright overlay image file
160
160
self.copyright_pic = gtk.gdk.pixbuf_new_from_file( \
161
os.path.expanduser( self.prefs.copyright_filename ) )
161
os.path.expanduser( self.prefs.copyright_filename ) )
162
162
except gobject.GError as e:
163
163
dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, \
164
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, \
165
"Unable to load copyright overlay image: " + \
164
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, \
165
"Unable to load copyright overlay image: " + \
167
167
dialog.format_secondary_text( "While this issue persists, you " + \
168
"can not proceed with this operation" )
168
"can not proceed with this operation" )
174
174
if self.copyright_pic.get_width() > self.pic.get_width() or \
175
175
self.copyright_pic.get_height() > self.pic.get_height():
176
176
dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, \
177
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, \
178
"The copyright overlay image is larger than the photo!" )
177
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, \
178
"The copyright overlay image is larger than the photo!" )
179
179
dialog.format_secondary_text( "While this issue persists, you " + \
180
180
"can not proceed with this operation" )
201
201
append = self.prefs.export_append
202
202
out_filename = self.filename[ : self.filename.rindex( '.' ) ] + \
203
append + self.filename[ self.filename.rindex( '.' ) : ]
203
append + self.filename[ self.filename.rindex( '.' ) : ]
205
205
# calculate geometry
206
206
top = 0 if self.pos.find( 'top' ) != -1 else \
207
self.pic.get_height() - self.copyright_pic.get_height()
207
self.pic.get_height() - self.copyright_pic.get_height()
208
208
left = 0 if self.pos.find( 'left' ) != -1 else \
209
self.pic.get_width() - self.copyright_pic.get_width()
209
self.pic.get_width() - self.copyright_pic.get_width()
211
211
infile = self.filename
213
# caluclate program args
213
# calculate program args
214
214
resize_geometry = str( self.prefs.resize_width ) + "x" + \
215
str( self.prefs.resize_height )
215
str( self.prefs.resize_height )
216
216
copyright_geometry = str( self.copyright_pic.get_width() ) + \
217
"x" + str( self.copyright_pic.get_height() ) + "+" + \
218
str( left ) + "+" + str( top )
217
"x" + str( self.copyright_pic.get_height() ) + "+" + \
218
str( left ) + "+" + str( top )
219
219
args = [ self.IMAGEMAGICK_CONVERT_BIN, \
220
os.path.expanduser( self.prefs.copyright_filename ), \
221
"-geometry", copyright_geometry, \
222
"-compose", "screen", "-composite", \
220
os.path.expanduser( self.prefs.copyright_filename ), \
221
"-geometry", copyright_geometry, \
222
"-compose", "screen", "-composite", \
224
224
if( self.prefs.resize ):
225
225
args[ 1 : 1 ] = [ "(", "-resize", resize_geometry, \
226
self.filename, "-unsharp", "1x3+.6+.09", ")" ]
228
228
args[ 1 : 1 ] = [ self.filename ]
230
230
# call ImageMagick to do our dirtywork
232
print "calling: " + ' '.join( args )
232
233
ret = call( args )
234
235
print >>sys.stderr, "ImageMagick failed, returned ", ret
332
333
# work out minimum scale required to fit pic_orig in to the widget area
333
334
scale = min( float( width ) / self.pic.get_width(),
334
float( height ) / self.pic.get_height() )
335
float( height ) / self.pic.get_height() )
336
337
# scale the images
337
338
self.pic_scale = self.pic.scale_simple( \
338
int( round( scale * self.pic.get_width() ) ), \
339
int( round( scale * self.pic.get_height() ) ), \
340
gtk.gdk.INTERP_BILINEAR )
339
int( round( scale * self.pic.get_width() ) ), \
340
int( round( scale * self.pic.get_height() ) ), \
341
gtk.gdk.INTERP_BILINEAR )
341
342
if self.copyright_pic != None:
342
343
self.copyright_pic_scale = self.copyright_pic.scale_simple( \
343
int( round( scale * self.copyright_pic.get_width() ) ), \
344
int( round( scale * self.copyright_pic.get_height() ) ), \
345
gtk.gdk.INTERP_BILINEAR )
344
int( round( scale * self.copyright_pic.get_width() ) ), \
345
int( round( scale * self.copyright_pic.get_height() ) ), \
346
gtk.gdk.INTERP_BILINEAR )
347
348
# recreate the pixmap to render to the screen
348
349
self.generate_pixmap()
364
365
pixbuf = self.pic_scale.copy()
366
367
if self.copyright_pic != None and \
367
getattr( self, "copyright_pic_scale", None ) != None:
368
getattr( self, "copyright_pic_scale", None ) != None:
369
370
# work out position of copyright overlay
370
371
if self.pos == "top-left" or self.pos == "bottom-left":
372
373
if self.pos == "top-right" or self.pos == "bottom-right":
373
374
x = self.pic_scale.get_width() - \
374
self.copyright_pic_scale.get_width()
375
self.copyright_pic_scale.get_width()
375
376
if self.pos == "top-left" or self.pos == "top-right":
377
378
if self.pos == "bottom-left" or self.pos == "bottom-right":
378
379
y = self.pic_scale.get_height() - \
379
self.copyright_pic_scale.get_height()
380
self.copyright_pic_scale.get_height()
382
383
self.copyright_pic_scale.composite( pixbuf, x, y, \
383
self.copyright_pic_scale.get_width(), \
384
self.copyright_pic_scale.get_height(), \
385
x, y, 1, 1, gtk.gdk.INTERP_NEAREST, 255 )
384
self.copyright_pic_scale.get_width(), \
385
self.copyright_pic_scale.get_height(), \
386
x, y, 1, 1, gtk.gdk.INTERP_NEAREST, 255 )
387
# draw composit pixbuf to the off-screen pixmap
388
# draw composite pixbuf to the off-screen pixmap
388
389
self.pixmap.draw_pixbuf( widget.get_style().white_gc, \
389
pixbuf, 0, 0, pre_width, pre_height, \
390
pixbuf.get_width(), pixbuf.get_height() )
390
pixbuf, 0, 0, pre_width, pre_height, \
391
pixbuf.get_width(), pixbuf.get_height() )
392
# draw black rectangles if the pic is being centered
393
# draw black rectangles if the pic is being centred
393
394
if pre_width > 0 or pre_height > 0:
394
395
self.pixmap.draw_rectangle( widget.get_style().black_gc, True, \
396
pre_width if pre_width > 0 else width, \
397
pre_height if pre_height > 0 else height )
397
pre_width if pre_width > 0 else width, \
398
pre_height if pre_height > 0 else height )
398
399
if post_width > 0 or post_height > 0:
399
400
self.pixmap.draw_rectangle( widget.get_style().black_gc, True, \
400
width - post_width if post_width > 0 else 0, \
401
height - post_height if post_height > 0 else 0, \
402
post_width if post_width > 0 else width, \
403
post_height if post_height > 0 else height )
401
width - post_width if post_width > 0 else 0, \
402
height - post_height if post_height > 0 else 0, \
403
post_width if post_width > 0 else width, \
404
post_height if post_height > 0 else height )
405
406
def move_pos( self, tok_from, tok_to ):
406
408
# calculate copyright overlay pos
407
409
pos = self.pos.replace( tok_from, tok_to, 1 )
415
417
def drawing_area_expose_event( self, widget, event ):
416
418
x, y, width, height = event.area
417
419
widget.window.draw_drawable( \
418
widget.get_style().fg_gc[ gtk.STATE_NORMAL ], \
419
self.pixmap, x, y, x, y, width, height )
420
widget.get_style().fg_gc[ gtk.STATE_NORMAL ], \
421
self.pixmap, x, y, x, y, width, height )