Skip to content

Commit c053124

Browse files
authored
Merge pull request #281 from scratchcpp/image_api
Add image format API
2 parents edbb4ba + 0552615 commit c053124

35 files changed

+854
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ target_sources(scratchcpp
4848
include/scratchcpp/sprite.h
4949
include/scratchcpp/itimer.h
5050
include/scratchcpp/keyevent.h
51+
include/scratchcpp/iimageformat.h
52+
include/scratchcpp/iimageformatfactory.h
5153
)
5254

5355
add_library(zip SHARED

Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ EXCLUDE_SYMLINKS = NO
975975

976976
EXCLUDE_PATTERNS = */internal/* \
977977
*/src/blocks/* \
978+
*/src/imageformats/* \
978979
*_p.cpp \
979980
*_p.h
980981

docs/Image formats.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
\page imageFormats Image formats
2+
3+
To work with costumes, libscratchcpp has to read image files from Scratch projects.
4+
No formats are currently supported, but that will change soon.
5+
6+
# Implementing a custom image format
7+
To implement a custom image format that libscratchcpp doesn't support,
8+
subclass \link libscratchcpp::IImageFormat IImageFormat \endlink
9+
and override all of the methods.
10+
11+
Then subclass \link libscratchcpp::IImageFormatFactory IImageFormatFactory \endlink
12+
and override \link libscratchcpp::IImageFormatFactory::createInstance() createInstance() \endlink.
13+
14+
It's recommended to use the libscratchcpp namespace like this in your classes:
15+
```cpp
16+
using namespace libscratchcpp;
17+
```
18+
19+
Here's an example of the `createInstance()` method:
20+
```cpp
21+
std::shared_ptr<IImageFormat> TiffImageFormatFactory::createInstance() {
22+
return std::make_shared<TiffImageFormat>();
23+
}
24+
```
25+
26+
# Registering the image format
27+
To register the image format, you need to register the factory class.
28+
It will be used by classes such as \link libscratchcpp::Costume Costume \endlink
29+
internally to instantiate your image format.
30+
31+
```cpp
32+
libscratchcpp::ScratchConfiguration::registerImageFormat("tiff", std::make_shared<TiffImageFormatFactory>());
33+
```

include/scratchcpp/asset.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
2828

2929
const std::string &dataFormat() const;
3030

31+
const char *data() const;
32+
void setData(const char *data);
33+
34+
protected:
35+
virtual void processData(const char *data) { }
36+
3137
private:
3238
spimpl::unique_impl_ptr<AssetPrivate> impl;
3339
};

include/scratchcpp/costume.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include "iimageformat.h"
56
#include "spimpl.h"
67

78
#include "asset.h"
@@ -28,8 +29,22 @@ class LIBSCRATCHCPP_EXPORT Costume : public Asset
2829
int rotationCenterY() const;
2930
void setRotationCenterY(int newRotationCenterY);
3031

32+
unsigned int width() const;
33+
unsigned int height() const;
34+
35+
double scale() const;
36+
void setScale(double scale);
37+
38+
bool mirrorHorizontally() const;
39+
void setMirrorHorizontally(bool mirror);
40+
41+
Rgb **bitmap() const;
42+
3143
Broadcast *broadcast();
3244

45+
protected:
46+
void processData(const char *data) override;
47+
3348
private:
3449
spimpl::unique_impl_ptr<CostumePrivate> impl;
3550
};

include/scratchcpp/iimageformat.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "global.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
/*! A typedef for unsigned int. Holds the RGBA values. */
11+
using Rgb = unsigned int;
12+
13+
/*! \brief The IImageFormat class is an interface for image format implementation. */
14+
class LIBSCRATCHCPP_EXPORT IImageFormat
15+
{
16+
public:
17+
virtual ~IImageFormat() { }
18+
19+
/*! Sets the image data (buffer). */
20+
virtual void setData(const char *data) = 0;
21+
22+
/*! Returns the width of the image. */
23+
virtual unsigned int width() const = 0;
24+
25+
/*! Returns the height of the image. */
26+
virtual unsigned int height() const = 0;
27+
28+
/*! Returns the color at the given pixel. */
29+
virtual Rgb colorAt(unsigned int x, unsigned int y, double scale = 1) const = 0;
30+
};
31+
32+
} // namespace libscratchcpp
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <memory>
6+
7+
#include "global.h"
8+
9+
namespace libscratchcpp
10+
{
11+
12+
class IImageFormat;
13+
14+
/*! \brief The IImageFormatFactory class is an interface which is used to instantiate image formats. */
15+
class LIBSCRATCHCPP_EXPORT IImageFormatFactory
16+
{
17+
public:
18+
virtual ~IImageFormatFactory() { }
19+
20+
/*! Creates an instance of the image format. */
21+
virtual std::shared_ptr<IImageFormat> createInstance() const = 0;
22+
};
23+
24+
} // namespace libscratchcpp

include/scratchcpp/scratchconfiguration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace libscratchcpp
1111
{
1212

1313
class IExtension;
14+
class IImageFormat;
15+
class IImageFormatFactory;
1416
class ScratchConfigurationPrivate;
1517

1618
/*! \brief The ScratchConfiguration class provides methods for adding custom extensions. */
@@ -35,6 +37,10 @@ class LIBSCRATCHCPP_EXPORT ScratchConfiguration
3537
return nullptr;
3638
};
3739

40+
static void registerImageFormat(const std::string &name, std::shared_ptr<IImageFormatFactory> formatFactory);
41+
static void removeImageFormat(const std::string &name);
42+
static std::shared_ptr<IImageFormat> createImageFormat(const std::string &name);
43+
3844
private:
3945
static const std::vector<std::shared_ptr<IExtension>> getExtensions();
4046

include/scratchcpp/sprite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
4747
double size() const;
4848
void setSize(double newSize);
4949

50+
void setCurrentCostume(int newCostume);
51+
5052
double direction() const;
5153
void setDirection(double newDirection);
5254

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ add_subdirectory(blocks)
1212
add_subdirectory(engine)
1313
add_subdirectory(internal)
1414
add_subdirectory(scratch)
15+
add_subdirectory(imageformats)

0 commit comments

Comments
 (0)