summaryrefslogtreecommitdiff
path: root/libcore/imagemagickwrapper.c
blob: 79e48e71c1da553b0fc98f34b7862d8d7dd6f024 (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
#include <string>
#include <sstream>
#include "imagemagickwrapper.h"
#include "../config.h"
#include "imagescaler.h"

cImageMagickWrapper::cImageMagickWrapper() {
    InitializeMagick(NULL);
}

cImageMagickWrapper::~cImageMagickWrapper() {
}

cImage *cImageMagickWrapper::CreateImage(int width, int height, bool preserveAspect) {
    if (image == NULL) return NULL;

    int w, h;
    w = cairo_image_surface_get_width(image);
    h = cairo_image_surface_get_height(image);
    if (width == 0)
        width = w;
    if (height == 0)
        height = h;

    cairo_surface_t *surface;
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);

    cairo_t *cr;
    cr = cairo_create(surface);

    double sx = width / (double)w;
    double sy = height / (double)h;
    if (preserveAspect) {
        if (sx < sy)
            sy = sx;
        if (sy < sx)
            sx = sy;
    }
    cairo_scale(cr, sx, sy);

    cairo_set_source_surface(cr, image, 0, 0);
    cairo_paint(cr);

    cairo_status_t status = cairo_status (cr);
    if (status)
        dsyslog("skindesigner: Cairo CreateImage Error %s", cairo_status_to_string(status));

    unsigned char *data = cairo_image_surface_get_data(surface);
    cImage *cimage = new cImage(cSize(width, height), (tColor*)data);

    cairo_destroy(cr);
    cairo_surface_destroy(image);
    image = NULL;

    return cimage;
}

bool cImageMagickWrapper::LoadImage(const char *fullpath) {
    if ((fullpath == NULL) || (strlen(fullpath) < 5))
        return false;

    if (image != NULL) cairo_surface_destroy(image);

    if (config.debugImageLoading)
        dsyslog("skindesigner: trying to load: %s", fullpath);

    image = cairo_image_surface_create_from_png(fullpath);

    if (cairo_surface_status(image)) {
        if (config.debugImageLoading)
            dsyslog("skindesigner: Cairo LoadImage Error: %s", cairo_status_to_string(cairo_surface_status(image)));
        image = NULL;
        return false;
    }

    return true;
}

// Just a different way to call LoadImage. Calls the above one.
bool cImageMagickWrapper::LoadImage(std::string FileName, std::string Path, std::string Extension) {
    std::stringstream sstrImgFile;
    sstrImgFile << Path << FileName << "." << Extension;
    std::string imgFile = sstrImgFile.str();
    return LoadImage(imgFile.c_str());
}