-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.rb
315 lines (262 loc) · 7.93 KB
/
database.rb
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Database of current images
require 'yaml'
require 'json'
require 'gtk2'
require 'RMagick'
require 'fileutils'
$blank = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB, false, 8, DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH*3/4)
class Database
private_class_method :new
def Database.load(directory)
db = nil
if File.exists?(directory + '/simplephoto.yaml')
db = YAML::load( File.read(directory + '/simplephoto.yaml') )
db.postload(directory)
else
db = new(directory)
end
db.addimages()
db
end
attr_reader :directory
attr_reader :images
def encode_with coder
coder['images'] = @images
end
def init_with coder
@images = coder['images']
@directory = nil
@backup = false
end
def initialize(directory)
@directory = directory
@images = []
@backup = false
end
def postload(directory)
@directory = directory
@images.each{|im| im.directory = directory}
end
def addimages()
dirlist = Dir.new(@directory).to_a.sort!
dirlist.each{|filename|
next if (filename.upcase !~ /\.JPG$/)
if ([email protected]?{|im| im.hasfile?(filename) } &&
[email protected]?{|im| im.ispossiblealt?(filename) }) then
@images << Image.new(@directory, filename)
end
}
# Remove references to non-existing files
@images.reject!{|im|
!dirlist.any?{|file| file == im.filename}
}
@images.sort_by!{|im| im.filename.upcase }
end
def save()
if (!@backup) then
if (File.exists?(directory + '/simplephoto.yaml')) then
date = DateTime.now.strftime("%Y%jT%H%MZ")
FileUtils.mv(directory + '/simplephoto.yaml', directory + "/simplephoto.yaml.bkp-#{date}")
end
@backup = true
end
File.open(directory + '/simplephoto.yaml', 'w') { |f|
f.print self.to_yaml
}
end
end
class Image
attr_reader :filename
attr_reader :description
attr_reader :rank
attr_reader :alt
attr_reader :defaultalt
attr_accessor :directory
# Non-persistent
# GDK pixbuf thumbnail
attr_accessor :thumbnail
# Display in filelist?
attr_accessor :display
attr_accessor :histogram
def encode_with coder
coder['filename'] = @filename
coder['description'] = @description
coder['rank'] = @rank
coder['alt'] = @alt
coder['defaultalt'] = @defaultalt
end
def init_with coder
@filename = coder['filename']
@description = coder['description']
@rank = coder['rank']
@alt = coder['alt']
@defaultalt = coder['defaultalt']
initdefault()
end
def initialize(directory, filename)
@directory = directory
@filename = filename
@description = ""
@rank = 0
initdefault()
end
def initdefault()
@thumbnail = $blank
@exif = nil
@display = true
@alt = [] if (!@alt)
@defaultalt = -1 if (!@defaultalt)
@cacheinfo = nil
end
def exif
return @exif if (@exif)
exiftool = `exiftool -json #{self.fullname}`
@exif = JSON.parse(exiftool)[0]
return @exif
end
def genthumbnail(force = false)
return if (@thumbnail != $blank)
# Hack to make sure thumbnail is generated in another process
results = Parallel.map([self]) do |im|
image = Magick::Image::read(im.fullname).first
image.auto_orient!
pix_w = image.columns
pix_h = image.rows
r1 = DEFAULT_IMAGE_WIDTH/pix_w.to_f
r2 = DEFAULT_IMAGE_HEIGHT/pix_h.to_f
out = [im.fullname, image.thumbnail([r1, r2].min)]
image.destroy!
out
end
scaled_pix = results[0][1]
@thumbnail = Gdk::Pixbuf.new(scaled_pix.export_pixels_to_str(), Gdk::Pixbuf::COLORSPACE_RGB,
false, 8, scaled_pix.columns, scaled_pix.rows, scaled_pix.columns*3)
scaled_pix.destroy!
#FIXME: Make sure thumbnail is updated in the listmodel of photolist.rb
end
#FIXME: Clear up cache when necessary
def genfullimage(width, height)
cacheinfo = [fullname, width, height, File.mtime(fullname)]
if (cacheinfo != @cacheinfo) then
image = Magick::Image::read(fullname).first
image.auto_orient!
pix_w = image.columns
pix_h = image.rows
r1 = width/pix_w.to_f
r2 = height/pix_h.to_f
simage = image.scale([r1, r2].min)
hist = (0..255).map{0}
#.quantize(256, Magick::GRAYColorspace, Magick::NoDitherMethod)
simage.thumbnail(200, 200).color_histogram.each{|pixel, count|
hist[(pixel.intensity*255/Magick::QuantumRange).round.to_i] += count
}
sum = hist.inject(0){|sum, t| sum + t}
@histogram = hist.map{|x| x.to_f/sum}
@imagecache = simage
@cacheinfo = cacheinfo
else
simage = @imagecache
end
if (@defaultalt > -1) then
@alt[@defaultalt].modifiers.each{|mod|
simage = mod.apply(simage)
}
end
simage
end
def export(outfile)
image = Magick::Image::read(fullname).first
if (@defaultalt > -1) then
@alt[@defaultalt].modifiers.each{|mod|
image = mod.apply(image)
}
end
image.write(outfile)
image.destroy!
GC.start()
end
def fullname
@directory + "/" + currentfilename
end
def currentfilename
((@defaultalt == -1) ? @filename : @alt[@defaultalt].filename)
end
def description=(desc)
@description = desc
end
def rank=(rank)
@rank = rank
end
def createalt(newfile)
fn = @filename
if (newfile) then
base = fn
ext = ""
match = fn.match(/(.*)(\..*)/)
if (match) then
base = match[1]
ext = match[2]
else
puts "ERROR: Cannot split #{fn} is base/ext pair! Continuing nevertheless"
end
i = 0
begin
fn = "#{base}_mod#{i}#{ext}"
i += 1
end while (File.exists?(@directory + "/" + fn))
puts "Export as #{fn}."
export(@directory + "/" + fn)
end
newalt = ImageAlternate.new(fn)
@alt << newalt
@defaultalt = @alt.length-1
end
def nextalt
puts "da=#{@defaultalt}"
@defaultalt += 1
@defaultalt = -1 if (@defaultalt >= @alt.length)
puts "dae=#{@defaultalt}"
end
def removealt
@alt.delete_at(@defaultalt) if (@defaultalt > 0)
@defaultalt -= 1
nextalt()
end
def getcurrentalt(create)
if (@defaultalt == -1) then
if (create) then
createalt(false)
else
return nil
end
end
@alt[@defaultalt]
end
def hasfile?(filename)
return true if (@filename == filename)
@alt.each{|alt|
return true if (alt.filename == filename)
}
return false
end
def ispossiblealt?(filename)
prefix = filename.upcase.split(/[-_.]/)[0]
prefix2 = @filename.upcase.split(/[-_.]/)[0]
if (prefix == prefix2) then
newalt = ImageAlternate.new(filename)
@alt << newalt
@defaultalt = @alt.length-1
return true
end
return false
end
end
# Reference to a modified image
class ImageAlternate
attr_reader :filename
attr_reader :modifiers
def initialize(filename)
@filename = filename
@modifiers = []
end
end