summaryrefslogtreecommitdiff
path: root/modules/pbm/bitmap_loader_pbm.cpp
blob: ab0805a6b018afb22a3b35ab82a5f69339acbe6a (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*************************************************************************/
/*  bitmap_loader_pbm.cpp                                                */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "bitmap_loader_pbm.h"
#include "os/file_access.h"
#include "scene/resources/bit_mask.h"


static bool _get_token(FileAccessRef& f,uint8_t &saved,PoolVector<uint8_t>& r_token,bool p_binary=false,bool p_single_chunk=false) {


	int token_max = r_token.size();
	PoolVector<uint8_t>::Write w;
	if (token_max)
		w=r_token.write();
	int ofs=0;
	bool lf=false;


	while(true) {

		uint8_t b;
		if (saved) {
			b=saved;
			saved=0;
		} else {
			b = f->get_8();
		}
		if (f->eof_reached()) {
			if (ofs) {
				w=PoolVector<uint8_t>::Write();
				r_token.resize(ofs);
				return true;
			} else {
				return false;
			}
		}

		if (!ofs && !p_binary && b=='#') {
			//skip comment
			while(b!='\n') {
				if (f->eof_reached()) {
					return false;
				}

				b = f->get_8();
			}

			lf=true;

		} else if (b<=32 && !(p_binary && (ofs || lf))) {

			if (b=='\n') {
				lf=true;
			}


			if (ofs && !p_single_chunk) {
				w=PoolVector<uint8_t>::Write();
				r_token.resize(ofs);
				saved=b;

				return true;
			}
		} else {

			bool resized=false;
			while (ofs>=token_max) {
				if (token_max)
					token_max<<=1;
				else
					token_max=1;
				resized=true;
			}
			if (resized) {
				w=PoolVector<uint8_t>::Write();
				r_token.resize(token_max);
				w=r_token.write();
			}
			w[ofs++]=b;
		}
	}

	return false;
}

static int _get_number_from_token(PoolVector<uint8_t>& r_token) {

	int len = r_token.size();
	PoolVector<uint8_t>::Read r = r_token.read();
	return String::to_int((const char*)r.ptr(),len);

}


RES ResourceFormatPBM::load(const String &p_path,const String& p_original_path,Error *r_error) {

#define _RETURN(m_err)\
{\
	if (r_error)\
		*r_error=m_err;\
	ERR_FAIL_V(RES());\
}


	FileAccessRef f=FileAccess::open(p_path,FileAccess::READ);
	uint8_t saved=0;
	if (!f)
		_RETURN(ERR_CANT_OPEN);

	PoolVector<uint8_t> token;

	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	if (token.size()!=2) {
		_RETURN(ERR_FILE_CORRUPT);
	}
	if (token[0]!='P') {
		_RETURN(ERR_FILE_CORRUPT);
	}
	if (token[1]!='1' && token[1]!='4') {
		_RETURN(ERR_FILE_CORRUPT);
	}

	bool bits = token[1]=='4';

	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	int width = _get_number_from_token(token);
	if (width<=0) {
		_RETURN(ERR_FILE_CORRUPT);
	}


	if (!_get_token(f,saved,token)) {
		_RETURN(ERR_PARSE_ERROR);
	}

	int height = _get_number_from_token(token);
	if (height<=0) {
		_RETURN(ERR_FILE_CORRUPT);
	}


	Ref<BitMap> bm;
	bm.instance();
	bm->create(Size2i(width,height));

	if (!bits) {

		int required_bytes = width*height;
		if (!_get_token(f,saved,token,false,true)) {
			_RETURN(ERR_PARSE_ERROR);
		}

		if (token.size()<required_bytes) {
			_RETURN(ERR_FILE_CORRUPT);
		}

		PoolVector<uint8_t>::Read r=token.read();
			
		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {


				char num = r[i*width+j];
				bm->set_bit(Point2i(j,i),num=='0');
			}

		}



	} else {
		//a single, entire token of bits!
		if (!_get_token(f,saved,token,true)) {
			_RETURN(ERR_PARSE_ERROR);
		}
		int required_bytes = Math::ceil((width*height)/8.0);
		if (token.size()<required_bytes) {
			_RETURN(ERR_FILE_CORRUPT);
		}

		PoolVector<uint8_t>::Read r=token.read();
		int bitwidth = width;
		if (bitwidth % 8)
			bitwidth+=8-(bitwidth%8);

		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {

				int ofs = bitwidth*i+j;

				uint8_t byte = r[ofs/8];
				bool bit = (byte>>(7-(ofs%8)))&1;

				bm->set_bit(Point2i(j,i),!bit);

			}

		}

	}

	return bm;


}

void ResourceFormatPBM::get_recognized_extensions(List<String> *p_extensions) const {
	p_extensions->push_back("pbm");
}
bool ResourceFormatPBM::handles_type(const String& p_type) const {
	return p_type=="BitMap";
}
String ResourceFormatPBM::get_resource_type(const String &p_path) const {

	if (p_path.get_extension().to_lower()=="pbm")
		return "BitMap";
	return "";
}