Subversion Repositories Kolibri OS

Rev

Rev 8382 | Rev 8396 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3107 leency 1
//Asper
5598 pavelyakov 2
#ifndef INCLUDE_LIBIMG_H
3
#define INCLUDE_LIBIMG_H
3107 leency 4
 
5598 pavelyakov 5
#ifndef INCLUDE_KOLIBRI_H
6
#include "../lib/kolibri.h"
7
#endif
8
 
9
#ifndef INCLUDE_MEM_H
10
#include "../lib/mem.h"
11
#endif
12
 
13
#ifndef INCLUDE_DLL_H
14
#include "../lib/dll.h"
15
#endif
16
 
3107 leency 17
//library
18
dword libimg = #alibimg;
7252 leency 19
char alibimg[] = "/sys/lib/libimg.obj";
5626 leency 20
 
8381 leency 21
dword libimg_init   = #alibimg_init;
22
dword img_is_img    = #aimg_is_img;
23
dword img_to_rgb2   = #aimg_to_rgb2;
24
dword img_decode    = #aimg_decode;
25
dword img_destroy   = #aimg_destroy;
26
dword img_draw      = #aimg_draw;
27
dword img_create    = #aimg_create;
28
dword img_encode    = #aimg_encode;
29
dword img_convert   = #aimg_convert;
30
dword img_from_file = #aimg_from_file;
7190 leency 31
 
3107 leency 32
//dword img_flip    = #aimg_flip;
33
//dword img_rotate  = #aimg_rotate;
3839 Asper 34
$DD 2 dup 0
3107 leency 35
 
36
//import  libimg                     , \
8381 leency 37
char alibimg_init[]   = "lib_init";
38
char aimg_is_img[]    = "img_is_img";
39
char aimg_to_rgb2[]   = "img_to_rgb2";
40
char aimg_decode[]    = "img_decode";
41
char aimg_destroy[]   = "img_destroy";
42
char aimg_draw[]      = "img_draw";
43
char aimg_create[]    = "img_create";
44
char aimg_encode[]    = "img_encode";
45
char aimg_convert[]   = "img_convert";
46
char aimg_from_file[] = "img_from_file";
7252 leency 47
//char aimg_flip[]    = "img_flip";
48
//char aimg_rotate[]  = "img_rotate ";
3107 leency 49
 
7977 leency 50
//invoke  img.scale, ebx, 0, 0, [ebx + Image.Width], [ebx + Image.Height], 0, LIBIMG_SCALE_TYPE_STRETCH, LIBIMG_SCALE_ALG_BILINEAR, edx, ecx
51
 
7190 leency 52
#define LIBIMG_FORMAT_BMP       1
53
#define LIBIMG_FORMAT_ICO       2
54
#define LIBIMG_FORMAT_CUR       3
55
#define LIBIMG_FORMAT_GIF       4
56
#define LIBIMG_FORMAT_PNG       5
57
#define LIBIMG_FORMAT_JPEG      6
58
#define LIBIMG_FORMAT_TGA       7
59
#define LIBIMG_FORMAT_PCX       8
60
#define LIBIMG_FORMAT_XCF       9
61
#define LIBIMG_FORMAT_TIFF     10
62
#define LIBIMG_FORMAT_PNM      11
63
#define LIBIMG_FORMAT_WBMP     12
64
#define LIBIMG_FORMAT_XBM      13
65
#define LIBIMG_FORMAT_Z80      14
3107 leency 66
 
7190 leency 67
// values for Image.Type
68
// must be consecutive to allow fast switch on Image.Type in support functions
7995 leency 69
#define IMAGE_BPP8i  1  // indexed
70
#define IMAGE_BPP24  2
71
#define IMAGE_BPP32  3
72
#define IMAGE_BPP15  4
73
#define IMAGE_BPP16  5
74
#define IMAGE_BPP1   6
75
#define IMAGE_BPP8g  7  // grayscale
76
#define IMAGE_BPP2i  8
77
#define IMAGE_BPP4i  9
78
#define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
7254 leency 79
                        // kernel doesn't handle this image type,
80
                        // libimg can only create and destroy such images
7190 leency 81
 
7995 leency 82
struct libimg_image
83
{
84
    dword checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
85
    dword w;
86
    dword h;
87
    dword next;
88
    dword previous;
89
    dword type;     // one of Image.bppN
90
    dword imgsrc;
91
    dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
92
    dword extended;
93
    dword flags;    // bitfield
94
    dword delay;    // used iff Image.IsAnimated is set in Flags
95
    dword image;
96
    void load();
97
    void convert_into();
98
    void replace_color();
99
    void set_vars();
100
    void draw();
101
};
7190 leency 102
 
7995 leency 103
:void libimg_image::set_vars()
104
{
105
    $push edi
106
    EDI = image;
107
    checksum = DSWORD[EDI];
108
    w = ESDWORD[EDI+4];
109
    h = ESDWORD[EDI+8];
110
    next = ESDWORD[EDI+12];
111
    previous = ESDWORD[EDI+16];
112
    imgsrc = ESDWORD[EDI+24];
113
    palette = ESDWORD[EDI+28];
114
    extended = ESDWORD[EDI+32];
115
    flags = ESDWORD[EDI+36];
116
    delay = ESDWORD[EDI+40];
117
    $pop edi
118
}
119
 
120
:void libimg_image::load(dword file_path)
121
{
8389 leency 122
    if (image) img_destroy stdcall(image);
123
    img_from_file stdcall(file_path);
7995 leency 124
    if (!EAX) {
125
        notify("'Error: Image not loaded'E");
126
    } else {
127
        image = EAX;
128
        set_vars();
129
    }
130
}
131
 
132
:void libimg_image::replace_color(dword old_color, new_color)
133
{
134
    EDX =  w * h * 4 + imgsrc;
135
    for (ESI = imgsrc; ESI < EDX; ESI += 4) if (DSDWORD[ESI]==old_color) DSDWORD[ESI] = new_color;
136
}
137
 
138
:void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
139
{
140
    if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
141
}
142
 
143
:void libimg_image::convert_into(dword _to)
144
{
145
    img_convert stdcall(image, 0, _to, 0, 0);
146
    if (!EAX) {
147
        notify("'LibImg convertation error!'E");
148
    } else {
149
        image = EAX;
150
        set_vars();
151
    }
152
}
153
 
7224 leency 154
:dword create_image(dword type, dword width, dword height) {
155
    img_create stdcall(width, height, type);
156
    return EAX;
157
}
158
 
159
// size - output parameter, error code / the size of encoded data
160
:dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
161
    img_encode stdcall(image_ptr, options, specific_options);
162
    ESDWORD[size] = ECX;
163
 
164
    return EAX;
165
}
166
 
7867 leency 167
#ifdef LANG_RUS
7893 leency 168
#define TEXT_FILE_SAVED_AS "'Файл сохранен как "
169
#else
7867 leency 170
#define TEXT_FILE_SAVED_AS "'File saved as "
171
#endif
7254 leency 172
:void save_image(dword _image_pointer, _w, _h, _path)
7224 leency 173
{
174
    char save_success_message[4096+200];
175
    dword encoded_data=0;
176
    dword encoded_size=0;
177
    dword image_ptr = 0;
178
 
7995 leency 179
    image_ptr = create_image(IMAGE_BPP24, _w, _h);
7190 leency 180
 
7224 leency 181
    if (image_ptr == 0) {
182
        notify("'Error saving file, probably not enought memory!' -E");
183
    }
184
    else {
185
        EDI = image_ptr;
7995 leency 186
        memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
7224 leency 187
 
188
        encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
189
 
190
        img_destroy stdcall(image_ptr);
191
 
192
        if(encoded_data == 0) {
193
            notify("'Error saving file, incorrect data!' -E");
194
        }
195
        else {
7227 leency 196
            if (CreateFile(encoded_size, encoded_data, _path) == 0) {
7867 leency 197
                strcpy(#save_success_message, TEXT_FILE_SAVED_AS);
7783 leency 198
                strcat(#save_success_message, _path);
199
                strcat(#save_success_message, "' -O");
7224 leency 200
                notify(#save_success_message);
201
            }
202
            else {
7783 leency 203
                notify("'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E");
7224 leency 204
            }
205
        }
206
    }
7190 leency 207
}
208
 
7995 leency 209
 
210
 
211
/////////////////////////////
212
/*
213
//  DRAW ICON PATTERN / TEMP
214
*/
215
/////////////////////////////
216
 
217
:void DrawIcon32(dword x,y, _bg, icon_n) {
218
    static dword bg;
8381 leency 219
    static dword pure_img32;
220
    if (!pure_img32) || (bg!=_bg) {
7995 leency 221
        bg = _bg;
8382 leency 222
        if (pure_img32) img_destroy stdcall(pure_img32);
8381 leency 223
        img_from_file stdcall("/sys/icons32.png");
224
        pure_img32 = EAX;
225
        //now fill transparent with another color
226
        EDX = ESDWORD[EAX+4] * ESDWORD[EAX+8] * 4 + ESDWORD[EAX+24];
227
        for (ESI = ESDWORD[EAX+24]; ESI < EDX; ESI += 4) {
228
            if (DSDWORD[ESI]==0x00000000) DSDWORD[ESI] = bg;
229
        }
7995 leency 230
    }
8381 leency 231
    img_draw stdcall(pure_img32, x, y, 32, 32, 0, icon_n*32);
7977 leency 232
}
233
 
8381 leency 234
:void DrawIcon16(dword x,y, _bg, icon_n) {
235
    static dword bg;
236
    static dword pure_img16;
237
    dword bgshadow;
238
    if (!pure_img16) || (bg!=_bg) {
239
        bg = _bg;
240
        bgshadow = MixColors(bg, 0, 220);
8382 leency 241
        if (pure_img16) img_destroy stdcall(pure_img16);
8381 leency 242
        img_from_file stdcall("/sys/icons16.png");
243
        pure_img16 = EAX;
244
        //now fill transparent with another color
245
        EDX = ESDWORD[EAX+4] * ESDWORD[EAX+8] * 4 + ESDWORD[EAX+24];
246
        for (ESI = ESDWORD[EAX+24]; ESI < EDX; ESI += 4) {
247
            if (DSDWORD[ESI]==0xffFFFfff) DSDWORD[ESI] = bg;
248
            if (DSDWORD[ESI]==0xffCACBD6) DSDWORD[ESI] = bgshadow;
249
        }
7995 leency 250
    }
8381 leency 251
    img_draw stdcall(pure_img16, x, y, 16, 16, 0, icon_n*16);
7995 leency 252
}
7224 leency 253
 
5598 pavelyakov 254
#endif