summaryrefslogtreecommitdiff
path: root/imagemagickwrapper.c
blob: 7cdf9802c8450f20dd61507050bca87f5f3f943b (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
115
116
117
118
119
120
121
122
123
#include <string>
#include <sstream>
#include "imagemagickwrapper.h"
#include "config.h"

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

cImageMagickWrapper::~cImageMagickWrapper() {
}

cImage *cImageMagickWrapper::CreateImage() {
    int w, h;
    w = buffer.columns();
    h = buffer.rows();
    cImage *image = new cImage(cSize(w, h));
    const PixelPacket *pixels = buffer.getConstPixels(0, 0, w, h);
    for (int iy = 0; iy < h; ++iy) {
        for (int ix = 0; ix < w; ++ix) {
            tColor col = (~int(pixels->opacity * 255 / MaxRGB) << 24)
            | (int(pixels->green * 255 / MaxRGB) << 8)
            | (int(pixels->red * 255 / MaxRGB) << 16)
            | (int(pixels->blue * 255 / MaxRGB) );
            image->SetPixel(cPoint(ix, iy), col);
            ++pixels;
        }
    }
    return image;
}

cImage cImageMagickWrapper::CreateImageCopy() {
    int w, h;
    w = buffer.columns();
    h = buffer.rows();
    cImage image (cSize(w, h));
    const PixelPacket *pixels = buffer.getConstPixels(0, 0, w, h);
    for (int iy = 0; iy < h; ++iy) {
        for (int ix = 0; ix < w; ++ix) {
            tColor col = (~int(pixels->opacity * 255 / MaxRGB) << 24)
            | (int(pixels->green * 255 / MaxRGB) << 8)
            | (int(pixels->red * 255 / MaxRGB) << 16)
            | (int(pixels->blue * 255 / MaxRGB) );
            image.SetPixel(cPoint(ix, iy), col);
            ++pixels;
        }
    }
    return image;
}

bool cImageMagickWrapper::LoadImage(std::string FileName, std::string Path, std::string Extension) {
    try {
        std::stringstream sstrImgFile;
        sstrImgFile << Path << FileName << "." << Extension;
        std::string imgFile = sstrImgFile.str();
        if (config.GetValue("debugImageLoading"))
            dsyslog("nopacity: trying to load: %s", imgFile.c_str());
        buffer.read(imgFile.c_str());
        if (config.GetValue("debugImageLoading"))
            dsyslog("nopacity: %s sucessfully loaded", imgFile.c_str());
    } catch (...) {
        return false;
    }
    return true;
}

bool cImageMagickWrapper::LoadImage(const char *fullpath) {
    if ((fullpath == NULL) || (strlen(fullpath) < 5))
        return false;
    try {
        if (config.GetValue("debugImageLoading"))
            dsyslog("nopacity: trying to load: %s", fullpath);
        buffer.read(fullpath);
        if (config.GetValue("debugImageLoading"))
            dsyslog("nopacity: %s sucessfully loaded", fullpath);
    } catch (...) {
        return false;
    }
    return true;
}

Color cImageMagickWrapper::Argb2Color(tColor col) {
    tIndex alpha = (col & 0xFF000000) >> 24;
    tIndex red = (col & 0x00FF0000) >> 16;
    tIndex green = (col & 0x0000FF00) >> 8;
    tIndex blue = (col & 0x000000FF);
    Color color(MaxRGB*red/255, MaxRGB*green/255, MaxRGB*blue/255, MaxRGB*(0xFF-alpha)/255);
    return color;
}

void cImageMagickWrapper::CreateGradient(tColor back, tColor blend, int width, int height, double wfactor, double hfactor) {
    Color Back = Argb2Color(back);
    Color Blend = Argb2Color(blend);
    int maxw = MaxRGB * wfactor;
    int maxh = MaxRGB * hfactor;

    Image imgblend(Geometry(width, height), Blend);
    imgblend.modifyImage();
    imgblend.type(TrueColorMatteType);
    PixelPacket *pixels = imgblend.getPixels(0, 0, width, height);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            PixelPacket *pixel = pixels + y * width + x;
            int opacity = (maxw / width * x + maxh - maxh / height * y) / 2;
            pixel->opacity = (opacity <= MaxRGB) ? opacity : MaxRGB;
        }
    }
    imgblend.syncPixels();

    Image imgback(Geometry(width, height), Back);
    imgback.composite(imgblend, 0, 0, OverCompositeOp);

    buffer = imgback;
}

void cImageMagickWrapper::CreateBackground(tColor back, tColor blend, int width, int height, bool mirror) {
    CreateGradient(back, blend, width, height, 0.8, 0.8);
    if (mirror)
        buffer.flop();
}
void cImageMagickWrapper::CreateBackgroundReverse(tColor back, tColor blend, int width, int height) {
    CreateGradient(back, blend, width, height, 1.3, 0.7);
}