[core] For binary image data use uint8_t, not char

This commit is contained in:
John Firebaugh
2015-11-23 13:37:00 -08:00
parent a620912176
commit 36081f6486
13 changed files with 31 additions and 34 deletions

View File

@@ -3,17 +3,15 @@
#include <mbgl/util/noncopyable.hpp>
#include <string>
#include <cstdint>
namespace mbgl {
class StillImage : util::noncopyable {
public:
uint16_t width = 0;
uint16_t height = 0;
using Pixel = uint32_t;
std::unique_ptr<Pixel[]> pixels;
size_t width = 0;
size_t height = 0;
std::unique_ptr<uint8_t[]> pixels;
};
}

View File

@@ -31,11 +31,11 @@ struct ImageReader : private noncopyable
virtual unsigned height() const=0;
virtual bool hasAlpha() const=0;
virtual bool premultipliedAlpha() const=0;
virtual void read(unsigned x,unsigned y, unsigned width, unsigned height, char* image)=0;
virtual void read(unsigned x, unsigned y, unsigned width, unsigned height, uint8_t* image)=0;
virtual ~ImageReader() {}
};
std::unique_ptr<ImageReader> getImageReader(char const* data, size_t size);
std::unique_ptr<ImageReader> getImageReader(const uint8_t* data, size_t size);
}}

View File

@@ -51,13 +51,13 @@ private:
unsigned width_;
unsigned height_;
public:
JpegReader(char const* data, size_t size);
JpegReader(const uint8_t* data, size_t size);
~JpegReader();
unsigned width() const;
unsigned height() const;
inline bool hasAlpha() const { return false; }
inline bool premultipliedAlpha() const { return true; }
void read(unsigned x,unsigned y, unsigned w, unsigned h, char *image);
void read(unsigned x, unsigned y, unsigned w, unsigned h, uint8_t* image);
private:
void init();
static void on_error(j_common_ptr cinfo);

View File

@@ -49,13 +49,13 @@ private:
int color_type_;
bool has_alpha_;
public:
PngReader(char const* data, std::size_t size);
PngReader(const uint8_t* data, std::size_t size);
~PngReader();
unsigned width() const;
unsigned height() const;
inline bool hasAlpha() const { return has_alpha_; }
bool premultipliedAlpha() const { return true; } // png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2)
void read(unsigned x,unsigned y, unsigned width, unsigned height, char * image);
void read(unsigned x,unsigned y, unsigned width, unsigned height, uint8_t* image);
private:
void init();
static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length);

View File

@@ -7,14 +7,14 @@
namespace mbgl {
namespace util {
std::string compress_png(int width, int height, const void *rgba);
std::string compress_png(size_t width, size_t height, const uint8_t* rgba);
class Image {
public:
explicit Image(const std::string &img);
explicit Image(const std::string& img);
inline const char *getData() const { return img.get(); }
inline const uint8_t* getData() const { return img.get(); }
inline uint32_t getWidth() const { return width; }
inline uint32_t getHeight() const { return height; }
inline operator bool() const { return img && width && height; }
@@ -24,7 +24,7 @@ private:
uint32_t width = 0, height = 0;
// the raw image data
std::unique_ptr<char[]> img;
std::unique_ptr<uint8_t[]> img;
};

View File

@@ -11,7 +11,7 @@
namespace mbgl {
namespace util {
std::string compress_png(int width, int height, const void *rgba) {
std::string compress_png(size_t width, size_t height, const uint8_t* rgba) {
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, width * height * 4, NULL);
if (!provider) {
return "";
@@ -97,7 +97,7 @@ Image::Image(const std::string &source_data) {
height = uint32_t(CGImageGetHeight(image));
CGRect rect = {{ 0, 0 }, { static_cast<CGFloat>(width), static_cast<CGFloat>(height) }};
img = std::make_unique<char[]>(width * height * 4);
img = std::make_unique<uint8_t[]>(width * height * 4);
CGContextRef context = CGBitmapContextCreate(img.get(), width, height, 8, width * 4,
color_space, kCGImageAlphaPremultipliedLast);
if (!context) {

View File

@@ -179,7 +179,7 @@ std::unique_ptr<StillImage> HeadlessView::readStillImage() {
auto image = std::make_unique<StillImage>();
image->width = w;
image->height = h;
image->pixels = std::make_unique<uint32_t[]>(w * h);
image->pixels = std::make_unique<uint8_t[]>(w * h * 4);
MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels.get()));

View File

@@ -27,7 +27,7 @@ const static bool png_version_check = []() {
namespace mbgl {
namespace util {
std::string compress_png(int width, int height, const void *rgba) {
std::string compress_png(size_t width, size_t height, const uint8_t* rgba) {
png_voidp error_ptr = 0;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, error_ptr, NULL, NULL);
if (!png_ptr) {
@@ -63,7 +63,7 @@ std::string compress_png(int width, int height, const void *rgba) {
png_bytep *rows = nullptr;
} pointers(height);
for (int i = 0; i < height; i++) {
for (size_t i = 0; i < height; i++) {
pointers.rows[i] = (png_bytep)((png_bytep)rgba + width * 4 * i);
}
@@ -78,10 +78,10 @@ Image::Image(std::string const& data)
{
try
{
auto reader = getImageReader(data.c_str(), data.size());
auto reader = getImageReader(reinterpret_cast<const uint8_t*>(data.c_str()), data.size());
width = reader->width();
height = reader->height();
img = std::make_unique<char[]>(width * height * 4);
img = std::make_unique<uint8_t[]>(width * height * 4);
reader->read(0, 0, width, height, img.get());
}
catch (ImageReaderException const& ex)

View File

@@ -7,7 +7,7 @@
namespace mbgl { namespace util {
inline boost::optional<std::string> type_from_bytes(char const* data, size_t size)
inline boost::optional<std::string> type_from_bytes(const uint8_t* data, size_t size)
{
using result_type = boost::optional<std::string>;
if (size >= 4)
@@ -42,9 +42,9 @@ inline boost::optional<std::string> type_from_bytes(char const* data, size_t siz
return result_type();
}
std::unique_ptr<ImageReader> getImageReader(char const* data, size_t size)
std::unique_ptr<ImageReader> getImageReader(const uint8_t* data, size_t size)
{
boost::optional<std::string> type = type_from_bytes(data,size);
boost::optional<std::string> type = type_from_bytes(data, size);
if (type)
{
if (*type == "png")

View File

@@ -13,8 +13,8 @@ namespace mbgl { namespace util {
// ctor
template <typename T>
JpegReader<T>::JpegReader(char const* data, size_t size)
: source_(data, size),
JpegReader<T>::JpegReader(const uint8_t* data, size_t size)
: source_(reinterpret_cast<const char*>(data), size),
stream_(source_),
width_(0),
height_(0)
@@ -145,7 +145,7 @@ unsigned JpegReader<T>::height() const
}
template <typename T>
void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char* image)
void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image)
{
stream_.clear();
stream_.seekg(0, std::ios_base::beg);

View File

@@ -40,8 +40,8 @@ void PngReader<T>::png_read_data(png_structp png_ptr, png_bytep data, png_size_t
}
template <typename T>
PngReader<T>::PngReader(char const* data, std::size_t size)
: source_(data,size),
PngReader<T>::PngReader(const uint8_t* data, std::size_t size)
: source_(reinterpret_cast<const char*>(data), size),
stream_(source_),
width_(0),
height_(0),
@@ -115,7 +115,7 @@ unsigned PngReader<T>::height() const
}
template <typename T>
void PngReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * image)
void PngReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image)
{
stream_.clear();
stream_.seekg(0, std::ios_base::beg);

View File

@@ -360,8 +360,7 @@ void NodeMap::renderFinished() {
cb->Call(1, argv);
} else if (img) {
v8::Local<v8::Object> pixels = Nan::NewBuffer(
reinterpret_cast<char *>(img->pixels.get()),
size_t(img->width) * size_t(img->height) * sizeof(mbgl::StillImage::Pixel),
reinterpret_cast<char *>(img->pixels.get()), img->width * img->height * 4,
// Retain the StillImage object until the buffer is deleted.
[](char *, void *hint) {

View File

@@ -42,7 +42,7 @@ TEST(Bilinear, Scaling) {
util::bilinearScale(srcData, srcSize, { 252, 380, 12, 12 }, dstData, dstSize, { 18, 90, 24, 24 }, false);
const std::string data { reinterpret_cast<char *>(dstData), dstSize.x * dstSize.y * sizeof(uint32_t) };
util::write_file("test/fixtures/sprites/atlas_actual.png", util::compress_png(dstSize.x, dstSize.y, dstData));
util::write_file("test/fixtures/sprites/atlas_actual.png", util::compress_png(dstSize.x, dstSize.y, reinterpret_cast<uint8_t *>(dstData)));
util::write_file("test/fixtures/sprites/atlas_actual.bin", util::compress(data));
const std::string reference = util::decompress(util::read_file("test/fixtures/sprites/atlas_reference.bin"));