PersistentImage.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2021 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "PersistentImage.h"
  24. using namespace dsr;
  25. PERSISTENT_DEFINITION(PersistentImage)
  26. static uint8_t readHexaDecimal(const ReadableString &text, int &readFrom) {
  27. uint8_t result = 0u;
  28. for (int i = 0; i < 2; i++) {
  29. result = result << 4;
  30. DsrChar c = text[readFrom];
  31. if (U'0' <= c && c <= U'9') {
  32. result = result | (c - U'0');
  33. } else if (U'a' <= c && c <= U'f') {
  34. result = result | (c - U'a' + 10);
  35. } else if (U'A' <= c && c <= U'F') {
  36. result = result | (c - U'A' + 10);
  37. }
  38. readFrom++;
  39. }
  40. return result;
  41. }
  42. bool PersistentImage::assignValue(const ReadableString &text) {
  43. if (string_caseInsensitiveMatch(text, U"NONE")) {
  44. // Set the handle to null
  45. this->value = OrderedImageRgbaU8();
  46. } else {
  47. // Create an image from the text
  48. int colonIndex = string_findFirst(text, U':');
  49. if (colonIndex == -1) {
  50. printText("Missing colon when creating PersistentImage from text!\n");
  51. return false;
  52. }
  53. ReadableString leftSide = string_before(text, colonIndex);
  54. if (string_caseInsensitiveMatch(leftSide, U"FILE")) {
  55. // Read image from the file path
  56. this->value = image_load_RgbaU8(string_after(text, colonIndex));
  57. } else {
  58. // Read dimensions and a sequence of pixels as hexadecimals
  59. int xIndex = string_findFirst(text, U'x');
  60. if (xIndex == -1 || xIndex > colonIndex) {
  61. printText("Missing x when parsing embedded PersistentImage from text!\n");
  62. return false;
  63. }
  64. int width = string_toInteger(string_before(leftSide, xIndex));
  65. int height = string_toInteger(string_after(leftSide, xIndex));
  66. if (width <= 0 || height <= 0) {
  67. // No pixels found
  68. this->value = OrderedImageRgbaU8();
  69. } else {
  70. this->value = image_create_RgbaU8(width, height);
  71. int readIndex = colonIndex + 1;
  72. for (int y = 0; y < height; y++) {
  73. for (int x = 0; x < width; x++) {
  74. int red = readHexaDecimal(text, readIndex);
  75. int green = readHexaDecimal(text, readIndex);
  76. int blue = readHexaDecimal(text, readIndex);
  77. int alpha = readHexaDecimal(text, readIndex);
  78. image_writePixel(this->value, x, y, ColorRgbaI32(red, green, blue, alpha));
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. static const String hexadecimals = U"0123456789ABCDEF";
  87. static void writeHexaDecimal(String &out, uint8_t value) {
  88. string_appendChar(out, hexadecimals[(value & 0b11110000) >> 4]);
  89. string_appendChar(out, hexadecimals[value & 0b00001111]);
  90. }
  91. String& PersistentImage::toStreamIndented(String &out, const ReadableString &indentation) const {
  92. string_append(out, indentation);
  93. if (string_length(this->path)) {
  94. string_append(out, "File:", this->path);
  95. } else if (image_exists(this->value)) {
  96. int width = image_getWidth(this->value);
  97. int height = image_getHeight(this->value);
  98. string_append(out, width, U"x", height, U":");
  99. for (int y = 0; y < height; y++) {
  100. for (int x = 0; x < width; x++) {
  101. ColorRgbaI32 color = image_readPixel_clamp(this->value, x, y);
  102. writeHexaDecimal(out, color.red);
  103. writeHexaDecimal(out, color.green);
  104. writeHexaDecimal(out, color.blue);
  105. writeHexaDecimal(out, color.alpha);
  106. }
  107. }
  108. } else {
  109. string_append(out, U"None");
  110. }
  111. return out;
  112. }