Skip to content

Commit cd78df4

Browse files
authored
Merge pull request #292 from scratchcpp/png
Add PNG image format
2 parents 7ef8894 + 313d50e commit cd78df4

File tree

12 files changed

+255
-1
lines changed

12 files changed

+255
-1
lines changed

docs/Image formats.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\page imageFormats Image formats
22

33
To work with costumes, libscratchcpp has to read image files from Scratch projects.
4-
Only JPEG is currently supported, but support for PNG is coming soon.
4+
Only JPEG and PNG are currently supported, but SVG is going to be supported in the future.
55

66
# Implementing a custom image format
77
To implement a custom image format that libscratchcpp doesn't support,

src/imageformats/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
option(LIBSCRATCHCPP_JPEG_SUPPORT "JPEG image support" ON)
2+
option(LIBSCRATCHCPP_PNG_SUPPORT "PNG image support" ON)
23

34
add_subdirectory(stub)
45

@@ -7,6 +8,11 @@ if (LIBSCRATCHCPP_JPEG_SUPPORT)
78
add_subdirectory(jpeg)
89
endif()
910

11+
if (LIBSCRATCHCPP_PNG_SUPPORT)
12+
target_compile_definitions(scratchcpp PRIVATE PNG_SUPPORT)
13+
add_subdirectory(png)
14+
endif()
15+
1016
target_sources(scratchcpp
1117
PRIVATE
1218
defaultimageformats.cpp

src/imageformats/defaultimageformats.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "jpeg/jpegimageformatfactory.h"
88
#endif
99

10+
#ifdef PNG_SUPPORT
11+
#include "png/pngimageformatfactory.h"
12+
#endif
13+
1014
namespace libscratchcpp
1115
{
1216

@@ -18,6 +22,10 @@ class DefaultImageFormats
1822
#ifdef JPEG_SUPPORT
1923
ScratchConfiguration::registerImageFormat("jpg", std::make_shared<JpegImageFormatFactory>());
2024
#endif
25+
26+
#ifdef PNG_SUPPORT
27+
ScratchConfiguration::registerImageFormat("png", std::make_shared<PngImageFormatFactory>());
28+
#endif
2129
}
2230
};
2331

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
pngimageformat.cpp
4+
pngimageformat.h
5+
pngimageformatfactory.cpp
6+
pngimageformatfactory.h
7+
)
8+
9+
target_link_libraries(scratchcpp PRIVATE gd)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "pngimageformat.h"
4+
5+
using namespace libscratchcpp;
6+
7+
PngImageFormat::PngImageFormat()
8+
{
9+
}
10+
11+
PngImageFormat::~PngImageFormat()
12+
{
13+
if (m_img)
14+
gdImageDestroy(m_img);
15+
}
16+
17+
void PngImageFormat::setData(unsigned int size, void *data)
18+
{
19+
if (m_img)
20+
gdImageDestroy(m_img);
21+
22+
m_img = gdImageCreateFromPngPtr(size, data);
23+
}
24+
25+
unsigned int PngImageFormat::width() const
26+
{
27+
return m_img ? gdImageSX(m_img) : 0;
28+
}
29+
30+
unsigned int PngImageFormat::height() const
31+
{
32+
return m_img ? gdImageSY(m_img) : 0;
33+
}
34+
35+
Rgb PngImageFormat::colorAt(unsigned int x, unsigned int y, double scale) const
36+
{
37+
if (!m_img)
38+
return 0;
39+
40+
int color = gdImageGetPixel(m_img, x / scale, y / scale);
41+
int alpha = 127 - gdImageAlpha(m_img, color); // gdImageAlpha() returns values from 0 to 127
42+
43+
if (alpha == 127) // 127 should be the max (255)
44+
alpha = 255;
45+
else
46+
alpha *= 2;
47+
48+
return rgba(gdImageRed(m_img, color), gdImageGreen(m_img, color), gdImageBlue(m_img, color), alpha);
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/iimageformat.h>
6+
#include <gd.h>
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class PngImageFormat : public IImageFormat
12+
{
13+
public:
14+
PngImageFormat();
15+
~PngImageFormat();
16+
17+
void setData(unsigned int size, void *data) override;
18+
19+
unsigned int width() const override;
20+
unsigned int height() const override;
21+
22+
Rgb colorAt(unsigned int x, unsigned int y, double scale) const override;
23+
24+
private:
25+
gdImagePtr m_img = nullptr;
26+
};
27+
28+
} // namespace libscratchcpp
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "pngimageformatfactory.h"
4+
#include "pngimageformat.h"
5+
6+
using namespace libscratchcpp;
7+
8+
PngImageFormatFactory::PngImageFormatFactory()
9+
{
10+
}
11+
12+
std::shared_ptr<IImageFormat> PngImageFormatFactory::createInstance() const
13+
{
14+
return std::make_shared<PngImageFormat>();
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/iimageformatfactory.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class PngImageFormatFactory : public IImageFormatFactory
11+
{
12+
public:
13+
PngImageFormatFactory();
14+
15+
std::shared_ptr<IImageFormat> createInstance() const override;
16+
};
17+
18+
} // namespace libscratchcpp

test/image1.png

122 Bytes
Loading

test/image2.png

501 Bytes
Loading

0 commit comments

Comments
 (0)