Skip to content

Commit 18c9c32

Browse files
authored
Merge pull request #291 from scratchcpp/jpeg
Add JPEG image format
2 parents 21c61ca + dc8af67 commit 18c9c32

File tree

17 files changed

+432
-3
lines changed

17 files changed

+432
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
sudo apt-get update
24-
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev
24+
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev libgd-dev
2525
shell: bash
2626
- name: Configure CMake
2727
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

.github/workflows/utests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
sudo apt-get update
24-
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev
24+
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev libgd-dev
2525
shell: bash
2626
- name: Configure CMake
2727
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ include_directories(thirdparty/zip/src)
6161

6262
include_directories(thirdparty/spimpl)
6363

64+
include(build/FindGD.cmake)
65+
6466
target_link_libraries(scratchcpp PRIVATE nlohmann_json::nlohmann_json)
6567
target_link_libraries(scratchcpp PRIVATE utf8cpp)
6668
target_link_libraries(scratchcpp PRIVATE zip)

build/FindGD.cmake

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# - Find GD
2+
# Find the native GD includes and library
3+
# This module defines
4+
# GD_INCLUDE_DIR, where to find gd.h, etc.
5+
# GD_LIBRARIES, the libraries needed to use GD.
6+
# GD_FOUND, If false, do not try to use GD.
7+
# also defined, but not for general use are
8+
# GD_LIBRARY, where to find the GD library.
9+
# GD_SUPPORTS_PNG, GD_SUPPORTS_JPEG, GD_SUPPORTS_GIF, test
10+
# support for image formats in GD.
11+
12+
FIND_PATH(GD_INCLUDE_DIR gd.h
13+
/usr/local/include
14+
/usr/include
15+
)
16+
17+
if(WIN32 AND NOT CYGWIN)
18+
SET(GD_NAMES ${GD_NAMES} bgd)
19+
else(WIN32)
20+
SET(GD_NAMES ${GD_NAMES} gd)
21+
endif(WIN32 AND NOT CYGWIN)
22+
23+
FIND_LIBRARY(GD_LIBRARY
24+
NAMES ${GD_NAMES}
25+
PATHS /usr/lib64 /usr/lib /usr/local/lib
26+
)
27+
28+
IF (GD_LIBRARY AND GD_INCLUDE_DIR)
29+
SET(GD_LIBRARIES ${GD_LIBRARY})
30+
SET(GD_FOUND "YES")
31+
ELSE (GD_LIBRARY AND GD_INCLUDE_DIR)
32+
SET(GD_FOUND "NO")
33+
ENDIF (GD_LIBRARY AND GD_INCLUDE_DIR)
34+
message("Found GD: ${GD_FOUND}")
35+
IF (GD_FOUND)
36+
IF (WIN32 AND NOT CYGWIN)
37+
SET(GD_SUPPORTS_PNG ON)
38+
SET(GD_SUPPORTS_JPEG ON)
39+
SET(GD_SUPPORTS_GIF ON)
40+
get_filename_component(GD_LIBRARY_DIR ${GD_LIBRARY} PATH)
41+
ELSE (WIN32 AND NOT CYGWIN)
42+
INCLUDE(CheckLibraryExists)
43+
GET_FILENAME_COMPONENT(GD_LIB_PATH ${GD_LIBRARY} PATH)
44+
GET_FILENAME_COMPONENT(GD_LIB ${GD_LIBRARY} NAME)
45+
46+
CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImagePng" "${GD_LIB_PATH}" GD_SUPPORTS_PNG)
47+
IF (GD_SUPPORTS_PNG)
48+
find_package(PNG)
49+
IF (PNG_FOUND)
50+
SET(GD_LIBRARIES ${GD_LIBRARIES} ${PNG_LIBRARIES})
51+
SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${PNG_INCLUDE_DIR})
52+
ELSE (PNG_FOUND)
53+
SET(GD_SUPPORTS_PNG "NO")
54+
ENDIF (PNG_FOUND)
55+
ENDIF (GD_SUPPORTS_PNG)
56+
57+
CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImageJpeg" "${GD_LIB_PATH}" GD_SUPPORTS_JPEG)
58+
IF (GD_SUPPORTS_JPEG)
59+
find_package(JPEG)
60+
IF (JPEG_FOUND)
61+
SET(GD_LIBRARIES ${GD_LIBRARIES} ${JPEG_LIBRARIES})
62+
SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${JPEG_INCLUDE_DIR})
63+
ELSE (JPEG_FOUND)
64+
SET(GD_SUPPORTS_JPEG "NO")
65+
ENDIF (JPEG_FOUND)
66+
ENDIF (GD_SUPPORTS_JPEG)
67+
68+
CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImageGif" "${GD_LIB_PATH}" GD_SUPPORTS_GIF)
69+
70+
# Trim the list of include directories
71+
SET(GDINCTRIM)
72+
FOREACH(GD_DIR ${GD_INCLUDE_DIR})
73+
SET(GD_TMP_FOUND OFF)
74+
FOREACH(GD_TRIMMED ${GDINCTRIM})
75+
IF ("${GD_DIR}" STREQUAL "${GD_TRIMMED}")
76+
SET(GD_TMP_FOUND ON)
77+
ENDIF ("${GD_DIR}" STREQUAL "${GD_TRIMMED}")
78+
ENDFOREACH(GD_TRIMMED ${GDINCTRIM})
79+
IF (NOT GD_TMP_FOUND)
80+
SET(GDINCTRIM "${GDINCTRIM}" "${GD_DIR}")
81+
ENDIF (NOT GD_TMP_FOUND)
82+
ENDFOREACH(GD_DIR ${GD_INCLUDE_DIR})
83+
SET(GD_INCLUDE_DIR ${GDINCTRIM})
84+
85+
SET(GD_LIBRARY_DIR)
86+
87+
# Generate trimmed list of library directories and list of libraries
88+
FOREACH(GD_LIB ${GD_LIBRARIES})
89+
GET_FILENAME_COMPONENT(GD_NEXTLIBDIR ${GD_LIB} PATH)
90+
SET(GD_TMP_FOUND OFF)
91+
FOREACH(GD_LIBDIR ${GD_LIBRARY_DIR})
92+
IF ("${GD_NEXTLIBDIR}" STREQUAL "${GD_LIBDIR}")
93+
SET(GD_TMP_FOUND ON)
94+
ENDIF ("${GD_NEXTLIBDIR}" STREQUAL "${GD_LIBDIR}")
95+
ENDFOREACH(GD_LIBDIR ${GD_LIBRARIES})
96+
IF (NOT GD_TMP_FOUND)
97+
SET(GD_LIBRARY_DIR "${GD_LIBRARY_DIR}" "${GD_NEXTLIBDIR}")
98+
ENDIF (NOT GD_TMP_FOUND)
99+
ENDFOREACH(GD_LIB ${GD_LIBRARIES})
100+
ENDIF (WIN32 AND NOT CYGWIN)
101+
ENDIF (GD_FOUND)
102+
103+
IF (GD_FOUND)
104+
IF (NOT GD_FIND_QUIETLY)
105+
MESSAGE(STATUS "Found GD: ${GD_LIBRARY}")
106+
ENDIF (NOT GD_FIND_QUIETLY)
107+
ELSE (GD_FOUND)
108+
IF (GD_FIND_REQUIRED)
109+
MESSAGE(FATAL_ERROR "Could not find GD library")
110+
ENDIF (GD_FIND_REQUIRED)
111+
ENDIF (GD_FOUND)
112+
113+
MARK_AS_ADVANCED(
114+
GD_LIBRARY
115+
GD_LIBRARIES
116+
GD_INCLUDE_DIR
117+
GD_LIBRARY_DIR
118+
GD_SUPPORTS_PNG
119+
GD_SUPPORTS_JPEG
120+
GD_SUPPORTS_GIF
121+
)

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-
No formats are currently supported, but that will change soon.
4+
Only JPEG is currently supported, but support for PNG is coming soon.
55

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

include/scratchcpp/iimageformat.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@ namespace libscratchcpp
1010
/*! A typedef for unsigned int. Holds the RGBA values. */
1111
using Rgb = unsigned int;
1212

13+
/*! Returns the red component of the quadruplet rgb. */
14+
inline constexpr int red(Rgb rgb)
15+
{
16+
return ((rgb >> 16) & 0xff);
17+
}
18+
19+
/*! Returns the green component of the quadruplet rgb. */
20+
inline constexpr int green(Rgb rgb)
21+
{
22+
return ((rgb >> 8) & 0xff);
23+
}
24+
25+
/*! Returns the blue component of the quadruplet rgb. */
26+
inline constexpr int blue(Rgb rgb)
27+
{
28+
return (rgb & 0xff);
29+
}
30+
31+
/*! Returns the alpha component of the quadruplet rgb. */
32+
inline constexpr int alpha(Rgb rgb)
33+
{
34+
return rgb >> 24;
35+
}
36+
37+
/*! Creates an RGB triplet from the given color components. */
38+
inline constexpr Rgb rgb(int r, int g, int b)
39+
{
40+
return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu);
41+
}
42+
43+
/*! Creates an RGBA quadruplet from the given color components. */
44+
inline constexpr Rgb rgba(int r, int g, int b, int a)
45+
{
46+
return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu);
47+
}
48+
1349
/*! \brief The IImageFormat class is an interface for image format implementation. */
1450
class LIBSCRATCHCPP_EXPORT IImageFormat
1551
{

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+
}

0 commit comments

Comments
 (0)