types.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2019 to 2022 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_API_TYPES
  25. #define DFPSR_API_TYPES
  26. #include <cstdint>
  27. #include <memory>
  28. #include "../image/Color.h"
  29. #include "../math/IRect.h"
  30. #include "stringAPI.h"
  31. // Define DFPSR_INTERNAL_ACCESS before any include to get internal access to exposed types
  32. #ifdef DFPSR_INTERNAL_ACCESS
  33. #define IMPL_ACCESS public
  34. #else
  35. #define IMPL_ACCESS protected
  36. #endif
  37. namespace dsr {
  38. enum class ImageFileFormat {
  39. Unknown, // Used as an error code for unidentified formats.
  40. JPG, // Lossy compressed image format storing brightness separated from red and blue offsets using the discrete cosine transform of each block.
  41. PNG, // Lossless compressed image format. Some image editors don't save RGB values where alpha is zero, which will bleed through black edges in bi-linear interpolation when the interpolated alpha is not zero.
  42. TGA, // Lossless compressed format. Applications usually give Targa better control over the alpha channel than PNG, but it's more common that the Targa specification is interpreted in incompatible ways.
  43. BMP // Uncompressed image format for storing data that does not really represent an image and you just want it to be exact.
  44. };
  45. enum class PackOrderIndex {
  46. RGBA, // Windows
  47. BGRA, // Ubuntu
  48. ARGB,
  49. ABGR
  50. };
  51. enum class Sampler {
  52. Nearest,
  53. Linear
  54. };
  55. enum class ReturnCode {
  56. Good,
  57. KeyNotFound,
  58. ParsingFailure
  59. };
  60. // A handle to a model.
  61. class ModelImpl;
  62. using Model = std::shared_ptr<ModelImpl>;
  63. // A handle to a multi-threaded rendering context.
  64. class RendererImpl;
  65. using Renderer = std::shared_ptr<RendererImpl>;
  66. // A handle to a window.
  67. // The Window wraps itself around native window backends to abstract away platform specific details.
  68. // It also makes it easy to load and use a graphical interface using the optional component system.
  69. class DsrWindow;
  70. using Window = std::shared_ptr<DsrWindow>;
  71. // A handle to a GUI component.
  72. // Components are an abstraction for graphical user interfaces, which might not always be powerful enough.
  73. // * If you're making something advanced that components cannot do,
  74. // you can also use draw calls and input events directly against the window without using Component.
  75. class VisualComponent;
  76. using Component = std::shared_ptr<VisualComponent>;
  77. // A handle to a GUI theme.
  78. // Themes describes the visual appearance of an interface.
  79. // By having more than one theme for your interface, you can let the user select one.
  80. class VisualThemeImpl;
  81. using VisualTheme = std::shared_ptr<VisualThemeImpl>;
  82. // A handle to a raster font
  83. class RasterFontImpl;
  84. using RasterFont = std::shared_ptr<RasterFontImpl>;
  85. // A handle to a media machine.
  86. // Media machines can be used to generate, filter and analyze images.
  87. // Everything running in a media machine is guaranteed to be 100% deterministic to the last bit.
  88. // This reduces the amount of code where maintenance has to be performed during porting.
  89. // It also means that any use of float or double is forbidden.
  90. struct VirtualMachine;
  91. struct MediaMachine : IMPL_ACCESS std::shared_ptr<VirtualMachine> {
  92. MediaMachine(); // Defaults to null
  93. IMPL_ACCESS:
  94. explicit MediaMachine(const std::shared_ptr<VirtualMachine>& machine);
  95. };
  96. // Images
  97. // Points to a buffer region holding at least height * stride bytes.
  98. // Each row contains:
  99. // * A number of visible pixels
  100. // * A number of unused bytes
  101. // New or cloned images have their stride aligned to 16-bytes
  102. // Stride is the number of bytes from the start of one row to the next
  103. // Sub-images have the same stride and buffer as their parent
  104. // Some unused pixels may be visible somewhere else
  105. // 8-bit unsigned integer grayscale image
  106. class ImageU8Impl;
  107. struct ImageU8 : IMPL_ACCESS std::shared_ptr<ImageU8Impl> {
  108. ImageU8(); // Defaults to null
  109. IMPL_ACCESS:
  110. explicit ImageU8(const std::shared_ptr<ImageU8Impl>& image);
  111. explicit ImageU8(const ImageU8Impl& image);
  112. };
  113. // Invariant:
  114. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  115. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  116. // * No other image can displays pixels from its padding
  117. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  118. struct AlignedImageU8 : public ImageU8 {
  119. AlignedImageU8() {} // Defaults to null
  120. IMPL_ACCESS:
  121. explicit AlignedImageU8(const std::shared_ptr<ImageU8Impl>& image) : ImageU8(image) {}
  122. explicit AlignedImageU8(const ImageU8Impl& image) : ImageU8(image) {}
  123. };
  124. // 16-bit unsigned integer grayscale image
  125. class ImageU16Impl;
  126. struct ImageU16 : IMPL_ACCESS std::shared_ptr<ImageU16Impl> {
  127. ImageU16(); // Defaults to null
  128. IMPL_ACCESS:
  129. explicit ImageU16(const std::shared_ptr<ImageU16Impl>& image);
  130. explicit ImageU16(const ImageU16Impl& image);
  131. };
  132. // Invariant:
  133. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  134. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  135. // * No other image can displays pixels from its padding
  136. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  137. struct AlignedImageU16 : public ImageU16 {
  138. AlignedImageU16() {} // Defaults to null
  139. IMPL_ACCESS:
  140. explicit AlignedImageU16(const std::shared_ptr<ImageU16Impl>& image) : ImageU16(image) {}
  141. explicit AlignedImageU16(const ImageU16Impl& image) : ImageU16(image) {}
  142. };
  143. // 32-bit floating-point grayscale image
  144. class ImageF32Impl;
  145. struct ImageF32 : IMPL_ACCESS std::shared_ptr<ImageF32Impl> {
  146. ImageF32(); // Defaults to null
  147. IMPL_ACCESS:
  148. explicit ImageF32(const std::shared_ptr<ImageF32Impl>& image);
  149. explicit ImageF32(const ImageF32Impl& image);
  150. };
  151. // Invariant:
  152. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  153. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  154. // * No other image can displays pixels from its padding
  155. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  156. struct AlignedImageF32 : public ImageF32 {
  157. AlignedImageF32() {} // Defaults to null
  158. IMPL_ACCESS:
  159. explicit AlignedImageF32(const std::shared_ptr<ImageF32Impl>& image) : ImageF32(image) {}
  160. explicit AlignedImageF32(const ImageF32Impl& image) : ImageF32(image) {}
  161. };
  162. // 4x8-bit unsigned integer RGBA color image
  163. class ImageRgbaU8Impl;
  164. struct ImageRgbaU8 : IMPL_ACCESS std::shared_ptr<ImageRgbaU8Impl> {
  165. ImageRgbaU8(); // Defaults to null
  166. IMPL_ACCESS:
  167. explicit ImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image);
  168. explicit ImageRgbaU8(const ImageRgbaU8Impl& image);
  169. };
  170. // Invariant:
  171. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  172. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  173. // * No other image can displays pixels from its padding
  174. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  175. struct AlignedImageRgbaU8 : public ImageRgbaU8 {
  176. AlignedImageRgbaU8() {} // Defaults to null
  177. IMPL_ACCESS:
  178. explicit AlignedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : ImageRgbaU8(image) {}
  179. explicit AlignedImageRgbaU8(const ImageRgbaU8Impl& image) : ImageRgbaU8(image) {}
  180. };
  181. // Invariant:
  182. // * Using the default RGBA pack order
  183. // This removes the need to implement filters for different pack orders when RGBA can be safely assumed
  184. // Just use AlignedImageRgbaU8 if channels don't have to be aligned
  185. struct OrderedImageRgbaU8 : public AlignedImageRgbaU8 {
  186. OrderedImageRgbaU8() {} // Defaults to null
  187. IMPL_ACCESS:
  188. explicit OrderedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : AlignedImageRgbaU8(image) {}
  189. explicit OrderedImageRgbaU8(const ImageRgbaU8Impl& image) : AlignedImageRgbaU8(image) {}
  190. };
  191. }
  192. #endif