bitmap_loader_pbm.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* bitmap_loader_pbm.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "bitmap_loader_pbm.h"
  31. #include "os/file_access.h"
  32. #include "scene/resources/bit_mask.h"
  33. static bool _get_token(FileAccessRef &f, uint8_t &saved, DVector<uint8_t> &r_token, bool p_binary = false, bool p_single_chunk = false) {
  34. int token_max = r_token.size();
  35. DVector<uint8_t>::Write w;
  36. if (token_max)
  37. w = r_token.write();
  38. int ofs = 0;
  39. bool lf = false;
  40. while (true) {
  41. uint8_t b;
  42. if (saved) {
  43. b = saved;
  44. saved = 0;
  45. } else {
  46. b = f->get_8();
  47. }
  48. if (f->eof_reached()) {
  49. if (ofs) {
  50. w = DVector<uint8_t>::Write();
  51. r_token.resize(ofs);
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. if (!ofs && !p_binary && b == '#') {
  58. //skip comment
  59. while (b != '\n') {
  60. if (f->eof_reached()) {
  61. return false;
  62. }
  63. b = f->get_8();
  64. }
  65. lf = true;
  66. } else if (b <= 32 && !(p_binary && (ofs || lf))) {
  67. if (b == '\n') {
  68. lf = true;
  69. }
  70. if (ofs && !p_single_chunk) {
  71. w = DVector<uint8_t>::Write();
  72. r_token.resize(ofs);
  73. saved = b;
  74. return true;
  75. }
  76. } else {
  77. bool resized = false;
  78. while (ofs >= token_max) {
  79. if (token_max)
  80. token_max <<= 1;
  81. else
  82. token_max = 1;
  83. resized = true;
  84. }
  85. if (resized) {
  86. w = DVector<uint8_t>::Write();
  87. r_token.resize(token_max);
  88. w = r_token.write();
  89. }
  90. w[ofs++] = b;
  91. }
  92. }
  93. return false;
  94. }
  95. static int _get_number_from_token(DVector<uint8_t> &r_token) {
  96. int len = r_token.size();
  97. DVector<uint8_t>::Read r = r_token.read();
  98. return String::to_int((const char *)r.ptr(), len);
  99. }
  100. RES ResourceFormatPBM::load(const String &p_path, const String &p_original_path, Error *r_error) {
  101. #define _RETURN(m_err) \
  102. { \
  103. if (r_error) \
  104. *r_error = m_err; \
  105. ERR_FAIL_V(RES()); \
  106. }
  107. FileAccessRef f = FileAccess::open(p_path, FileAccess::READ);
  108. uint8_t saved = 0;
  109. if (!f)
  110. _RETURN(ERR_CANT_OPEN);
  111. DVector<uint8_t> token;
  112. if (!_get_token(f, saved, token)) {
  113. _RETURN(ERR_PARSE_ERROR);
  114. }
  115. if (token.size() != 2) {
  116. _RETURN(ERR_FILE_CORRUPT);
  117. }
  118. if (token[0] != 'P') {
  119. _RETURN(ERR_FILE_CORRUPT);
  120. }
  121. if (token[1] != '1' && token[1] != '4') {
  122. _RETURN(ERR_FILE_CORRUPT);
  123. }
  124. bool bits = token[1] == '4';
  125. if (!_get_token(f, saved, token)) {
  126. _RETURN(ERR_PARSE_ERROR);
  127. }
  128. int width = _get_number_from_token(token);
  129. if (width <= 0) {
  130. _RETURN(ERR_FILE_CORRUPT);
  131. }
  132. if (!_get_token(f, saved, token)) {
  133. _RETURN(ERR_PARSE_ERROR);
  134. }
  135. int height = _get_number_from_token(token);
  136. if (height <= 0) {
  137. _RETURN(ERR_FILE_CORRUPT);
  138. }
  139. Ref<BitMap> bm;
  140. bm.instance();
  141. bm->create(Size2i(width, height));
  142. if (!bits) {
  143. int required_bytes = width * height;
  144. if (!_get_token(f, saved, token, false, true)) {
  145. _RETURN(ERR_PARSE_ERROR);
  146. }
  147. if (token.size() < required_bytes) {
  148. _RETURN(ERR_FILE_CORRUPT);
  149. }
  150. DVector<uint8_t>::Read r = token.read();
  151. for (int i = 0; i < height; i++) {
  152. for (int j = 0; j < width; j++) {
  153. char num = r[i * width + j];
  154. bm->set_bit(Point2i(j, i), num == '0');
  155. }
  156. }
  157. } else {
  158. //a single, entire token of bits!
  159. if (!_get_token(f, saved, token, true)) {
  160. _RETURN(ERR_PARSE_ERROR);
  161. }
  162. int required_bytes = Math::ceil((width * height) / 8.0);
  163. if (token.size() < required_bytes) {
  164. _RETURN(ERR_FILE_CORRUPT);
  165. }
  166. DVector<uint8_t>::Read r = token.read();
  167. int bitwidth = width;
  168. if (bitwidth % 8)
  169. bitwidth += 8 - (bitwidth % 8);
  170. for (int i = 0; i < height; i++) {
  171. for (int j = 0; j < width; j++) {
  172. int ofs = bitwidth * i + j;
  173. uint8_t byte = r[ofs / 8];
  174. bool bit = (byte >> (7 - (ofs % 8))) & 1;
  175. bm->set_bit(Point2i(j, i), !bit);
  176. }
  177. }
  178. }
  179. return bm;
  180. }
  181. void ResourceFormatPBM::get_recognized_extensions(List<String> *p_extensions) const {
  182. p_extensions->push_back("pbm");
  183. }
  184. bool ResourceFormatPBM::handles_type(const String &p_type) const {
  185. return p_type == "BitMap";
  186. }
  187. String ResourceFormatPBM::get_resource_type(const String &p_path) const {
  188. if (p_path.extension().to_lower() == "pbm")
  189. return "BitMap";
  190. return "";
  191. }