-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.go
300 lines (247 loc) · 7.91 KB
/
image.go
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
/*
* Copyright (c) 2013-2021 Utkan Güngördü <[email protected]>
* Copyright (c) 2021-2024 Piotr Grabowski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gomicsv
import (
"fmt"
"log"
"path/filepath"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
"github.com/fauu/gomicsv/util"
)
const savedImagePNGQuality = 9
var interpolations = []gdk.InterpType{gdk.INTERP_NEAREST, gdk.INTERP_TILES, gdk.INTERP_BILINEAR, gdk.INTERP_HYPER}
func (app *App) pixbufLoaded() bool {
if app.Config.DoublePage && !app.shouldForceSinglePage() {
return app.S.PixbufL != nil && app.S.PixbufR != nil
}
return app.S.PixbufL != nil
}
func (app *App) pixbufSize() (w, h int) {
if !app.pixbufLoaded() {
return 0, 0
}
s := &app.S
if app.Config.DoublePage && !app.shouldForceSinglePage() {
return s.PixbufL.GetWidth() + s.PixbufR.GetWidth(), util.Max(s.PixbufL.GetHeight(), s.PixbufR.GetHeight())
}
return s.PixbufL.GetWidth(), s.PixbufL.GetHeight()
}
func (app *App) updateStatus() {
s := &app.S
if !app.pixbufLoaded() {
return
}
zoom := int(100 * app.S.Scale)
lenStr := "?"
if s.Archive.Len() != nil {
lenStr = fmt.Sprint(*s.Archive.Len())
}
markedStr := ""
if app.currentPageIsJumpmarked() {
markedStr = " MARKED "
}
var msg, title string
if app.Config.DoublePage && !app.shouldForceSinglePage() {
leftPath, _ := s.Archive.Name(s.ArchivePos)
left := filepath.Base(leftPath)
rightPath, _ := s.Archive.Name(s.ArchivePos + 1)
right := filepath.Base(rightPath)
leftIndex := s.ArchivePos + 1
rightIndex := s.ArchivePos + 2
leftw, lefth := s.PixbufL.GetWidth(), s.PixbufL.GetHeight()
rightw, righth := s.PixbufR.GetWidth(), s.PixbufR.GetHeight()
if app.Config.MangaMode {
left, right = right, left
leftIndex, rightIndex = rightIndex, leftIndex
leftw, rightw = rightw, leftw
}
msg = fmt.Sprintf("%d+%d / %s %s | %dx%d - %dx%d (%d%%) | %s | %s - %s", leftIndex, rightIndex, lenStr, markedStr, leftw, lefth, rightw, righth, zoom, s.Archive.ArchiveName(), left, right)
title = fmt.Sprintf("[%d+%d / %s] %s", leftIndex, rightIndex, lenStr, s.Archive.ArchiveName())
} else {
imgPath, _ := s.Archive.Name(s.ArchivePos)
w, h := s.PixbufL.GetWidth(), s.PixbufL.GetHeight()
msg = fmt.Sprintf("%d / %s %s | %dx%d (%d%%) | %s | %s", s.ArchivePos+1, lenStr, markedStr, w, h, zoom, s.Archive.ArchiveName(), imgPath)
title = fmt.Sprintf("[%d / %s] %s", s.ArchivePos+1, lenStr, s.Archive.ArchiveName())
}
app.setStatus(msg)
app.W.MainWindow.SetTitle(title)
}
func (app *App) getImageAreaInnerSize() (width, height int) {
alloc := app.W.ScrolledWindow.GetAllocation()
return alloc.GetWidth() - 4, alloc.GetHeight() - 4 // 2u of padding from the ScrolledWindow and 2u from the Viewport
}
func (app *App) getScaledSize() (scale float64) {
if !app.pixbufLoaded() {
return
}
scrw, scrh := app.getImageAreaInnerSize()
w, h := app.pixbufSize()
switch app.Config.ZoomMode {
case FitToWidth:
needscale := (app.Config.Enlarge && w < scrw) || (app.Config.Shrink && w > scrw)
if needscale {
return float64(scrw) / float64(w)
}
case FitToHalfWidth:
needscale := (app.Config.Enlarge && w/2 < scrw) || (app.Config.Shrink && w/2 > scrw)
if needscale {
return float64(scrw) / float64(w/2)
}
case FitToHeight:
return float64(scrh) / float64(h)
case BestFit:
needscale := (app.Config.Enlarge && (w < scrw && h < scrh)) || (app.Config.Shrink && (w > scrw || h > scrh))
if needscale {
fw, _ := util.Fit(w, h, scrw, scrh)
return float64(fw) / float64(w)
}
}
return 1
}
func (app *App) shouldForceSinglePage() bool {
if app.S.PixbufR == nil {
return true
}
return app.Config.OneWide && (app.S.PixbufL.GetWidth() > app.S.PixbufL.GetHeight() || app.S.PixbufR.GetWidth() > app.S.PixbufR.GetHeight())
}
func (app *App) blit() {
if !app.pixbufLoaded() {
return
}
app.S.Scale = app.getScaledSize()
// Check whether the scale of the left image is different from the old one?
if app.Config.DoublePage && !app.shouldForceSinglePage() {
left := app.S.PixbufL
right := app.S.PixbufR
if app.Config.MangaMode {
left, right = right, left
}
if err := app.doBlit(app.W.ImageL, left, app.S.Scale); err != nil {
app.showError(err.Error())
return
}
if err := app.doBlit(app.W.ImageR, right, app.S.Scale); err != nil {
app.showError(err.Error())
return
}
} else {
app.W.ImageR.Clear()
if err := app.doBlit(app.W.ImageL, app.S.PixbufL, app.S.Scale); err != nil {
app.showError(err.Error())
return
}
}
if app.S.Scale != 1 || app.Config.HFlip || app.Config.VFlip {
util.GC()
}
}
func (app *App) doBlit(image *gtk.Image, pixbuf *gdk.Pixbuf, scale float64) (err error) {
image.Clear()
if app.Config.HFlip {
pixbuf, err = pixbuf.Flip(true)
if err != nil {
return err
}
}
if app.Config.VFlip {
pixbuf, err = pixbuf.Flip(false)
if err != nil {
return err
}
}
if scale != 1 {
w, h := pixbuf.GetWidth(), pixbuf.GetHeight()
pixbuf, err = pixbuf.ScaleSimple(int(float64(w)*scale), int(float64(h)*scale), interpolations[app.Config.Interpolation])
if err != nil {
return err
}
}
image.SetFromPixbuf(pixbuf)
return nil
}
const saveImageErrorMsgTpl = "Couldn't save image: %v"
func (app *App) saveImage(path string) {
if app.S.PixbufR == nil {
if app.S.PixbufL == nil {
return
} else {
if err := app.S.PixbufL.SavePNG(path, savedImagePNGQuality); err != nil {
app.showError(fmt.Sprintf(saveImageErrorMsgTpl, err))
return
}
}
}
// We know we're in double page mode
stichedPixbuf, err := app.getStichedPixbuf()
if err != nil {
app.showError(fmt.Sprintf(saveImageErrorMsgTpl, err))
return
}
if err := stichedPixbuf.SavePNG(path, savedImagePNGQuality); err != nil {
app.showError(fmt.Sprintf(saveImageErrorMsgTpl, err))
return
}
app.notificationShow(fmt.Sprintf("Saved to %s", path), ShortNotification)
}
const copyImageSuccessMsg = "Copied image to clipboard"
func (app *App) copyImageToClipboard() {
clipboard, err := gtk.ClipboardGet(gdk.GdkAtomIntern("CLIPBOARD", true))
if err != nil {
log.Panicf("getting clipboard: %v", err)
}
if app.S.PixbufR == nil {
if app.S.PixbufL != nil {
clipboard.SetImage(app.S.PixbufL)
app.notificationShow(copyImageSuccessMsg, ShortNotification)
}
return
}
// We know we're in double page mode
stichedPixbuf, err := app.getStichedPixbuf()
if err != nil {
log.Printf("Error stiching images: %v", err)
app.showError("Couldn't copy image to clipboard")
return
}
clipboard.SetImage(stichedPixbuf)
app.notificationShow(copyImageSuccessMsg, ShortNotification)
}
// getStichedPixbuf creates a Pixbuf combining the left and right image Pixbufs
func (app *App) getStichedPixbuf() (*gdk.Pixbuf, error) {
l, r := app.S.PixbufL, app.S.PixbufR
if app.Config.MangaMode {
l, r = r, l
}
// ASSUMPTION: Both Pixbufs have the same parameters except for size
mergedPixbuf, err := gdk.PixbufNew(
l.GetColorspace(),
l.GetHasAlpha(),
l.GetBitsPerSample(),
l.GetWidth()+r.GetWidth(),
util.Max(l.GetHeight(), r.GetHeight()),
)
if err != nil {
return nil, fmt.Errorf("creating pixbuf: %v", err)
}
mergedPixbuf.Fill(app.Config.BackgroundColor.ToPixelInt())
// POLISH: If heights are unequal, center the smaller image vertically, just as it appears in the program
pixbufCopyPixels(l, mergedPixbuf, 0)
pixbufCopyPixels(r, mergedPixbuf, l.GetWidth())
return mergedPixbuf, nil
}