summaryrefslogtreecommitdiffstats
path: root/kernel/ksquirrel-libs/fmt_utils.cpp
blob: 306fee9a7c48ab348c6f2bb906c263a8c5188935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2004 Dmitry Baryshev <ksquirrel@tut.by>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later
    version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>

#include "ksquirrel-libs/fmt_utils.h"
#include "ksquirrel-libs/fmt_defs.h"

#include <sstream>

void fmt_utils::fillAlpha(RGBA *scan, int w, u8 value)
{
    if(scan)
    {        
        for(int i = 0;i < w;i++)
            (scan + i)->a = value;
    }
}

void fmt_utils::flipv(s8 *image, s32 bytes_w, s32 h)
{
    s32 i;
    
    if(!image)
	    return;

    s8 *hptr = new s8 [bytes_w];

    if(!hptr)
	    return;

    for(i = 0; i < h/2; i++)
    {
        memcpy(hptr, image + i * bytes_w, bytes_w);
        memcpy(image + i * bytes_w, image + (h - i - 1) * bytes_w, bytes_w);
        memcpy(image + (h - i - 1) * bytes_w, hptr, bytes_w);
    }

    delete hptr;
}

void fmt_utils::fliph(s8 *image, s32 w, s32 h, s32 bpp)
{
    s32 x, y, x2;
    s32 bpl = w * bpp;
    s8 a[bpp], *t;

    if(!image)
	    return;

    for(y = 0;y < h;y++)
    {
	    for(x = 0, x2 = w-1;x < w/2;x++,x2--)
	    {
	        t = image + y*bpl;

	        memcpy(a, t + x2*bpp, bpp);
	        memcpy(t + x2*bpp, t + x*bpp, bpp);
	        memcpy(t + x*bpp, a, bpp);
	    }
    }
}

u16 fmt_utils::konvertWord(u16 a)
{
    u16 i = a, b;

    b = i & 255;
    b = b << 8;
    b = b|0;
    i = i >> 8;
    i = i|0;

    return i|b;
}

u32 fmt_utils::konvertLong(u32 a)
{
    u32 i = a, b, c;
    u16 m, n;

    b = i >> 16; // high word
    c = i << 16;
    c = c >> 16; // low word

    m = (u16)b;
    n = (u16)c;

    m = fmt_utils::konvertWord(m);
    n = fmt_utils::konvertWord(n);

    b = m;
    c = n;
    c = c << 16;

    b = b|0;
    c = c|0;

    return b|c;
}

std::string fmt_utils::colorSpaceByBpp(const int bpp)
{
    switch(bpp)
    {
	    case 1:
	        return std::string("Monochrome");

    	case 4:
    	case 8:
    	case 15:
    	case 16:
	        return std::string("Color indexed");

    	case 24:
	        return std::string("RGB");

    	case 32:
	        return std::string("RGBA");

    	default:
	        return std::string("Unknown");
    }
}

void fmt_utils::expandMono1Byte(const u32 byte, u8 *array)
{
    u8 mask = 0x80;
    u8 *p = array;
    u8 b = byte & 0xff; 

    for(s32 i = 0;i < 8;i++)
    {
    	*p = (b & mask) ? 1 : 0;

	    p++;
    	mask >>= 1;
    }
}

void fmt_utils::expandMono2Byte(const u32 byte, u8 *array)
{
    u16 mask = 0x8000;
    u8 *p = array;
    u16 b = byte & 0xffff; 

    for(s32 i = 0;i < 16;i++)
    {
    	*p = (b & mask) ? 1 : 0;

	    p++;
    	mask >>= 1;
    }
}

void fmt_utils::expandMono4Byte(const u32 byte, u8 *array)
{
    u32 mask = 0x80000000;
    u8 *p = array;
    u32 b = byte & 0xffffffff; 

    for(s32 i = 0;i < 32;i++)
    {
    	*p = (b & mask) ? 1 : 0;

	    p++;
    	mask >>= 1;
    }
}