Skip to content

Commit

Permalink
add-raw-pixel-buffer-to-image: addressed review comments
Browse files Browse the repository at this point in the history
- Moved to use function to determine api specific alpha/byte format
  type.
- Updated infra submodule to latest master, in order to have
  `is_little_endian` function for example.
- Updated copyright
  • Loading branch information
bnert committed Sep 7, 2020
1 parent 4f615e4 commit 5ed6085
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
[submodule "lib/infra"]
path = lib/infra
url = https://github.com/cycfi/infra
branch = master
12 changes: 2 additions & 10 deletions examples/image_pixmap_chessboard.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
/*=============================================================================
Copyright (c) 2016-2020 Joel de Guzman
Copyright (c) 2016-2020 Brent Soles
Distributed under the MIT License (https://opensource.org/licenses/MIT)
=============================================================================*/
#include "app.hpp"

using namespace cycfi::artist;

bool constexpr is_little_endian() {
union {
uint32_t i;
char c[4];
} cmp = {0x01020304};
return !(cmp.c[0] == 1);
}

int constexpr rows = 8;
int constexpr cols = 8;
int constexpr square_side = 100;
Expand All @@ -25,7 +17,7 @@ size_t constexpr pix_buf_size = rows * cols * square_area;

uint32_t constexpr white = 0xffffffff;
// on little endian systems RGBA is formatted as ABGR for values
std::function<uint32_t()> black = []() { return is_little_endian() ? 0xff000000 : 0x000000ff; };
std::function<uint32_t()> black = []() { return cycfi::is_little_endian() ? 0xff000000 : 0x000000ff; };

auto constexpr window_size = extent{
static_cast<float>(cols * square_side),
Expand Down
25 changes: 17 additions & 8 deletions lib/impl/skia/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ using std::pair;

namespace cycfi::artist
{
static const map<img_fmt, pair<SkAlphaType, SkColorType>> _img_fmt_map_to_api_type = {
{img_fmt::INVALID, {SkAlphaType::kUnknown_SkAlphaType, SkColorType::kUnknown_SkColorType}},
{img_fmt::GRAY8, {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kGray_8_SkColorType}},
{img_fmt::RGB16, {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGB_565_SkColorType}},
{img_fmt::RGB32, {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGB_888x_SkColorType}},
{img_fmt::RGBA32, {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGBA_8888_SkColorType}}
};
pair<SkAlphaType, SkColorType> _map_img_fmt_to_api_type(const img_fmt& fmt)
{
switch(fmt)
{
case img_fmt::GRAY8:
return {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kGray_8_SkColorType};
case img_fmt::RGB16:
return {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGB_565_SkColorType};
case img_fmt::RGB32:
return {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGB_888x_SkColorType};
case img_fmt::RGBA32:
return {SkAlphaType::kOpaque_SkAlphaType, SkColorType::kRGBA_8888_SkColorType};
default:
return {SkAlphaType::kUnknown_SkAlphaType, SkColorType::kUnknown_SkColorType};
}
}

image::image(extent size)
: _impl{ new artist::image_impl(size) }
Expand Down Expand Up @@ -70,7 +79,7 @@ namespace cycfi::artist
SkAlphaType alpha_fmt;
SkColorType byte_fmt;
try {
std::tie(alpha_fmt, byte_fmt) = _img_fmt_map_to_api_type.at(fmt);
std::tie(alpha_fmt, byte_fmt) = _map_img_fmt_to_api_type(fmt);
} catch(std::exception& /* e */) {
throw std::runtime_error{ "Error: unrecognized format." };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/infra

0 comments on commit 5ed6085

Please sign in to comment.