Subversion Repositories Kolibri OS

Rev

Rev 7995 | Rev 8381 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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