1 /* Image library
2  *
3  * Copyright (c) 2016 Max Stepin
4  * maxst at users.sourceforge.net
5  *
6  * zlib license
7  * ------------
8  *
9  * This software is provided 'as-is', without any express or implied
10  * warranty.  In no event will the authors be held liable for any damages
11  * arising from the use of this software.
12  *
13  * Permission is granted to anyone to use this software for any purpose,
14  * including commercial applications, and to alter it and redistribute it
15  * freely, subject to the following restrictions:
16  *
17  * 1. The origin of this software must not be misrepresented; you must not
18  *    claim that you wrote the original software. If you use this software
19  *    in a product, an acknowledgment in the product documentation would be
20  *    appreciated but is not required.
21  * 2. Altered source versions must be plainly marked as such, and must not be
22  *    misrepresented as being the original software.
23  * 3. This notice may not be removed or altered from any source distribution.
24  *
25  */
26 #ifndef IMAGE_H
27 #define IMAGE_H
28 #include <string.h>
29 #include <vector>
30 
31 struct rgb { unsigned char r, g, b; };
32 
33 struct Image
34 {
35   typedef unsigned char * ROW;
36   unsigned int w, h, bpp, type;
37   int ps, ts;
38   rgb pl[256];
39   unsigned char tr[256];
40   unsigned int delay_num, delay_den;
41   unsigned char * p;
42   ROW * rows;
ImageImage43   Image() : w(0), h(0), bpp(0), type(0), ps(0), ts(0), delay_num(1), delay_den(10), p(0), rows(0)
44   {
45     memset(pl, 255, sizeof(pl));
46     memset(tr, 255, sizeof(tr));
47   }
~ImageImage48   ~Image() { }
initImage49   void init(unsigned int w1, unsigned int h1, unsigned int bpp1, unsigned int type1)
50   {
51     w = w1; h = h1; bpp = bpp1; type = type1;
52     int rowbytes = w * bpp;
53     delete[] rows; delete[] p;
54     rows = new ROW[h];
55     rows[0] = p = new unsigned char[h * rowbytes];
56     for (unsigned int j=1; j<h; j++)
57       rows[j] = rows[j-1] + rowbytes;
58   }
initImage59   void init(unsigned int w, unsigned int h, Image * image)
60   {
61     init(w, h, image->bpp, image->type);
62     if ((ps = image->ps) != 0) memcpy(&pl[0], &image->pl[0], ps*3);
63     if ((ts = image->ts) != 0) memcpy(&tr[0], &image->tr[0], ts);
64   }
initImage65   void init(Image * image) { init(image->w, image->h, image); }
freeImage66   void free() { delete[] rows; delete[] p; }
67 };
68 
69 int load_image(char * szName, Image * image);
70 unsigned char find_common_coltype(std::vector<Image>& img);
71 void optim_upconvert(Image * image, unsigned char coltype);
72 void optim_duplicates(std::vector<Image>& img, unsigned int first);
73 void optim_dirty_transp(Image * image);
74 void optim_downconvert(std::vector<Image>& img);
75 void optim_palette(std::vector<Image>& img);
76 void optim_add_transp(std::vector<Image>& img);
77 
78 #endif /* IMAGE_H */
79