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
|
#include <gtk-3.0/gtk/gtk.h>
#include <stdlib.h>
#include "image.h"
#include "window.h"
static GtkWidget *image;
static GdkPixbuf *pixbuf;
static GdkPixbuf *curr_pixbuf;
static int pixbuf_width, pixbuf_height, curr_pixbuf_width, curr_pixbuf_height;
static double aspect_ratio;
static double curr_zoom;
static enum scale_mode curr_scale_mod;
static int loading = 0;
static GFile *file;
static GFileInputStream *is;
GtkWidget *new_image()
{
image = gtk_image_new();
return image;
}
void pixbuf_load_callback(
GObject *source_object, GAsyncResult *res, gpointer user_data)
{
if (pixbuf != NULL)
g_object_unref(pixbuf);
if (is != NULL)
g_object_unref(is);
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_stream_finish(res, &error);
if (error != NULL) {
loading = 0;
g_printerr("%s\n", error->message);
exit(EXIT_FAILURE);
}
curr_pixbuf_width = pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf));
curr_pixbuf_height = pixbuf_height =
gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf));
aspect_ratio = (double)pixbuf_width / (double)pixbuf_height;
fit_image();
set_window_title(g_file_get_basename(file), pixbuf_width, pixbuf_height);
if (file != NULL)
g_object_unref(file);
loading = 0;
}
void file_read_callback(
GObject *source_object, GAsyncResult *res, gpointer user_data)
{
GError *error = NULL;
is = g_file_read_finish(file, res, &error);
if (error != NULL) {
loading = 0;
g_printerr("%s\n", error->message);
exit(EXIT_FAILURE);
}
gdk_pixbuf_new_from_stream_async(
G_INPUT_STREAM(is), NULL, &pixbuf_load_callback, NULL);
}
int load_image(char *file_name)
{
if (loading)
return -1;
loading = 1;
file = g_file_new_for_path(file_name);
g_file_read_async(
file, G_PRIORITY_DEFAULT, NULL, &file_read_callback, NULL);
curr_zoom = 1.0;
return 0;
}
void update_pixbuf()
{
if (curr_pixbuf != NULL)
g_object_unref(curr_pixbuf);
if (pixbuf == NULL)
return;
curr_pixbuf = gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), curr_pixbuf_width,
curr_pixbuf_height, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf));
}
void fit_image()
{
gint win_width, win_height;
get_curr_win_size(&win_width, &win_height);
curr_scale_mod = fit;
if (pixbuf == NULL || win_width < 1 || win_height < 1)
return;
if (win_width < pixbuf_width && win_height >= pixbuf_height) {
curr_pixbuf_width = win_width;
curr_pixbuf_height = (double)curr_pixbuf_width / aspect_ratio;
} else if (win_width >= pixbuf_width && win_height < pixbuf_height) {
curr_pixbuf_height = win_height;
curr_pixbuf_width = (double)curr_pixbuf_height * aspect_ratio;
} else if (win_width < pixbuf_width && win_height < pixbuf_height) {
if (((double)win_width / (double)win_height) > aspect_ratio) {
curr_pixbuf_height = win_height;
curr_pixbuf_width = ((double)curr_pixbuf_height * aspect_ratio);
} else {
curr_pixbuf_width = win_width;
curr_pixbuf_height = (double)curr_pixbuf_width / aspect_ratio;
}
} else {
curr_pixbuf_width = pixbuf_width;
curr_pixbuf_height = pixbuf_height;
}
curr_zoom = (double)curr_pixbuf_width / (double)pixbuf_width;
if (curr_pixbuf_width < 1 || curr_pixbuf_height < 1)
return;
update_pixbuf();
}
void zoom(int type)
{
curr_scale_mod = zoomed;
if (pixbuf == NULL)
return;
if (type == 0) {
if (curr_zoom == 1.0)
return;
curr_zoom = 1.0;
} else if (type < 0) {
if (curr_zoom < 0.2)
return;
curr_zoom -= 0.1;
} else if (type > 0) {
if (curr_zoom > 2)
return;
curr_zoom += 0.1;
}
curr_pixbuf_width = curr_zoom * (double)pixbuf_width;
curr_pixbuf_height = curr_zoom * (double)pixbuf_height;
update_pixbuf();
}
enum scale_mode get_current_scale_mode()
{
return curr_scale_mod;
}
|