2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2016-10-11 18:44:26 +00:00
|
|
|
/* bitmap_loader_pbm.cpp */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2016-10-11 18:44:26 +00:00
|
|
|
#include "bitmap_loader_pbm.h"
|
2016-01-03 20:14:28 +00:00
|
|
|
#include "os/file_access.h"
|
|
|
|
#include "scene/resources/bit_mask.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static bool _get_token(FileAccessRef &f, uint8_t &saved, DVector<uint8_t> &r_token, bool p_binary = false, bool p_single_chunk = false) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
int token_max = r_token.size();
|
|
|
|
DVector<uint8_t>::Write w;
|
|
|
|
if (token_max)
|
2017-03-18 23:36:26 +00:00
|
|
|
w = r_token.write();
|
|
|
|
int ofs = 0;
|
|
|
|
bool lf = false;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (true) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
uint8_t b;
|
|
|
|
if (saved) {
|
2017-03-18 23:36:26 +00:00
|
|
|
b = saved;
|
|
|
|
saved = 0;
|
2016-01-03 20:14:28 +00:00
|
|
|
} else {
|
|
|
|
b = f->get_8();
|
|
|
|
}
|
|
|
|
if (f->eof_reached()) {
|
|
|
|
if (ofs) {
|
2017-03-18 23:36:26 +00:00
|
|
|
w = DVector<uint8_t>::Write();
|
2016-01-03 20:14:28 +00:00
|
|
|
r_token.resize(ofs);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!ofs && !p_binary && b == '#') {
|
2016-01-03 20:14:28 +00:00
|
|
|
//skip comment
|
2017-03-18 23:36:26 +00:00
|
|
|
while (b != '\n') {
|
2016-01-03 20:14:28 +00:00
|
|
|
if (f->eof_reached()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = f->get_8();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
lf = true;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (b <= 32 && !(p_binary && (ofs || lf))) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (b == '\n') {
|
|
|
|
lf = true;
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ofs && !p_single_chunk) {
|
2017-03-18 23:36:26 +00:00
|
|
|
w = DVector<uint8_t>::Write();
|
2016-01-03 20:14:28 +00:00
|
|
|
r_token.resize(ofs);
|
2017-03-18 23:36:26 +00:00
|
|
|
saved = b;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool resized = false;
|
|
|
|
while (ofs >= token_max) {
|
2016-01-03 20:14:28 +00:00
|
|
|
if (token_max)
|
2017-03-18 23:36:26 +00:00
|
|
|
token_max <<= 1;
|
2016-01-03 20:14:28 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
token_max = 1;
|
|
|
|
resized = true;
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
if (resized) {
|
2017-03-18 23:36:26 +00:00
|
|
|
w = DVector<uint8_t>::Write();
|
2016-01-03 20:14:28 +00:00
|
|
|
r_token.resize(token_max);
|
2017-03-18 23:36:26 +00:00
|
|
|
w = r_token.write();
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
w[ofs++] = b;
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static int _get_number_from_token(DVector<uint8_t> &r_token) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
int len = r_token.size();
|
|
|
|
DVector<uint8_t>::Read r = r_token.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
return String::to_int((const char *)r.ptr(), len);
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
RES ResourceFormatPBM::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#define _RETURN(m_err) \
|
|
|
|
{ \
|
|
|
|
if (r_error) \
|
|
|
|
*r_error = m_err; \
|
|
|
|
ERR_FAIL_V(RES()); \
|
|
|
|
}
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccessRef f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
uint8_t saved = 0;
|
2016-01-03 20:14:28 +00:00
|
|
|
if (!f)
|
|
|
|
_RETURN(ERR_CANT_OPEN);
|
|
|
|
|
|
|
|
DVector<uint8_t> token;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!_get_token(f, saved, token)) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (token.size() != 2) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (token[0] != 'P') {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (token[1] != '1' && token[1] != '4') {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool bits = token[1] == '4';
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!_get_token(f, saved, token)) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = _get_number_from_token(token);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (width <= 0) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!_get_token(f, saved, token)) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
int height = _get_number_from_token(token);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (height <= 0) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<BitMap> bm;
|
|
|
|
bm.instance();
|
2017-03-18 23:36:26 +00:00
|
|
|
bm->create(Size2i(width, height));
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
if (!bits) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int required_bytes = width * height;
|
|
|
|
if (!_get_token(f, saved, token, false, true)) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (token.size() < required_bytes) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<uint8_t>::Read r = token.read();
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
for (int j = 0; j < width; j++) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char num = r[i * width + j];
|
|
|
|
bm->set_bit(Point2i(j, i), num == '0');
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//a single, entire token of bits!
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!_get_token(f, saved, token, true)) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_PARSE_ERROR);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int required_bytes = Math::ceil((width * height) / 8.0);
|
|
|
|
if (token.size() < required_bytes) {
|
2016-01-03 20:14:28 +00:00
|
|
|
_RETURN(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<uint8_t>::Read r = token.read();
|
2016-01-12 09:19:56 +00:00
|
|
|
int bitwidth = width;
|
|
|
|
if (bitwidth % 8)
|
2017-03-18 23:36:26 +00:00
|
|
|
bitwidth += 8 - (bitwidth % 8);
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
for (int j = 0; j < width; j++) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ofs = bitwidth * i + j;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t byte = r[ofs / 8];
|
|
|
|
bool bit = (byte >> (7 - (ofs % 8))) & 1;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bm->set_bit(Point2i(j, i), !bit);
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatPBM::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("pbm");
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ResourceFormatPBM::handles_type(const String &p_type) const {
|
|
|
|
return p_type == "BitMap";
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
String ResourceFormatPBM::get_resource_type(const String &p_path) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_path.extension().to_lower() == "pbm")
|
2016-01-03 20:14:28 +00:00
|
|
|
return "BitMap";
|
|
|
|
return "";
|
|
|
|
}
|