Subversion Repositories Kolibri OS

Rev

Rev 7977 | Rev 8013 | 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
 
17
#ifndef INCLUDE_LIBIO_H
7049 leency 18
#include "../lib/obj/libio.h"
5598 pavelyakov 19
#endif
20
 
3107 leency 21
//library
22
dword libimg = #alibimg;
7252 leency 23
char alibimg[] = "/sys/lib/libimg.obj";
5626 leency 24
 
3107 leency 25
dword libimg_init = #alibimg_init;
26
dword img_is_img  = #aimg_is_img;
27
dword img_to_rgb2 = #aimg_to_rgb2;
28
dword img_decode  = #aimg_decode;
29
dword img_destroy = #aimg_destroy;
30
dword img_draw    = #aimg_draw;
7229 leency 31
dword img_create  = #aimg_create;
32
dword img_encode  = #aimg_encode;
7977 leency 33
dword img_convert = #aimg_convert;
7190 leency 34
 
3107 leency 35
//dword img_flip    = #aimg_flip;
36
//dword img_rotate  = #aimg_rotate;
3839 Asper 37
$DD 2 dup 0
3107 leency 38
 
39
//import  libimg                     , \
7252 leency 40
char alibimg_init[] = "lib_init";
41
char aimg_is_img[]  = "img_is_img";
42
char aimg_to_rgb2[] = "img_to_rgb2";
43
char aimg_decode[]  = "img_decode";
44
char aimg_destroy[] = "img_destroy";
45
char aimg_draw[]    = "img_draw";
7977 leency 46
char aimg_create[]  = "img_create";
47
char aimg_encode[]  = "img_encode";
48
char aimg_convert[] = "img_convert";
7252 leency 49
//char aimg_flip[]    = "img_flip";
50
//char aimg_rotate[]  = "img_rotate ";
3107 leency 51
 
7977 leency 52
//invoke  img.scale, ebx, 0, 0, [ebx + Image.Width], [ebx + Image.Height], 0, LIBIMG_SCALE_TYPE_STRETCH, LIBIMG_SCALE_ALG_BILINEAR, edx, ecx
53
 
7190 leency 54
#define LIBIMG_FORMAT_BMP       1
55
#define LIBIMG_FORMAT_ICO       2
56
#define LIBIMG_FORMAT_CUR       3
57
#define LIBIMG_FORMAT_GIF       4
58
#define LIBIMG_FORMAT_PNG       5
59
#define LIBIMG_FORMAT_JPEG      6
60
#define LIBIMG_FORMAT_TGA       7
61
#define LIBIMG_FORMAT_PCX       8
62
#define LIBIMG_FORMAT_XCF       9
63
#define LIBIMG_FORMAT_TIFF     10
64
#define LIBIMG_FORMAT_PNM      11
65
#define LIBIMG_FORMAT_WBMP     12
66
#define LIBIMG_FORMAT_XBM      13
67
#define LIBIMG_FORMAT_Z80      14
3107 leency 68
 
7190 leency 69
// values for Image.Type
70
// must be consecutive to allow fast switch on Image.Type in support functions
7995 leency 71
#define IMAGE_BPP8i  1  // indexed
72
#define IMAGE_BPP24  2
73
#define IMAGE_BPP32  3
74
#define IMAGE_BPP15  4
75
#define IMAGE_BPP16  5
76
#define IMAGE_BPP1   6
77
#define IMAGE_BPP8g  7  // grayscale
78
#define IMAGE_BPP2i  8
79
#define IMAGE_BPP4i  9
80
#define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
7254 leency 81
                        // kernel doesn't handle this image type,
82
                        // libimg can only create and destroy such images
7190 leency 83
 
7995 leency 84
struct libimg_image
85
{
86
    dword checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
87
    dword w;
88
    dword h;
89
    dword next;
90
    dword previous;
91
    dword type;     // one of Image.bppN
92
    dword imgsrc;
93
    dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
94
    dword extended;
95
    dword flags;    // bitfield
96
    dword delay;    // used iff Image.IsAnimated is set in Flags
97
    dword image;
98
    void load();
99
    void convert_into();
100
    void replace_color();
101
    void set_vars();
102
    void draw();
103
};
7190 leency 104
 
7995 leency 105
:void libimg_image::set_vars()
106
{
107
    $push edi
108
    EDI = image;
109
    checksum = DSWORD[EDI];
110
    w = ESDWORD[EDI+4];
111
    h = ESDWORD[EDI+8];
112
    next = ESDWORD[EDI+12];
113
    previous = ESDWORD[EDI+16];
114
    imgsrc = ESDWORD[EDI+24];
115
    palette = ESDWORD[EDI+28];
116
    extended = ESDWORD[EDI+32];
117
    flags = ESDWORD[EDI+36];
118
    delay = ESDWORD[EDI+40];
119
    $pop edi
120
}
121
 
122
:void libimg_image::load(dword file_path)
123
{
124
    load_image(file_path);
125
    if (!EAX) {
126
        notify("'Error: Image not loaded'E");
127
    } else {
128
        image = EAX;
129
        set_vars();
130
    }
131
}
132
 
133
:void libimg_image::replace_color(dword old_color, new_color)
134
{
135
    EDX =  w * h * 4 + imgsrc;
136
    for (ESI = imgsrc; ESI < EDX; ESI += 4) if (DSDWORD[ESI]==old_color) DSDWORD[ESI] = new_color;
137
}
138
 
139
:void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
140
{
141
    if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
142
}
143
 
144
:void libimg_image::convert_into(dword _to)
145
{
146
    img_convert stdcall(image, 0, _to, 0, 0);
147
    if (!EAX) {
148
        notify("'LibImg convertation error!'E");
149
    } else {
150
        image = EAX;
151
        set_vars();
152
    }
153
}
154
 
7224 leency 155
:dword load_image(dword filename)
3107 leency 156
{
157
        //align 4
158
        dword img_data=0;
159
        dword img_data_len=0;
160
        dword fh=0;
161
        dword image=0;
162
 
163
        byte tmp_buf[40];
164
        $and     img_data, 0
165
        //$mov     eax, filename
166
        //$push    eax
167
        //invoke  file.open, eax, O_READ
168
        file_open stdcall (filename, O_READ);
169
        $or      eax, eax
170
        $jnz      loc05
171
        $stc
172
        return 0;
173
    @loc05:
174
        $mov     fh, eax
175
        //invoke  file.size
176
        file_size stdcall (filename);
177
        $mov     img_data_len, ebx
178
        //stdcall mem.Alloc, ebx
179
        mem_Alloc(EBX);
180
 
181
        $test    eax, eax
182
        $jz      error_close
183
        $mov     img_data, eax
184
        //invoke  file.read, [fh], eax, [img_data_len]
185
        file_read stdcall (fh, EAX, img_data_len);
186
        $cmp     eax, -1
187
        $jz      error_close
188
        $cmp     eax, img_data_len
189
        $jnz     error_close
190
        //invoke  file.close, [fh]
191
        file_close stdcall (fh);
192
        $inc     eax
193
        $jz      error_
194
//; img.decode checks for img.is_img
195
//;       //invoke  img.is_img, [img_data], [img_data_len]
196
//;       $or      eax, eax
197
//;       $jz      exit
198
        //invoke  img.decode, [img_data], [img_data_len], 0
199
        EAX=img_data;
200
        img_decode stdcall (EAX, img_data_len,0);
201
        $or      eax, eax
202
        $jz      error_
203
        $cmp     image, 0
204
        $pushf
205
        $mov     image, eax
206
        //call    init_frame
207
        $popf
208
        //call    update_image_sizes
209
        mem_Free(img_data);//free_img_data(img_data);
210
        $clc
211
        return image;
212
 
213
@error_free:
214
        //invoke  img.destroy, [image]
215
        img_destroy stdcall (image);
216
        $jmp     error_
217
 
218
@error_pop:
219
        $pop     eax
220
        $jmp     error_
221
@error_close:
222
        //invoke  file.close, [fh]
223
        file_close stdcall (fh);
224
@error_:
225
        mem_Free(img_data);
226
        $stc
227
        return 0;
228
}
5598 pavelyakov 229
 
7224 leency 230
:dword create_image(dword type, dword width, dword height) {
231
    img_create stdcall(width, height, type);
232
    return EAX;
233
}
234
 
235
// size - output parameter, error code / the size of encoded data
236
:dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
237
    img_encode stdcall(image_ptr, options, specific_options);
238
    ESDWORD[size] = ECX;
239
 
240
    return EAX;
241
}
242
 
7254 leency 243
//NOTICE: DO NOT FORGET TO INIT libio AND libimg!!!
7867 leency 244
#ifdef LANG_RUS
7893 leency 245
#define TEXT_FILE_SAVED_AS "'Файл сохранен как "
246
#else
7867 leency 247
#define TEXT_FILE_SAVED_AS "'File saved as "
248
#endif
7254 leency 249
:void save_image(dword _image_pointer, _w, _h, _path)
7224 leency 250
{
251
    char save_success_message[4096+200];
252
    dword encoded_data=0;
253
    dword encoded_size=0;
254
    dword image_ptr = 0;
255
 
7995 leency 256
    image_ptr = create_image(IMAGE_BPP24, _w, _h);
7190 leency 257
 
7224 leency 258
    if (image_ptr == 0) {
259
        notify("'Error saving file, probably not enought memory!' -E");
260
    }
261
    else {
262
        EDI = image_ptr;
7995 leency 263
        memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
7224 leency 264
 
265
        encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
266
 
267
        img_destroy stdcall(image_ptr);
268
 
269
        if(encoded_data == 0) {
270
            notify("'Error saving file, incorrect data!' -E");
271
        }
272
        else {
7227 leency 273
            if (CreateFile(encoded_size, encoded_data, _path) == 0) {
7867 leency 274
                strcpy(#save_success_message, TEXT_FILE_SAVED_AS);
7783 leency 275
                strcat(#save_success_message, _path);
276
                strcat(#save_success_message, "' -O");
7224 leency 277
                notify(#save_success_message);
278
            }
279
            else {
7783 leency 280
                notify("'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E");
7224 leency 281
            }
282
        }
283
    }
7190 leency 284
}
285
 
7995 leency 286
 
287
 
288
/////////////////////////////
289
/*
290
//  DRAW ICON PATTERN / TEMP
291
*/
292
/////////////////////////////
293
 
294
:void DrawIcon32(dword x,y, _bg, icon_n) {
295
    static libimg_image i32;
296
    static dword bg;
297
    //load_dll(libimg, #libimg_init,1);
298
    if (!i32.image) || (bg!=_bg) {
299
        bg = _bg;
300
        i32.load("/sys/icons32.png");
301
        i32.replace_color(0x00000000, bg);
302
        debugln("wolo");
303
    }
304
    if (icon_n>=0) i32.draw(x, y, 32, 32, 0, icon_n*32);
7977 leency 305
}
306
 
7995 leency 307
:void DrawIcon16(dword x,y, bg, icon_n) {
308
    static libimg_image i16;
309
    //load_dll(libimg, #libimg_init,1);
310
    if (!i16.image) {
311
        i16.load("/sys/icons16.png");
312
        i16.replace_color(0xffFFFfff, bg);
313
        i16.replace_color(0xffCACBD6, MixColors(bg, 0, 220));
314
    }
315
    if (icon_n>=0) i16.draw(x, y, 16, 16, 0, icon_n*16);
316
}
7224 leency 317
 
7229 leency 318
 
5598 pavelyakov 319
#endif