-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// QOI.fu - encoder and decoder of the "Quite OK Image" format | ||
// | ||
// Copyright (C) 2021-2024 Piotr Fusik | ||
// | ||
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef QOI_FU_CXX_H_INCLUDED_ | ||
#define QOI_FU_CXX_H_INCLUDED_ | ||
|
||
#define QOI_NO_STDIO | ||
#include "qoi.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
extern void* qoi_fu_cxx_encode(const void* data, const qoi_desc* desc, int* out_len); | ||
extern void* qoi_fu_cxx_decode(const void* data, int size, qoi_desc* desc, int channels); | ||
extern void qoi_fu_cxx_free(void* ptr); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif//QOI_FU_CXX_H_INCLUDED_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*.o | ||
/libqoi_fu_cxx.a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
libqoi_fu_cxx.a: qoi-fu.cpp.o QOI.cpp.o | ||
$(AR) rcs $@ $^ | ||
|
||
CXX_FLAGS:=-std=c++2a -O3 -march=native -mtune=native -Wall -Wextra -pedantic-errors -DNDEBUG | ||
|
||
qoi-fu.cpp.o: qoi-fu.cpp | ||
$(CXX) -c $(CXX_FLAGS) -I qoi-fu/transpiled -o$@ $< | ||
|
||
QOI.cpp.o: qoi-fu/transpiled/QOI.cpp | ||
$(CXX) -c $(CXX_FLAGS) -o$@ $< | ||
|
||
clean: | ||
$(RM) -f libqoi_fu_cxx.a qoi-fu.cpp.o QOI.cpp.o | ||
|
||
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include"QOI.hpp" | ||
#include<cassert> | ||
#include<stack> | ||
|
||
typedef struct{ | ||
unsigned int width; | ||
unsigned int height; | ||
unsigned char channels; | ||
unsigned char colorspace; | ||
}qoi_desc; | ||
|
||
template<typename Accessor, typename Accessor::type Member> | ||
struct access_private_member{ | ||
friend typename Accessor::type get(Accessor){ | ||
return Member; | ||
} | ||
}; | ||
|
||
struct encoder_accessor{ | ||
using type = std::shared_ptr<uint8_t[]> QOIEncoder::*; | ||
friend type get(encoder_accessor); | ||
}; | ||
|
||
struct decoder_accessor{ | ||
using type = std::shared_ptr<int[]> QOIDecoder::*; | ||
friend type get(decoder_accessor); | ||
}; | ||
|
||
template struct access_private_member<encoder_accessor, &QOIEncoder::encoded>; | ||
template struct access_private_member<decoder_accessor, &QOIDecoder::pixels>; | ||
|
||
namespace{ | ||
|
||
thread_local std::stack<std::shared_ptr<void>> qoi_fu_results; | ||
|
||
} | ||
|
||
extern "C"{ | ||
|
||
void* qoi_fu_cxx_encode(const void* data, const qoi_desc* desc, int* out_len){ | ||
QOIEncoder encoder; | ||
if(!encoder.encode(desc->width, desc->height, static_cast<const int*>(data), desc->channels == 4, desc->colorspace == 1)) | ||
return nullptr; | ||
*out_len = encoder.getEncodedSize(); | ||
auto result = std::move(encoder.*get(encoder_accessor{})); | ||
const auto ptr = result.get(); | ||
qoi_fu_results.push(std::move(result)); | ||
return ptr; | ||
} | ||
|
||
void* qoi_fu_cxx_decode(const void* data, int size, qoi_desc* desc, int){ | ||
QOIDecoder decoder; | ||
if(!decoder.decode(static_cast<const uint8_t*>(data), size)) | ||
return nullptr; | ||
desc->width = decoder.getWidth(); | ||
desc->height = decoder.getHeight(); | ||
desc->channels = decoder.hasAlpha() ? 4 : 3; | ||
desc->colorspace = decoder.isLinearColorspace() ? 1 : 0; | ||
auto result = std::move(decoder.*get(decoder_accessor{})); | ||
const auto ptr = result.get(); | ||
qoi_fu_results.push(std::move(result)); | ||
return ptr; | ||
} | ||
|
||
void qoi_fu_cxx_free([[maybe_unused]] void* ptr){ | ||
assert(ptr == qoi_fu_results.top().get()); | ||
qoi_fu_results.pop(); | ||
} | ||
|
||
} |