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  using Surface::write;
87 
88 protected:
89  virtual void read(uint32_t offset, void* buffer, size_t length) = 0;
90  virtual void write(uint32_t offset, const void* data, size_t length) = 0;
91 
95  size_t imageBytes;
99 };
100 
106 class MemoryImageSurface : public ImageSurface
107 {
108 public:
109  MemoryImageSurface(MemoryImageObject& image, PixelFormat format, const Blend* blend, size_t bufferSize,
111  : ImageSurface(image, format, bufferSize), imageData(imageData), blend(blend)
112  {
113  }
114 
115  Type getType() const override
116  {
117  return Type::Memory;
118  }
119 
120 protected:
121  void read(uint32_t offset, void* buffer, size_t length) override;
122  void write(uint32_t offset, const void* data, size_t length) override;
123 
125  const Blend* blend;
126 };
127 
134 {
135 public:
137  : ImageSurface(image, format, bufferSize), file(file)
138  {
139  }
140 
141  Type getType() const
142  {
143  return Type::File;
144  }
145 
146 protected:
147  void read(uint32_t offset, void* buffer, size_t length) override;
148  void write(uint32_t offset, const void* data, size_t length) override;
149 
150 private:
151  IFS::FileStream& file;
152 };
153 
154 } // namespace Graphics
Type getType() const override
Definition: ImageSurface.h:134
bool setAddrWindow(const Rect &rect) override
Definition: ImageSurface.h:101
void commit(uint16_t length) override
PixelFormat getPixelFormat() const override
Definition: ImageSurface.h:96
void write(uint32_t offset, const void *data, size_t length) override
bool writeDataBuffer(SharedBuffer &data, size_t offset, uint16_t length) override
Definition: ImageSurface.h:110
Definition: Libraries/Graphics/src/include/Graphics/Object.h:830
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
Type getType() const
Definition: ImageSurface.h:160
Manages a rectangular area of display memory with position information.
Definition: AddressWindow.h:56
bool setPixel(PackedColor color, Point pt) override
Type
Definition: Surface.h:69
size_t size() const
Definition: Graphics/src/include/Graphics/Buffer.h:189
Location and size of rectangular area (x, y, w, h)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:306
bool fillRect(PackedColor color, const Rect &rect) override
ImageSurface(ImageObject &image, PixelFormat format, size_t bufferSize)
Definition: ImageSurface.h:75
AddressWindow addressWindow
Definition: ImageSurface.h:131
Size getSize() const override
Definition: ImageSurface.h:91
File stream class.
Definition: IFS/FileStream.h:30
void(*)(void *param) PresentCallback
Definition: Surface.h:82
bool present(PresentCallback callback, void *param) override
Present surface to display device.
ImageObject & image
Definition: ImageSurface.h:130
Image surface using RAM as backing store.
Definition: ImageSurface.h:125
MemoryImageSurface(MemoryImageObject &image, PixelFormat format, const Blend *blend, size_t bufferSize, uint8_t *imageData)
Definition: ImageSurface.h:128
IFS::File File
Definition: Core/FileSystem.h:32
void read(uint32_t offset, void *buffer, size_t length) override
PixelFormat pixelFormat
Definition: ImageSurface.h:135
void(*)(ReadBuffer &data, size_t length, void *param) ReadCallback
Callback for readPixel() operations.
Definition: Surface.h:88
virtual void read(uint32_t offset, void *buffer, size_t length)=0
int readDataBuffer(ReadBuffer &buffer, ReadStatus *status, ReadCallback callback, void *param) override
Read some pixels.
SharedBuffer buffer
Definition: ImageSurface.h:134
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:124
uint8_t * getBuffer(uint16_t minBytes, uint16_t &available) override
const Blend * blend
Definition: ImageSurface.h:144
Buffer used for reading pixel data from device.
Definition: Graphics/src/include/Graphics/Buffer.h:205
bool blockFill(const void *data, uint16_t length, uint32_t repeat) override
Definition: Virtual.h:30
size_t imageBytes
Definition: ImageSurface.h:133
void reset() override
Reset surface ready for more commands.
Definition: ImageSurface.h:117
void write(MetaWriter &meta) const override
Definition: Surface.h:92
void read(uint32_t offset, void *buffer, size_t length) override
Size imageSize
Definition: ImageSurface.h:132
FileImageSurface(FileImageObject &image, PixelFormat format, size_t bufferSize, IFS::FileStream &file)
Definition: ImageSurface.h:155
Stores result of read operation.
Definition: Graphics/src/include/Graphics/Buffer.h:232
Stat stat() const override
Definition: ImageSurface.h:83
Blend operations.
Definition: Blend.h:60
PixelFormat
Definition: Colors.h:295
Shared heap-allocated data buffer.
Definition: Graphics/src/include/Graphics/Buffer.h:54
uint8_t bytesPerPixel
Definition: ImageSurface.h:136
Virtual class to access an image as a Surface.
Definition: ImageSurface.h:53
Size getSize() const
Definition: Libraries/Graphics/src/include/Graphics/Object.h:595
uint8_t * imageData
Definition: ImageSurface.h:143
virtual void write(uint32_t offset, const void *data, size_t length)=0
Virtual base class for an image.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:581
Image surface using filing system as backing store.
Definition: ImageSurface.h:152
Colour in device pixel format.
Definition: Colors.h:339
void write(uint32_t offset, const void *data, size_t length) override
size_t used
Definition: Surface.h:78
Definition: Libraries/Graphics/src/include/Graphics/Object.h:791
bool writePixels(const void *data, uint16_t length) override