Skip to content

Commit

Permalink
BINDINGS/C++: Added more image ctors (closes #211)
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed May 12, 2024
1 parent f3f8291 commit 30bf775
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/bindings/sail-c++/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ image::image(const std::string &path)
load(path);
}

image::image(SailPixelFormat pixel_format, unsigned width, unsigned height)
: image()
{
set_dimensions(width, height);
set_pixel_format(pixel_format);
set_bytes_per_line_auto();

SAIL_TRY_OR_EXECUTE(sail_malloc(this->height() * bytes_per_line(), &d->sail_image->pixels),
/* on error */ throw std::bad_alloc());
}

image::image(SailPixelFormat pixel_format, unsigned width, unsigned height, unsigned bytes_per_line)
: image()
{
set_dimensions(width, height);
set_pixel_format(pixel_format);
set_bytes_per_line(bytes_per_line);

SAIL_TRY_OR_EXECUTE(sail_malloc(this->height() * this->bytes_per_line(), &d->sail_image->pixels),
/* on error */ throw std::bad_alloc());
}

image::image(void *pixels, SailPixelFormat pixel_format, unsigned width, unsigned height)
: image()
{
Expand Down
13 changes: 13 additions & 0 deletions src/bindings/sail-c++/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class SAIL_EXPORT image
*/
explicit image(const std::string &path);

/*
* Constructs a new image out of the specified image properties and allocates the pixels
* for the specified pixel format. The pixels have uninitialized values.
* Assumes the pixels have no padding bytes in the end of every scan line.
*/
image(SailPixelFormat pixel_format, unsigned width, unsigned height);

/*
* Constructs a new image out of the specified image properties and allocates the pixels
* for the specified pixel format. The pixels have uninitialized values.
*/
image(SailPixelFormat pixel_format, unsigned width, unsigned height, unsigned bytes_per_line);

/*
* Constructs a new image out of the specified image properties and the pixels.
* Assumes the pixels have no padding bytes in the end of every scan line. The pixels
Expand Down
1 change: 1 addition & 0 deletions tests/bindings/c++/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sail_test(TARGET can-load-c++ SOURCES can-load.cpp LINK sail-c++)
sail_test(TARGET iccp-c++ SOURCES iccp.cpp LINK sail-c++)
sail_test(TARGET image-c++ SOURCES image.cpp LINK sail-c++)
sail_test(TARGET load-features-c++ SOURCES load_features.cpp LINK sail-c++)
sail_test(TARGET load-options-c++ SOURCES load_options.cpp LINK sail-c++)
sail_test(TARGET meta-data-c++ SOURCES meta_data.cpp LINK sail-c++)
Expand Down
100 changes: 100 additions & 0 deletions tests/bindings/c++/image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* This file is part of SAIL (https://github.com/HappySeaFox/sail)
Copyright (c) 2024 Dmitry Baryshev
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <sail-c++/sail-c++.h>

#include "munit.h"

static MunitResult test_image_create(const MunitParameter params[], void *user_data) {
(void)params;
(void)user_data;

{
sail::image image;
munit_assert(image.pixel_format() == SAIL_PIXEL_FORMAT_UNKNOWN);
munit_assert_null(image.pixels());
munit_assert_false(image.is_valid());
}

{
sail::image image(SAIL_PIXEL_FORMAT_BPP24_RGB, 16, 16);
munit_assert(image.pixel_format() == SAIL_PIXEL_FORMAT_BPP24_RGB);
munit_assert_uint(image.width(), ==, 16);
munit_assert_uint(image.height(), ==, 16);
munit_assert_not_null(image.pixels());
munit_assert_true(image.is_valid());
}

{
sail::image image(SAIL_PIXEL_FORMAT_BPP24_RGB, 16, 16, 50);
munit_assert(image.pixel_format() == SAIL_PIXEL_FORMAT_BPP24_RGB);
munit_assert_uint(image.width(), ==, 16);
munit_assert_uint(image.height(), ==, 16);
munit_assert_uint(image.bytes_per_line(), ==, 50);
munit_assert_not_null(image.pixels());
munit_assert_true(image.is_valid());
}

{
void *pixels = "abcdef";
sail::image image(pixels, SAIL_PIXEL_FORMAT_BPP24_RGB, 16, 16);
munit_assert(image.pixel_format() == SAIL_PIXEL_FORMAT_BPP24_RGB);
munit_assert_uint(image.width(), ==, 16);
munit_assert_uint(image.height(), ==, 16);
munit_assert_ptr_equal(image.pixels(), pixels);
munit_assert_true(image.is_valid());
}

{
void *pixels = "abcdef";
sail::image image(pixels, SAIL_PIXEL_FORMAT_BPP24_RGB, 16, 16, 50);
munit_assert(image.pixel_format() == SAIL_PIXEL_FORMAT_BPP24_RGB);
munit_assert_uint(image.width(), ==, 16);
munit_assert_uint(image.height(), ==, 16);
munit_assert_uint(image.bytes_per_line(), ==, 50);
munit_assert_ptr_equal(image.pixels(), pixels);
munit_assert_true(image.is_valid());
}

return MUNIT_OK;
}

static MunitTest test_suite_tests[] = {
{ (char *)"/create", test_image_create, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },

{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

static const MunitSuite test_suite = {
(char *)"/bindings/c++/image",
test_suite_tests,
NULL,
1,
MUNIT_SUITE_OPTION_NONE
};

int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
return munit_suite_main(&test_suite, NULL, argc, argv);
}

0 comments on commit 30bf775

Please sign in to comment.