Skip to content

Commit 0bf5dd5

Browse files
committed
Implement JPEG image format
1 parent 4b9349e commit 0bf5dd5

File tree

11 files changed

+270
-0
lines changed

11 files changed

+270
-0
lines changed

src/imageformats/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1+
option(LIBSCRATCHCPP_JPEG_SUPPORT "JPEG image support" ON)
2+
13
add_subdirectory(stub)
4+
5+
if (LIBSCRATCHCPP_JPEG_SUPPORT)
6+
target_compile_definitions(scratchcpp PRIVATE JPEG_SUPPORT)
7+
add_subdirectory(jpeg)
8+
endif()
9+
10+
target_sources(scratchcpp
11+
PRIVATE
12+
defaultimageformats.cpp
13+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/scratchconfiguration.h>
4+
#include <string>
5+
6+
#ifdef JPEG_SUPPORT
7+
#include "jpeg/jpegimageformatfactory.h"
8+
#endif
9+
10+
namespace libscratchcpp
11+
{
12+
13+
class DefaultImageFormats
14+
{
15+
public:
16+
DefaultImageFormats()
17+
{
18+
#ifdef JPEG_SUPPORT
19+
ScratchConfiguration::registerImageFormat("jpg", std::make_shared<JpegImageFormatFactory>());
20+
#endif
21+
}
22+
};
23+
24+
static DefaultImageFormats instance;
25+
26+
} // namespace libscratchcpp
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+
jpegimageformat.cpp
4+
jpegimageformat.h
5+
jpegimageformatfactory.cpp
6+
jpegimageformatfactory.h
7+
)
8+
9+
target_link_libraries(scratchcpp PRIVATE gd)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "jpegimageformat.h"
4+
5+
using namespace libscratchcpp;
6+
7+
JpegImageFormat::JpegImageFormat()
8+
{
9+
}
10+
11+
JpegImageFormat::~JpegImageFormat()
12+
{
13+
if (m_img)
14+
gdImageDestroy(m_img);
15+
}
16+
17+
void JpegImageFormat::setData(unsigned int size, void *data)
18+
{
19+
if (m_img)
20+
gdImageDestroy(m_img);
21+
22+
m_img = gdImageCreateFromJpegPtr(size, data);
23+
}
24+
25+
unsigned int JpegImageFormat::width() const
26+
{
27+
return m_img ? gdImageSX(m_img) : 0;
28+
}
29+
30+
unsigned int JpegImageFormat::height() const
31+
{
32+
return m_img ? gdImageSY(m_img) : 0;
33+
}
34+
35+
Rgb JpegImageFormat::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+
42+
return rgb(gdImageRed(m_img, color), gdImageGreen(m_img, color), gdImageBlue(m_img, color));
43+
}
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 JpegImageFormat : public IImageFormat
12+
{
13+
public:
14+
JpegImageFormat();
15+
~JpegImageFormat();
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 "jpegimageformatfactory.h"
4+
#include "jpegimageformat.h"
5+
6+
using namespace libscratchcpp;
7+
8+
JpegImageFormatFactory::JpegImageFormatFactory()
9+
{
10+
}
11+
12+
std::shared_ptr<IImageFormat> JpegImageFormatFactory::createInstance() const
13+
{
14+
return std::make_shared<JpegImageFormat>();
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 JpegImageFormatFactory : public IImageFormatFactory
11+
{
12+
public:
13+
JpegImageFormatFactory();
14+
15+
std::shared_ptr<IImageFormat> createInstance() const override;
16+
};
17+
18+
} // namespace libscratchcpp

test/image1.jpg

854 Bytes
Loading

test/image2.jpg

827 Bytes
Loading

test/imageformats/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ target_link_libraries(
1111
)
1212

1313
gtest_discover_tests(imageformatstub_test)
14+
15+
# jpeg
16+
add_executable(
17+
jpeg_test
18+
jpeg_test.cpp
19+
)
20+
21+
target_link_libraries(
22+
jpeg_test
23+
GTest::gtest_main
24+
scratchcpp
25+
)
26+
27+
gtest_discover_tests(jpeg_test)

0 commit comments

Comments
 (0)