WindowFonts.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <array>
  16. #include <OgreImage.h>
  17. #include <OgreColourValue.h>
  18. #include "WindowFonts.h"
  19. namespace{
  20. /**
  21. * An RGBA colour.
  22. */
  23. struct Colour{
  24. u8 r;
  25. u8 g;
  26. u8 b;
  27. u8 a;
  28. };
  29. /**
  30. * Fully transparent colour.
  31. */
  32. const Colour TRANSPARENT = {0, 0, 0, 0};
  33. /**
  34. * Colour of every palette index of a 4 bpp image.
  35. */
  36. typedef std::array<Colour, 16> Palette;
  37. /**
  38. * Palette of the dialog font: index 3 is the glyph, index 1 its shadow.
  39. */
  40. Palette TextPalette(){
  41. Palette palette;
  42. palette.fill(TRANSPARENT);
  43. palette[1] = {32, 32, 32, 255};
  44. palette[3] = {230, 230, 230, 255};
  45. return palette;
  46. }
  47. /**
  48. * Palette of the menu font and the small digits: index 1 is the glyph, index 15 its shadow.
  49. */
  50. Palette MenuPalette(){
  51. Palette palette;
  52. palette.fill(TRANSPARENT);
  53. palette[1] = {230, 230, 230, 255};
  54. palette[15] = {24, 24, 24, 255};
  55. return palette;
  56. }
  57. /**
  58. * Palette of the timer font. It's opaque: indices 2 to 4 are the glyph, the others the background.
  59. */
  60. Palette TimerPalette(){
  61. Palette palette;
  62. palette.fill({0, 0, 0, 255});
  63. palette[2] = {255, 0, 255, 255};
  64. palette[3] = {197, 0, 197, 255};
  65. palette[4] = {139, 0, 139, 255};
  66. palette[13] = {41, 41, 41, 255};
  67. palette[14] = {24, 24, 24, 255};
  68. palette[15] = {24, 24, 24, 255};
  69. return palette;
  70. }
  71. /**
  72. * Creates an image filled with a colour.
  73. *
  74. * @param[in] width Image width.
  75. * @param[in] height Image height.
  76. * @param[in] colour Colour of every pixel.
  77. * @return The image.
  78. */
  79. WindowFonts::RgbaImage CreateImage(int width, int height, const Colour& colour){
  80. WindowFonts::RgbaImage image;
  81. image.width = width;
  82. image.height = height;
  83. image.pixels.resize(width * height * 4);
  84. for (int i = 0; i < width * height; i ++){
  85. image.pixels[i * 4] = colour.r;
  86. image.pixels[i * 4 + 1] = colour.g;
  87. image.pixels[i * 4 + 2] = colour.b;
  88. image.pixels[i * 4 + 3] = colour.a;
  89. }
  90. return image;
  91. }
  92. /**
  93. * Copies a rectangle of a TIM image into an RGBA image, converting it with a palette.
  94. *
  95. * Pixels outside of the destination are ignored.
  96. *
  97. * @param[in] src The TIM image.
  98. * @param[in] src_x X coordinate of the rectangle in the TIM image.
  99. * @param[in] src_y Y coordinate of the rectangle in the TIM image.
  100. * @param[in] width Width of the rectangle.
  101. * @param[in] height Height of the rectangle.
  102. * @param[in] palette Colour of every palette index.
  103. * @param[out] dst The destination image.
  104. * @param[in] dst_x X coordinate of the rectangle in the destination image.
  105. * @param[in] dst_y Y coordinate of the rectangle in the destination image.
  106. */
  107. void Blit(
  108. const WindowFonts::IndexImage& src, int src_x, int src_y, int width, int height, const Palette& palette,
  109. WindowFonts::RgbaImage& dst, int dst_x, int dst_y
  110. ){
  111. for (int y = 0; y < height; y ++){
  112. for (int x = 0; x < width; x ++){
  113. const int tx = dst_x + x;
  114. const int ty = dst_y + y;
  115. if (tx < 0 || ty < 0 || tx >= dst.width || ty >= dst.height) continue;
  116. const Colour& c = palette[src.At(src_x + x, src_y + y) & 0x0F];
  117. const int i = (ty * dst.width + tx) * 4;
  118. dst.pixels[i] = c.r;
  119. dst.pixels[i + 1] = c.g;
  120. dst.pixels[i + 2] = c.b;
  121. dst.pixels[i + 3] = c.a;
  122. }
  123. }
  124. }
  125. /**
  126. * Reads a little endian 16 bit value.
  127. */
  128. u32 ReadU16(const u8* data){return data[0] | (data[1] << 8);}
  129. /**
  130. * Reads a little endian 32 bit value.
  131. */
  132. u32 ReadU32(const u8* data){return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);}
  133. }
  134. u8 WindowFonts::IndexImage::At(int x, int y) const{
  135. if (x < 0 || y < 0 || x >= width || y >= height) return 0;
  136. return pixels[y * width + x];
  137. }
  138. bool WindowFonts::ReadTim4(const u8* data, size_t size, IndexImage& image){
  139. // Header: magic (0x10) and flags. 4 bpp with a palette is 0x08.
  140. if (size < 8 || ReadU32(data) != 0x10 || (ReadU32(data + 4) & 0x0F) != 0x08) return false;
  141. size_t offset = 8;
  142. // Palette block: its length comes first, the colours are not needed.
  143. if (offset + 12 > size) return false;
  144. offset += ReadU32(data + offset);
  145. // Image block: length, x, y, width in 16 bit units, height.
  146. if (offset + 12 > size) return false;
  147. const int width = ReadU16(data + offset + 8) * 4;
  148. const int height = ReadU16(data + offset + 10);
  149. offset += 12;
  150. if (offset + static_cast<size_t>(width / 2) * height > size) return false;
  151. image.width = width;
  152. image.height = height;
  153. image.pixels.resize(width * height);
  154. for (int i = 0; i < width * height / 2; i ++){
  155. image.pixels[i * 2] = data[offset + i] & 0x0F;
  156. image.pixels[i * 2 + 1] = data[offset + i] >> 4;
  157. }
  158. return true;
  159. }
  160. WindowFonts::RgbaImage WindowFonts::BuildTextFont(const IndexImage& font){
  161. RgbaImage image = CreateImage(256, 256, TRANSPARENT);
  162. Blit(font, 0, 0, 256, 256, TextPalette(), image, 0, 0);
  163. return image;
  164. }
  165. WindowFonts::RgbaImage WindowFonts::BuildMenuFont(const IndexImage& window){
  166. RgbaImage image = CreateImage(128, 256, TRANSPARENT);
  167. // Twelve rows of 8 pixels at (128, 152).
  168. for (int row = 0; row < 12; row ++) Blit(window, 128, 152 + row * 8, 128, 8, MenuPalette(), image, 0, row * 16 + 8);
  169. return image;
  170. }
  171. WindowFonts::RgbaImage WindowFonts::BuildDigitsFont(const IndexImage& window){
  172. RgbaImage image = CreateImage(128, 8, TRANSPARENT);
  173. const Palette palette = MenuPalette();
  174. // Digits 0 to 9, in cells of 8 pixels at (136, 0).
  175. Blit(window, 136, 0, 80, 8, palette, image, 0, 0);
  176. // Symbols, taken from the menu font: '-', '+', '%' and ':'. Cell 10 is the blank space.
  177. Blit(window, 136, 240, 8, 8, palette, image, 88, 0);
  178. Blit(window, 152, 216, 8, 8, palette, image, 96, 0);
  179. Blit(window, 152, 232, 8, 8, palette, image, 104, 0);
  180. Blit(window, 168, 232, 8, 8, palette, image, 112, 0);
  181. return image;
  182. }
  183. WindowFonts::RgbaImage WindowFonts::BuildTimerFont(const IndexImage& window){
  184. // Two rows of 24 pixels at (144, 80). The image is wider than what's left of the texture.
  185. RgbaImage image = CreateImage(128, 48, {0, 0, 0, 255});
  186. Blit(window, 144, 80, 112, 48, TimerPalette(), image, 0, 0);
  187. return image;
  188. }
  189. void WindowFonts::SavePng(const RgbaImage& image, const std::string& file_name){
  190. Ogre::Image png(Ogre::PF_R8G8B8A8_UINT, image.width, image.height);
  191. for (int y = 0; y < image.height; y ++){
  192. for (int x = 0; x < image.width; x ++){
  193. const u8* p = &image.pixels[(y * image.width + x) * 4];
  194. png.setColourAt(Ogre::ColourValue(p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, p[3] / 255.0f), x, y, 0);
  195. }
  196. }
  197. png.save(file_name);
  198. }