-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify the image operations in extensions library #831
Open
wenbingl
wants to merge
12
commits into
main
Choose a base branch
from
unimg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4d9c428
Unify the image operations in extensions library
wenbingl bf294e3
fix the build configuration issue
wenbingl 169fb15
More build fixings
wenbingl 3e4c03d
Fix the native image codec
wenbingl 0fbe523
Merge branch 'main' into unimg
wenbingl f9e8c7c
fix encode_image
wenbingl 7d891fa
Add bgr/rgb conversion for encoding image
wenbingl 6cf204c
parity check
wenbingl 4dca247
build break
wenbingl 79cc6fa
update PNG encoding parameters
wenbingl 1fea714
build break on Linux
wenbingl e4e32cc
using MSE to compare images
wenbingl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,40 +1,124 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "png.h" | ||
#include "jpeglib.h" | ||
#include "op_def_struct.h" | ||
#include "ext_status.h" | ||
|
||
#include "encode_image.hpp" | ||
|
||
#include <opencv2/imgcodecs.hpp> | ||
|
||
namespace ort_extensions { | ||
|
||
void KernelEncodeImage::Compute(const ortc::Tensor<uint8_t>& input, ortc::Tensor<uint8_t>& output) const { | ||
// Setup inputs | ||
const auto dimensions_bgr = input.Shape(); | ||
|
||
const auto& dimensions_bgr = input.Shape(); | ||
if (dimensions_bgr.size() != 3 || dimensions_bgr[2] != 3) { | ||
// expect {H, W, C} as that's the inverse of what decode_image produces. | ||
// we have no way to check if it's BGR or RGB though | ||
ORTX_CXX_API_THROW("[EncodeImage] requires rank 3 BGR input in channels last format.", ORT_INVALID_ARGUMENT); | ||
} | ||
|
||
// Get data & the length | ||
std::vector<int32_t> height_x_width{static_cast<int32_t>(dimensions_bgr[0]), // H | ||
static_cast<int32_t>(dimensions_bgr[1])}; // W | ||
const int color_space = 3; | ||
const uint8_t* bgr_data = input.Data(); | ||
unsigned char* outbuffer = nullptr; | ||
std::vector<uint8_t> png_buffer; | ||
size_t outsize = 0; | ||
|
||
auto rgb_data = std::make_unique<uint8_t[]>(height_x_width[0] * height_x_width[1] * color_space); | ||
for (int y = 0; y < height_x_width[0]; ++y) { | ||
for (int x = 0; x < height_x_width[1]; ++x) { | ||
rgb_data[(y * height_x_width[1] + x) * color_space + 0] = bgr_data[(y * height_x_width[1] + x) * color_space + 2]; | ||
rgb_data[(y * height_x_width[1] + x) * color_space + 1] = bgr_data[(y * height_x_width[1] + x) * color_space + 1]; | ||
rgb_data[(y * height_x_width[1] + x) * color_space + 2] = bgr_data[(y * height_x_width[1] + x) * color_space + 0]; | ||
} | ||
} | ||
|
||
if (extension_ == ".jpg") { | ||
struct jpeg_compress_struct cinfo; | ||
struct jpeg_error_mgr jerr; | ||
|
||
cinfo.err = jpeg_std_error(&jerr); | ||
jpeg_create_compress(&cinfo); | ||
jpeg_mem_dest(&cinfo, &outbuffer, &outsize); | ||
|
||
cinfo.image_width = height_x_width[1]; | ||
cinfo.image_height = height_x_width[0]; | ||
cinfo.input_components = color_space; | ||
cinfo.in_color_space = JCS_RGB; | ||
|
||
// compression parameters is compatible with opencv | ||
jpeg_set_defaults(&cinfo); | ||
jpeg_set_quality(&cinfo, 95, TRUE); | ||
cinfo.optimize_coding = FALSE; | ||
cinfo.restart_interval = 0; | ||
cinfo.q_scale_factor[0] = jpeg_quality_scaling(-1); | ||
cinfo.q_scale_factor[1] = jpeg_quality_scaling(-1); | ||
|
||
const int sampling_factor = 0x221111; // 4:2:0 IMWRITE_JPEG_SAMPLING_FACTOR_420 | ||
cinfo.comp_info[0].v_samp_factor = (sampling_factor >> 16 ) & 0xF; | ||
cinfo.comp_info[0].h_samp_factor = (sampling_factor >> 20 ) & 0xF; | ||
cinfo.comp_info[1].v_samp_factor = 1; | ||
cinfo.comp_info[1].h_samp_factor = 1; | ||
// jpeg_default_qtables( &cinfo, TRUE ); | ||
|
||
jpeg_start_compress(&cinfo, TRUE); | ||
|
||
// data is const uint8_t but opencv2 wants void*. | ||
const void* bgr_data = input.Data(); | ||
const cv::Mat bgr_image(height_x_width, CV_8UC3, const_cast<void*>(bgr_data)); | ||
JSAMPROW row_pointer[1]; | ||
while (cinfo.next_scanline < cinfo.image_height) { | ||
row_pointer[0] = (JSAMPROW)&rgb_data[cinfo.next_scanline * cinfo.image_width * color_space]; | ||
jpeg_write_scanlines(&cinfo, row_pointer, 1); | ||
} | ||
|
||
// don't know output size ahead of time so need to encode and then copy to output | ||
std::vector<uint8_t> encoded_image; | ||
if (!cv::imencode(extension_, bgr_image, encoded_image)) { | ||
ORTX_CXX_API_THROW("[EncodeImage] Image encoding failed.", ORT_INVALID_ARGUMENT); | ||
jpeg_finish_compress(&cinfo); | ||
jpeg_destroy_compress(&cinfo); | ||
} else if (extension_ == ".png") { | ||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); | ||
if (!png_ptr) { | ||
ORTX_CXX_API_THROW("[EncodeImage] PNG create write struct failed.", ORT_INVALID_ARGUMENT); | ||
} | ||
|
||
png_infop info_ptr = png_create_info_struct(png_ptr); | ||
if (!info_ptr) { | ||
png_destroy_write_struct(&png_ptr, nullptr); | ||
ORTX_CXX_API_THROW("[EncodeImage] PNG create info struct failed.", ORT_INVALID_ARGUMENT); | ||
} | ||
|
||
if (setjmp(png_jmpbuf(png_ptr))) { | ||
png_destroy_write_struct(&png_ptr, &info_ptr); | ||
ORTX_CXX_API_THROW("[EncodeImage] PNG encoding failed.", ORT_INVALID_ARGUMENT); | ||
} | ||
|
||
png_set_write_fn(png_ptr, &png_buffer, [](png_structp png_ptr, png_bytep data, png_size_t length) { | ||
auto p = reinterpret_cast<std::vector<uint8_t>*>(png_get_io_ptr(png_ptr)); | ||
p->insert(p->end(), data, data + length); | ||
}, nullptr); | ||
|
||
png_set_IHDR(png_ptr, info_ptr, height_x_width[1], height_x_width[0], 8, PNG_COLOR_TYPE_RGB, | ||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | ||
|
||
png_write_info(png_ptr, info_ptr); | ||
|
||
for (int y = 0; y < height_x_width[0]; ++y) { | ||
png_write_row(png_ptr, (png_bytep)&rgb_data[y * height_x_width[1] * color_space]); | ||
} | ||
|
||
png_write_end(png_ptr, info_ptr); | ||
png_destroy_write_struct(&png_ptr, &info_ptr); | ||
|
||
outbuffer = png_buffer.data(); | ||
outsize = png_buffer.size(); | ||
} else { | ||
ORTX_CXX_API_THROW("[EncodeImage] Unsupported image format.", ORT_INVALID_ARGUMENT); | ||
} | ||
|
||
// Setup output & copy to destination | ||
std::vector<int64_t> output_dimensions{static_cast<int64_t>(encoded_image.size())}; | ||
std::vector<int64_t> output_dimensions{static_cast<int64_t>(outsize)}; | ||
uint8_t* data = output.Allocate(output_dimensions); | ||
memcpy(data, encoded_image.data(), encoded_image.size()); | ||
memcpy(data, outbuffer, outsize); | ||
|
||
if (outbuffer != png_buffer.data() && outbuffer != nullptr) { | ||
free(outbuffer); | ||
} | ||
} | ||
|
||
} // namespace ort_extensions |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps we can save
height_x_width[1]
in a variable here since it is called many times, to save compute/time to access the vector each time.