summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libnds/source/arm9/image.c
blob: 410a37c1c9bde072939f16b999b08237efd5c50f (plain) (blame)
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
/*---------------------------------------------------------------------------------
	$Id$


	Copyright (C) 2005
		Michael Noland (joat)
		Jason Rogers (dovoto)
		Dave Murphy (WinterMute)

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any
	damages arising from the use of this software.

	Permission is granted to anyone to use this software for any
	purpose, including commercial applications, and to alter it and
	redistribute it freely, subject to the following restrictions:

	1.	The origin of this software must not be misrepresented; you
		must not claim that you wrote the original software. If you use
		this software in a product, an acknowledgment in the product
		documentation would be appreciated but is not required.
	2.	Altered source versions must be plainly marked as such, and
		must not be misrepresented as being the original software.
	3.	This notice may not be removed or altered from any source
		distribution.

	$Log$
	Revision 1.7  2007/01/19 14:46:00  wntrmute
	name anonymous structs and unions for -std=c99
	
	Revision 1.6  2005/10/11 05:05:26  dovoto
	Added imageTileData(sImage* img) to allow loading of pcx as sprite data.
	Updated pcx.c to set image bit per pixel field
	
	Revision 1.5  2005/08/30 17:54:45  wntrmute
	only include required headers
	
	Revision 1.4  2005/08/22 08:10:35  wntrmute
	reformatted for consistency

	Revision 1.3  2005/08/11 14:53:00  desktopman
	Added image24to16(sImage* img)

	Revision 1.2  2005/08/01 23:18:22  wntrmute
	adjusted headers for logging

	Revision 1.1  2005/07/27 02:20:52  wntrmute
	add image functions


---------------------------------------------------------------------------------*/
#include <nds/jtypes.h>
#include <nds/arm9/image.h>
#include <nds/dma.h>


#include <malloc.h>

//---------------------------------------------------------------------------------
void image24to16(sImage* img) {
//---------------------------------------------------------------------------------

	int x;
	int y;

	u16* temp = (u16*)malloc(img->height*img->width*2);

	for(y=0;y<img->height;y++)
	{
		for(x=0;x<img->width;x++)
			temp[x+y*img->width]=(1<<15)|RGB15(img->image.data8[x*3+y*img->width*3]>>3, \
			img->image.data8[x*3+y*img->width*3+1]>>3, img->image.data8[x*3+y*img->width*3+2]>>3);
	}

	free(img->image.data8);

	img->bpp=16;
	img->image.data16 = temp;
}

//---------------------------------------------------------------------------------
void image8to16(sImage* img) {
//---------------------------------------------------------------------------------
	int i;

	u16* temp = (u16*)malloc(img->height*img->width*2);

	for(i = 0; i < img->height * img->width; i++)
		temp[i] = img->palette[img->image.data8[i]] | (1<<15);

	free (img->image.data8);
	free (img->palette);

	img->bpp = 16;
	img->image.data16 = temp;
}

//---------------------------------------------------------------------------------
void image8to16trans(sImage* img, u8 transparentColor) {
//---------------------------------------------------------------------------------
	int i;
	u8 c;

	u16* temp = (u16*)malloc(img->height*img->width*2);

	for(i = 0; i < img->height * img->width; i++) {

		c = img->image.data8[i];

		if(c != transparentColor)
			temp[i] = img->palette[c] | (1<<15);
		else
			temp[i] = img->palette[c];
	}

	free (img->image.data8);
	free (img->palette);

	img->bpp = 16;
	img->image.data16 = temp;
}
//---------------------------------------------------------------------------------
void imageTileData(sImage* img) {
//---------------------------------------------------------------------------------
	u32* temp;
	
	int ix, iy, tx, ty;

	int th, tw;

	int i = 0;
	
	//can only tile 8 bit data that is a multiple of 8 in dimention
	if(img->bpp != 8 || (img->height & 3) != 0 || (img->width & 3) != 0) return;

	th = img->height >> 3;
	tw = img->width >> 3;

	//buffer to hold data
	temp = (u32*)malloc(img->height * img->width);	

	for(ty = 0; ty < th; ty++)
		for(tx = 0; tx < tw; tx++)
			for(iy = 0; iy < 8; iy++)
				for(ix = 0; ix < 2; ix++)
					temp[i++] = img->image.data32[ix + tx * 2 + (iy + ty * 8) * tw * 2 ]; 

	free(img->image.data32);
	
	img->image.data32 = temp;
}

//---------------------------------------------------------------------------------
void imageDestroy(sImage* img) {
//---------------------------------------------------------------------------------
	if(img->image.data8) free (img->image.data8);
	if(img->palette && img->bpp == 8) free (img->palette);
}