Skip to content

Commit

Permalink
Add a new scaleHint to ImageData::load
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed May 23, 2024
1 parent 4938d71 commit 6f94554
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ImageDataWebP.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Jan Niklas Hasse <jhasse@bixense.com>
// Copyright 2021-2024 Jan Niklas Hasse <jhasse@bixense.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#pragma once

Expand All @@ -24,8 +24,8 @@ class ImageDataWebP : public ImageData {

/// we might scale the image using WebP built-in filter. These functions will return the
/// original image size, not the size of the resulting pixel buffer.
int getImageWidth() const;
int getImageHeight() const;
int getImageWidth() const override;
int getImageHeight() const override;

private:
#ifndef __EMSCRIPTEN__
Expand Down
6 changes: 3 additions & 3 deletions src/jngl/ImageData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace jngl {

std::unique_ptr<ImageData> ImageData::load(const std::string& filename) {
std::unique_ptr<ImageData> ImageData::load(const std::string& filename, double scaleHint) {
auto fullFilename = pathPrefix + filename;
const char* extensions[] = {
#ifndef NOPNG
Expand All @@ -41,8 +41,8 @@ std::unique_ptr<ImageData> ImageData::load(const std::string& filename) {
},
#endif
#ifndef NOWEBP
[](std::string filename, FILE* file) {
return std::make_unique<ImageDataWebP>(std::move(filename), file, 1);
[scaleHint](std::string filename, FILE* file) {
return std::make_unique<ImageDataWebP>(std::move(filename), file, scaleHint);
},
#endif
};
Expand Down
21 changes: 15 additions & 6 deletions src/jngl/ImageData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class ImageData {
/// Passing a filename will load the specified \a filename
///
/// PNG and WebP files are supported.
static std::unique_ptr<ImageData> load(const std::string& filename);
///
/// \a scaleHint might be completely ignored, compare getImageWidth with getWidth after loading
/// to check.
static std::unique_ptr<ImageData> load(const std::string& filename, double scaleHint = 1.);

virtual ~ImageData() = default;

Expand All @@ -25,22 +28,28 @@ class ImageData {
/// Returns the height of the image in pixels
virtual int getHeight() const = 0;

/// If the image has been scaled by getScaleFactor() this will return the original image width
virtual int getImageWidth() const = 0;
/// If the image has been scaled by getScaleFactor() this will return the original image height
virtual int getImageHeight() const = 0;

/// RGBA values ordered row-major
///
/// To access the x=5, y=7 pixel's green value:
/// `img.pixels()[5 * 4 + 7 * img.getWidth() * 4 + 1]`
/// `img->pixels()[5 * 4 + 7 * img->getWidth() * 4 + 1]`
///
/// This function doesn't create the underlying data or loads the image (that has already
/// happened in ImageData::load), so you may call this as often as you like.
///
/// Example:
/// \code
/// auto img = ImageData::load("foo");
/// size_t x = 5;
/// size_t y = 7;
/// uint8_t r = img.pixels()[x * 4 + y * img.getWidth() * 4];
/// uint8_t g = img.pixels()[x * 4 + y * img.getWidth() * 4 + 1];
/// uint8_t b = img.pixels()[x * 4 + y * img.getWidth() * 4 + 2];
/// uint8_t a = img.pixels()[x * 4 + y * img.getWidth() * 4 + 3];
/// uint8_t r = img->pixels()[x * 4 + y * img->getWidth() * 4];
/// uint8_t g = img->pixels()[x * 4 + y * img->getWidth() * 4 + 1];
/// uint8_t b = img->pixels()[x * 4 + y * img->getWidth() * 4 + 2];
/// uint8_t a = img->pixels()[x * 4 + y * img->getWidth() * 4 + 3];
/// \endcode
virtual const uint8_t* pixels() const = 0;
};
Expand Down
10 changes: 9 additions & 1 deletion src/png/ImageDataPNG.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Jan Niklas Hasse <jhasse@bixense.com>
// Copyright 2021-2024 Jan Niklas Hasse <jhasse@bixense.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#include "ImageDataPNG.hpp"

Expand Down Expand Up @@ -77,4 +77,12 @@ int ImageDataPNG::getHeight() const {
return static_cast<int>(y);
}

int ImageDataPNG::getImageWidth() const {
return static_cast<int>(x);
}

int ImageDataPNG::getImageHeight() const {
return static_cast<int>(y);
}

} // namespace jngl
7 changes: 6 additions & 1 deletion src/png/ImageDataPNG.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Jan Niklas Hasse <jhasse@bixense.com>
// Copyright 2021-2024 Jan Niklas Hasse <jhasse@bixense.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#pragma once

Expand All @@ -22,6 +22,11 @@ class ImageDataPNG : public ImageData {
int getWidth() const override;
int getHeight() const override;

/// returns getWidth() as PNGs don't support scaling
int getImageWidth() const override;
/// returns getHeight() as PNGs don't support scaling
int getImageHeight() const override;

private:
const static unsigned int PNG_BYTES_TO_CHECK = 4;
std::vector<uint8_t> imageData;
Expand Down

0 comments on commit 6f94554

Please sign in to comment.