ImageSurface.h
Go to the documentation of this file.
1 /****
2  * Surface.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Surface.h"
25 #include "AddressWindow.h"
26 
27 namespace Graphics
28 {
34 class ImageSurface : public Surface
35 {
36 public:
37  ImageSurface(ImageObject& image, PixelFormat format, size_t bufferSize)
38  : image(image), imageSize(image.getSize()), buffer(bufferSize), pixelFormat(format),
40  {
41  auto size = image.getSize();
42  imageBytes = size.w * size.h * bytesPerPixel;
43  }
44 
45  Stat stat() const override
46  {
47  return Stat{
48  .used = 0,
49  .available = buffer.size(),
50  };
51  }
52 
53  Size getSize() const override
54  {
55  return image.getSize();
56  }
57 
58  PixelFormat getPixelFormat() const override
59  {
60  return pixelFormat;
61  }
62 
63  bool setAddrWindow(const Rect& rect) override
64  {
65  addressWindow = rect;
66  return true;
67  }
68 
69  uint8_t* getBuffer(uint16_t minBytes, uint16_t& available) override;
70  void commit(uint16_t length) override;
71  bool blockFill(const void* data, uint16_t length, uint32_t repeat) override;
72  bool writeDataBuffer(SharedBuffer& data, size_t offset, uint16_t length) override
73  {
74  return writePixels(&data[offset], length);
75  }
76  bool setPixel(PackedColor color, Point pt) override;
77  bool writePixels(const void* data, uint16_t length) override;
78  int readDataBuffer(ReadBuffer& buffer, ReadStatus* status, ReadCallback callback, void* param) override;
79  void reset() override
80  {
81  }
82  bool present(PresentCallback callback, void* param) override;
83 
84  bool fillRect(PackedColor color, const Rect& rect) override;
85 
86 protected:
87  virtual void read(uint32_t offset, void* buffer, size_t length) = 0;
88  virtual void write(uint32_t offset, const void* data, size_t length) = 0;
89 
93  size_t imageBytes;
97 };
98 
105 {
106 public:
107  MemoryImageSurface(MemoryImageObject& image, PixelFormat format, const Blend* blend, size_t bufferSize,
108  uint8_t* imageData)
109  : ImageSurface(image, format, bufferSize), imageData(imageData), blend(blend)
110  {
111  }
112 
113  Type getType() const override
114  {
115  return Type::Memory;
116  }
117 
118 protected:
119  void read(uint32_t offset, void* buffer, size_t length) override;
120  void write(uint32_t offset, const void* data, size_t length) override;
121 
123  const Blend* blend;
124 };
125 
132 {
133 public:
135  : ImageSurface(image, format, bufferSize), file(file)
136  {
137  }
138 
139  Type getType() const
140  {
141  return Type::File;
142  }
143 
144 protected:
145  void read(uint32_t offset, void* buffer, size_t length) override;
146  void write(uint32_t offset, const void* data, size_t length) override;
147 
148 private:
149  IFS::FileStream& file;
150 };
151 
152 } // namespace Graphics
Type getType() const
Definition: ImageSurface.h:139
bool setPixel(PackedColor color, Point pt) override
bool fillRect(PackedColor color, const Rect &rect) override
ImageSurface(ImageObject &image, PixelFormat format, size_t bufferSize)
Definition: ImageSurface.h:37
bool setAddrWindow(const Rect &rect) override
Definition: ImageSurface.h:63
Definition: Libraries/Graphics/src/include/Graphics/Object.h:810
SharedBuffer buffer
Definition: ImageSurface.h:94
Colour in device pixel format.
Definition: Colors.h:339
MemoryImageSurface(MemoryImageObject &image, PixelFormat format, const Blend *blend, size_t bufferSize, uint8_t *imageData)
Definition: ImageSurface.h:107
bool writeDataBuffer(SharedBuffer &data, size_t offset, uint16_t length) override
Definition: ImageSurface.h:72
void commit(uint16_t length) override
PixelFormat getPixelFormat() const override
Definition: ImageSurface.h:58
size_t size() const
Definition: Graphics/src/include/Graphics/Buffer.h:151
uint8_t * getBuffer(uint16_t minBytes, uint16_t &available) override
Definition: Surface.h:58
Location and size of rectangular area (x, y, w, h)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:287
ImageObject & image
Definition: ImageSurface.h:90
Size getSize() const override
Definition: ImageSurface.h:53
size_t used
Definition: Surface.h:59
bool present(PresentCallback callback, void *param) override
Present surface to display device.
AddressWindow addressWindow
Definition: ImageSurface.h:91
Image surface using RAM as backing store.
Definition: ImageSurface.h:104
PixelFormat pixelFormat
Definition: ImageSurface.h:95
Size imageSize
Definition: ImageSurface.h:92
uint8_t * imageData
Definition: ImageSurface.h:122
virtual void read(uint32_t offset, void *buffer, size_t length)=0
Image surface using filing system as backing store.
Definition: ImageSurface.h:131
Stat stat() const override
Definition: ImageSurface.h:45
Type
Definition: Asset.h:82
Type getType() const override
Definition: ImageSurface.h:113
Virtual base class for an image.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:561
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:105
Shared heap-allocated data buffer.
Definition: Graphics/src/include/Graphics/Buffer.h:35
void reset() override
Reset surface ready for more commands.
Definition: ImageSurface.h:79
void(*)(ReadBuffer &data, size_t length, void *param) ReadCallback
Callback for readPixel() operations.
Definition: Surface.h:69
int readDataBuffer(ReadBuffer &buffer, ReadStatus *status, ReadCallback callback, void *param) override
Read some pixels.
bool blockFill(const void *data, uint16_t length, uint32_t repeat) override
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
virtual void write(uint32_t offset, const void *data, size_t length)=0
Stores result of read operation.
Definition: Graphics/src/include/Graphics/Buffer.h:213
size_t imageBytes
Definition: ImageSurface.h:93
Size getSize() const
Definition: Libraries/Graphics/src/include/Graphics/Object.h:575
File stream class.
Definition: IFS/FileStream.h:22
Manages a rectangular area of display memory with position information.
Definition: AddressWindow.h:37
bool writePixels(const void *data, uint16_t length) override
PixelFormat
Definition: Colors.h:295
const Blend * blend
Definition: ImageSurface.h:123
Blend operations.
Definition: Blend.h:40
FileImageSurface(FileImageObject &image, PixelFormat format, size_t bufferSize, IFS::FileStream &file)
Definition: ImageSurface.h:134
Interface for a drawing surface.
Definition: Surface.h:41
Definition: Virtual.h:30
Buffer used for reading pixel data from device.
Definition: Graphics/src/include/Graphics/Buffer.h:186
Definition: Libraries/Graphics/src/include/Graphics/Object.h:771
void(*)(void *param) PresentCallback
Definition: Surface.h:63
uint8_t bytesPerPixel
Definition: ImageSurface.h:96
Virtual class to access an image as a Surface.
Definition: ImageSurface.h:34
IFS::File File
Definition: Core/FileSystem.h:32