Subversion Repositories Kolibri OS

Rev

Rev 9643 | 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";
9396 leency 20
 
8381 leency 21
dword libimg_init   = #alibimg_init;
22
dword img_decode    = #aimg_decode;
23
dword img_destroy   = #aimg_destroy;
24
dword img_draw      = #aimg_draw;
25
dword img_create    = #aimg_create;
26
dword img_encode    = #aimg_encode;
27
dword img_convert   = #aimg_convert;
28
dword img_from_file = #aimg_from_file;
8396 leency 29
dword img_blend     = #aimg_blend;
9644 leency 30
dword img_flip      = #aimg_flip;
31
dword img_rotate    = #aimg_rotate;
32
dword img_scale     = #aimg_scale;
8396 leency 33
//dword img_is_img    = #aimg_is_img;
9644 leency 34
//dword img_to_rgb    = #aimg_to_rgb;
8396 leency 35
//dword img_to_rgb2   = #aimg_to_rgb2;
7190 leency 36
 
3839 Asper 37
$DD 2 dup 0
3107 leency 38
 
8396 leency 39
//import
8381 leency 40
char alibimg_init[]   = "lib_init";
41
char aimg_decode[]    = "img_decode";
42
char aimg_destroy[]   = "img_destroy";
43
char aimg_draw[]      = "img_draw";
44
char aimg_create[]    = "img_create";
45
char aimg_encode[]    = "img_encode";
46
char aimg_convert[]   = "img_convert";
47
char aimg_from_file[] = "img_from_file";
8396 leency 48
char aimg_blend[]     = "img_blend";
9644 leency 49
char aimg_flip[]      = "img_flip";
50
char aimg_rotate[]    = "img_rotate";
51
char aimg_scale[]     = "img_scale";
8396 leency 52
//char aimg_is_img[]    = "img_is_img";
9644 leency 53
//char aimg_to_rgb[]    = "img_to_rgb";
8396 leency 54
//char aimg_to_rgb2[]   = "img_to_rgb2";
3107 leency 55
 
7190 leency 56
#define LIBIMG_FORMAT_BMP       1
57
#define LIBIMG_FORMAT_ICO       2
58
#define LIBIMG_FORMAT_CUR       3
59
#define LIBIMG_FORMAT_GIF       4
60
#define LIBIMG_FORMAT_PNG       5
61
#define LIBIMG_FORMAT_JPEG      6
62
#define LIBIMG_FORMAT_TGA       7
63
#define LIBIMG_FORMAT_PCX       8
64
#define LIBIMG_FORMAT_XCF       9
65
#define LIBIMG_FORMAT_TIFF     10
66
#define LIBIMG_FORMAT_PNM      11
67
#define LIBIMG_FORMAT_WBMP     12
68
#define LIBIMG_FORMAT_XBM      13
69
#define LIBIMG_FORMAT_Z80      14
3107 leency 70
 
7190 leency 71
// values for Image.Type
72
// must be consecutive to allow fast switch on Image.Type in support functions
7995 leency 73
#define IMAGE_BPP8i  1  // indexed
74
#define IMAGE_BPP24  2
75
#define IMAGE_BPP32  3
76
#define IMAGE_BPP15  4
77
#define IMAGE_BPP16  5
78
#define IMAGE_BPP1   6
79
#define IMAGE_BPP8g  7  // grayscale
80
#define IMAGE_BPP2i  8
81
#define IMAGE_BPP4i  9
9396 leency 82
#define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
83
                        // kernel doesn't handle this image type,
7254 leency 84
                        // libimg can only create and destroy such images
7190 leency 85
 
9644 leency 86
//flip and rotate
9396 leency 87
#define FLIP_VERTICAL   0x01
88
#define FLIP_HORIZONTAL 0x02
89
#define ROTATE_90_CW    0x01
90
#define ROTATE_180      0x02
91
#define ROTATE_270_CW   0x03
92
#define ROTATE_90_CCW   ROTATE_270_CW
93
#define ROTATE_270_CCW  ROTATE_90_CW
94
 
9644 leency 95
//scale type                    //corresponding img.scale params
96
#define LIBIMG_SCALE_NONE       0     //do not scale
97
#define LIBIMG_SCALE_INTEGER    1     //scale factor, reserved 0
98
#define LIBIMG_SCALE_TILE       2     //new width, new height
99
#define LIBIMG_SCALE_STRETCH    3     //new width, new height
100
#define LIBIMG_SCALE_FIT_BOTH   LIBIMG_SCALE_STRETCH
101
#define LIBIMG_SCALE_FIT_MIN    4     //new width, new height
102
#define LIBIMG_SCALE_FIT_RECT   LIBIMG_SCALE_FIT_MIN
103
#define LIBIMG_SCALE_FIT_WIDTH  5     //new width, new height
104
#define LIBIMG_SCALE_FIT_HEIGHT 6     //new width, new height
105
#define LIBIMG_SCALE_FIT_MAX    7     //new width, new height
106
 
107
//interpolation algorithm
108
#define LIBIMG_INTER_NONE       0     //use it with LIBIMG_SCALE_INTEGER, LIBIMG_SCALE_TILE, etc
109
#define LIBIMG_INTER_BILINEAR   1
110
#define LIBIMG_INTER_DEFAULT    LIBIMG_INTER_BILINEAR
111
 
112
/*
113
// error codes
114
LIBIMG_ERROR_OUT_OF_MEMORY      = 1
115
LIBIMG_ERROR_FORMAT             = 2
116
LIBIMG_ERROR_CONDITIONS         = 3
117
LIBIMG_ERROR_BIT_DEPTH          = 4
118
LIBIMG_ERROR_ENCODER            = 5
119
LIBIMG_ERROR_SRC_TYPE           = 6
120
LIBIMG_ERROR_SCALE              = 7
121
LIBIMG_ERROR_INTER              = 8
122
LIBIMG_ERROR_NOT_INPLEMENTED    = 9
123
LIBIMG_ERROR_INVALID_INPUT      = 10
124
*/
125
 
7995 leency 126
struct libimg_image
127
{
9644 leency 128
    dword checksum;  // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
7995 leency 129
    dword w;
130
    dword h;
131
    dword next;
132
    dword previous;
133
    dword type;     // one of Image.bppN
134
    dword imgsrc;
135
    dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
136
    dword extended;
137
    dword flags;    // bitfield
138
    dword delay;    // used iff Image.IsAnimated is set in Flags
139
    dword image;
140
    void load();
141
    void convert_into();
142
    void replace_color();
8780 leency 143
    void replace_2colors();
7995 leency 144
    void set_vars();
145
    void draw();
146
};
7190 leency 147
 
7995 leency 148
:void libimg_image::set_vars()
149
{
150
    $push edi
151
    EDI = image;
8396 leency 152
    //checksum = ESDWORD[EDI];
7995 leency 153
    w = ESDWORD[EDI+4];
154
    h = ESDWORD[EDI+8];
8396 leency 155
    //next = ESDWORD[EDI+12];
156
    //previous = ESDWORD[EDI+16];
9396 leency 157
    type = ESDWORD[EDI+20];
158
    imgsrc = ESDWORD[EDI+24];
159
    //palette = ESDWORD[EDI+28];
160
    //extended = ESDWORD[EDI+32];
161
    //flags = ESDWORD[EDI+36];
162
    //delay = ESDWORD[EDI+40];
7995 leency 163
    $pop edi
164
}
165
 
166
:void libimg_image::load(dword file_path)
167
{
8389 leency 168
    if (image) img_destroy stdcall(image);
169
    img_from_file stdcall(file_path);
7995 leency 170
    if (!EAX) {
171
        notify("'Error: Image not loaded'E");
172
    } else {
173
        image = EAX;
174
        set_vars();
175
    }
176
}
177
 
178
:void libimg_image::replace_color(dword old_color, new_color)
179
{
180
    EDX =  w * h * 4 + imgsrc;
8780 leency 181
    ESI = old_color;
182
    ECX = new_color;
183
    FOR (EDI = imgsrc; EDI < EDX; EDI += 4) IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
7995 leency 184
}
185
 
8780 leency 186
:void libimg_image::replace_2colors(dword old_color1, new_color1, old_color2, new_color2)
187
{
188
    EDX =  w * h * 4 + imgsrc;
189
    ESI = old_color1;
190
    ECX = new_color1;
191
    EBX = old_color2;
192
    EAX = new_color2;
193
    FOR (EDI = imgsrc; EDI < EDX; EDI += 4) {
194
        IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
195
        ELSE IF (DSDWORD[EDI]==EBX) DSDWORD[EDI] = EAX;
196
    }
197
}
198
 
7995 leency 199
:void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
200
{
201
    if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
202
}
203
 
204
:void libimg_image::convert_into(dword _to)
205
{
206
    img_convert stdcall(image, 0, _to, 0, 0);
207
    if (!EAX) {
208
        notify("'LibImg convertation error!'E");
209
    } else {
9454 leency 210
        $push eax
211
        img_destroy stdcall(image);
212
        $pop eax
7995 leency 213
        image = EAX;
214
        set_vars();
215
    }
216
}
217
 
9016 leency 218
:dword save_image(dword _image_pointer, _w, _h, _path)
7224 leency 219
{
220
    dword encoded_data=0;
221
    dword encoded_size=0;
222
    dword image_ptr = 0;
9396 leency 223
 
9594 leency 224
    img_create stdcall(_w, _h, IMAGE_BPP24);
225
    image_ptr = EAX;
7190 leency 226
 
9016 leency 227
    if (!image_ptr) {
228
        return "Error creating image!";
7224 leency 229
    }
230
    else {
231
        EDI = image_ptr;
7995 leency 232
        memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
7224 leency 233
 
9643 leency 234
        img_encode stdcall(image_ptr, LIBIMG_FORMAT_PNG, 0);
235
        encoded_data = EAX;
236
        encoded_size = ECX;
7224 leency 237
 
238
        img_destroy stdcall(image_ptr);
239
 
9016 leency 240
        if(!encoded_data) {
241
            return "Error encoding image!";
7224 leency 242
        }
243
        else {
9016 leency 244
            if (!CreateFile(encoded_size, encoded_data, _path)) {
245
                return 0;
7224 leency 246
            }
247
            else {
9016 leency 248
                return "'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E";
7224 leency 249
            }
250
        }
251
    }
7190 leency 252
}
253
 
5598 pavelyakov 254
#endif