aboutsummaryrefslogtreecommitdiff
path: root/image.c
blob: 54892595680fde147ce44e6b04c4272f2afb5249 (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
#include "image.h"
#include <gtk-3.0/gtk/gtk.h>

GtkWidget *new_image()
{
    image = gtk_image_new();
    return image;
}

int load_image(char *file_name, int win_width, int win_height)
{
    printf("loading: %s\n", file_name);
    GError *error = NULL;
    if (pixbuf != NULL)
        g_object_unref(pixbuf);
    pixbuf = gdk_pixbuf_new_from_file(file_name, &error);
    if (error)
        return -1;
    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;
    curr_zoom = 1.0;
    fit_image(win_width, win_height);
    return 1;
}

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(int win_width, int win_height)
{
    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)
{
    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();
}

void print_supported_formats()
{
    GSList *list, *it;
    GdkPixbufFormat *format;
    gchar **extensions;
    gchar *ex;

    list = gdk_pixbuf_get_formats();
    if (list != NULL) {
        for (it = list; it->next != NULL; it = it->next) {
            format = (GdkPixbufFormat *)it->data;
            printf("%s:", gdk_pixbuf_format_get_description(format));
            extensions = gdk_pixbuf_format_get_extensions(format);
            for (ex = *extensions; *ex; ex++) {
                printf(" %s", ex);
            }
            printf("\n");
        }
        g_slist_free(list);
    }
}