Skip to content

Commit

Permalink
Added rive_decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Dec 17, 2024
1 parent 9c10174 commit 999af03
Show file tree
Hide file tree
Showing 12 changed files with 891 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmake/yup_modules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,11 @@ function (_yup_add_default_modules modules_path)
yup_add_module (${modules_path}/thirdparty/glad)
yup_add_module (${modules_path}/thirdparty/harfbuzz)
yup_add_module (${modules_path}/thirdparty/libpng)
yup_add_module (${modules_path}/thirdparty/libwebp)
yup_add_module (${modules_path}/thirdparty/sheenbidi)
yup_add_module (${modules_path}/thirdparty/yoga_library)
yup_add_module (${modules_path}/thirdparty/rive)
yup_add_module (${modules_path}/thirdparty/rive_decoders)
yup_add_module (${modules_path}/thirdparty/rive_renderer)
yup_add_module (${modules_path}/thirdparty/oboe_library)

Expand Down
78 changes: 78 additions & 0 deletions thirdparty/rive_decoders/include/rive/decoders/bitmap_decoder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2023 Rive
*/

#ifndef _RIVE_BITMAP_DECODER_HPP_
#define _RIVE_BITMAP_DECODER_HPP_

#include <memory>
#include <vector>

/// Bitmap will always take ownership of the bytes it is constructed with.
class Bitmap
{
public:
enum class PixelFormat : uint8_t
{
RGB,
RGBA
};

Bitmap(uint32_t width,
uint32_t height,
PixelFormat pixelFormat,
std::unique_ptr<const uint8_t[]> bytes);

Bitmap(uint32_t width,
uint32_t height,
PixelFormat pixelFormat,
const uint8_t* bytes);

private:
uint32_t m_Width;
uint32_t m_Height;
PixelFormat m_PixelFormat;
std::unique_ptr<const uint8_t[]> m_Bytes;

public:
uint32_t width() const { return m_Width; }
uint32_t height() const { return m_Height; }
PixelFormat pixelFormat() const { return m_PixelFormat; }
const uint8_t* bytes() const { return m_Bytes.get(); }
std::unique_ptr<const uint8_t[]> detachBytes()
{
return std::move(m_Bytes);
}
size_t byteSize() const;
size_t byteSize(PixelFormat format) const;
size_t bytesPerPixel(PixelFormat format) const;

enum class ImageType
{
png,
jpeg,
webp,
};

using BitmapDecoder = std::unique_ptr<Bitmap> (*)(const uint8_t bytes[],
size_t byteCount);

struct ImageFormat
{
const char* name;
ImageType type;
std::vector<uint8_t> fingerprint;
BitmapDecoder decodeImage;
};

static const ImageFormat* RecognizeImageFormat(const uint8_t bytes[],
size_t byteCount);

static std::unique_ptr<Bitmap> decode(const uint8_t bytes[],
size_t byteCount);

// Change the pixel format (note this will resize bytes).
void pixelFormat(PixelFormat format);
};

#endif
39 changes: 39 additions & 0 deletions thirdparty/rive_decoders/rive_decoders.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2024 - [email protected]
YUP is an open source library subject to open-source licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/

#include "rive_decoders.h"

#include <libpng/libpng.h>

#include "source/bitmap_decoder.cpp"
#include "source/bitmap_decoder_thirdparty.cpp"

#if RIVE_JPEG
#include "source/decode_jpeg.cpp"
#endif

#if RIVE_PNG
#include "source/decode_png.cpp"
#endif

#if RIVE_WEBP
#include "source/decode_webp.cpp"
#endif
47 changes: 47 additions & 0 deletions thirdparty/rive_decoders/rive_decoders.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2024 - [email protected]
YUP is an open source library subject to open-source licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/

/*
==============================================================================
BEGIN_JUCE_MODULE_DECLARATION
ID: rive_decoders
vendor: rive
version: 1.0
name: Rive Decoders.
description: The Rive Decoders is a companion library for ratser image decoding.
website: https://github.com/rive-app/rive-runtime
license: MIT
dependencies: libpng
searchpaths: include
defines: RIVE_PNG=1
enableARC: 1
END_JUCE_MODULE_DECLARATION
==============================================================================
*/

#pragma once

#include <libpng/libpng.h>
24 changes: 24 additions & 0 deletions thirdparty/rive_decoders/rive_decoders.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2024 - [email protected]
YUP is an open source library subject to open-source licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/

#include "rive_decoders.cpp"

// #include "source/bitmap_decoder_cg.mm"
71 changes: 71 additions & 0 deletions thirdparty/rive_decoders/source/bitmap_decoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 Rive
*/

#include "rive/decoders/bitmap_decoder.hpp"
#include "rive/rive_types.hpp"
#include <stdio.h>
#include <string.h>
#include <vector>

Bitmap::Bitmap(uint32_t width,
uint32_t height,
PixelFormat pixelFormat,
std::unique_ptr<const uint8_t[]> bytes) :
m_Width(width),
m_Height(height),
m_PixelFormat(pixelFormat),
m_Bytes(std::move(bytes))
{}

Bitmap::Bitmap(uint32_t width,
uint32_t height,
PixelFormat pixelFormat,
const uint8_t* bytes) :
Bitmap(width, height, pixelFormat, std::unique_ptr<const uint8_t[]>(bytes))
{}

size_t Bitmap::bytesPerPixel(PixelFormat format) const
{
switch (format)
{
case PixelFormat::RGB:
return 3;
case PixelFormat::RGBA:
return 4;
}
RIVE_UNREACHABLE();
}

size_t Bitmap::byteSize(PixelFormat format) const
{
return m_Width * m_Height * bytesPerPixel(format);
}

size_t Bitmap::byteSize() const { return byteSize(m_PixelFormat); }

void Bitmap::pixelFormat(PixelFormat format)
{
if (format == m_PixelFormat)
{
return;
}
auto nextByteSize = byteSize(format);
auto nextBytes = std::unique_ptr<uint8_t[]>(new uint8_t[nextByteSize]);

size_t fromBytesPerPixel = bytesPerPixel(m_PixelFormat);
size_t toBytesPerPixel = bytesPerPixel(format);
int writeIndex = 0;
int readIndex = 0;
for (uint32_t i = 0; i < m_Width * m_Height; i++)
{
for (size_t j = 0; j < toBytesPerPixel; j++)
{
nextBytes[writeIndex++] =
j < fromBytesPerPixel ? m_Bytes[readIndex++] : 255;
}
}

m_Bytes = std::move(nextBytes);
m_PixelFormat = format;
}
Loading

0 comments on commit 999af03

Please sign in to comment.