use absl::StarusOr<Data> as a return value instead on pointer to constructed value; rename variable

This commit is contained in:
Alexandra Latysheva 2020-10-30 13:41:55 +00:00
parent dcf397bb97
commit 56615d5d57

View File

@ -28,8 +28,8 @@ struct Data {
std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers; std::unique_ptr<sapi::v::Array<uint8_t>> row_pointers;
}; };
absl::Status ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, absl::StatusOr<Data> ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
absl::string_view infile, Data& d) { absl::string_view infile) {
sapi::v::Fd fd(open(infile.data(), O_RDONLY)); sapi::v::Fd fd(open(infile.data(), O_RDONLY));
if (fd.GetValue() < 0) { if (fd.GetValue() < 0) {
@ -87,38 +87,39 @@ absl::Status ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
SAPI_RETURN_IF_ERROR(api.png_set_sig_bytes(&struct_ptr, header.GetSize())); SAPI_RETURN_IF_ERROR(api.png_set_sig_bytes(&struct_ptr, header.GetSize()));
SAPI_RETURN_IF_ERROR(api.png_read_info(&struct_ptr, &info_ptr)); SAPI_RETURN_IF_ERROR(api.png_read_info(&struct_ptr, &info_ptr));
SAPI_ASSIGN_OR_RETURN(d.width, Data data;
SAPI_ASSIGN_OR_RETURN(data.width,
api.png_get_image_width(&struct_ptr, &info_ptr)); api.png_get_image_width(&struct_ptr, &info_ptr));
SAPI_ASSIGN_OR_RETURN(d.height, SAPI_ASSIGN_OR_RETURN(data.height,
api.png_get_image_height(&struct_ptr, &info_ptr)); api.png_get_image_height(&struct_ptr, &info_ptr));
SAPI_ASSIGN_OR_RETURN(d.color_type, SAPI_ASSIGN_OR_RETURN(data.color_type,
api.png_get_color_type(&struct_ptr, &info_ptr)); api.png_get_color_type(&struct_ptr, &info_ptr));
SAPI_ASSIGN_OR_RETURN(d.bit_depth, SAPI_ASSIGN_OR_RETURN(data.bit_depth,
api.png_get_bit_depth(&struct_ptr, &info_ptr)); api.png_get_bit_depth(&struct_ptr, &info_ptr));
SAPI_ASSIGN_OR_RETURN(d.number_of_passes, SAPI_ASSIGN_OR_RETURN(data.number_of_passes,
api.png_set_interlace_handling(&struct_ptr)); api.png_set_interlace_handling(&struct_ptr));
SAPI_RETURN_IF_ERROR(api.png_read_update_info(&struct_ptr, &info_ptr)); SAPI_RETURN_IF_ERROR(api.png_read_update_info(&struct_ptr, &info_ptr));
SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr)); SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr));
SAPI_ASSIGN_OR_RETURN(d.rowbytes, SAPI_ASSIGN_OR_RETURN(data.rowbytes,
api.png_get_rowbytes(&struct_ptr, &info_ptr)); api.png_get_rowbytes(&struct_ptr, &info_ptr));
d.row_pointers = data.row_pointers =
std::make_unique<sapi::v::Array<uint8_t>>(d.height * d.rowbytes); std::make_unique<sapi::v::Array<uint8_t>>(data.height * data.rowbytes);
SAPI_RETURN_IF_ERROR(api.png_read_image_wrapper( SAPI_RETURN_IF_ERROR(api.png_read_image_wrapper(
&struct_ptr, d.row_pointers->PtrAfter(), d.height, d.rowbytes)); &struct_ptr, data.row_pointers->PtrAfter(), data.height, data.rowbytes));
SAPI_RETURN_IF_ERROR(api.png_fclose(&file)); SAPI_RETURN_IF_ERROR(api.png_fclose(&file));
return absl::OkStatus(); return data;
} }
absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox, absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
absl::string_view outfile, Data& d) { absl::string_view outfile, Data& data) {
sapi::v::Fd fd(open(outfile.data(), O_WRONLY)); sapi::v::Fd fd(open(outfile.data(), O_WRONLY));
if (fd.GetValue() < 0) { if (fd.GetValue() < 0) {
return absl::InternalError("Error opening output file"); return absl::InternalError("Error opening output file");
@ -164,15 +165,16 @@ absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
SAPI_RETURN_IF_ERROR(api.png_init_io_wrapper(&struct_ptr, &file)); SAPI_RETURN_IF_ERROR(api.png_init_io_wrapper(&struct_ptr, &file));
SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr)); SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr));
SAPI_RETURN_IF_ERROR(api.png_set_IHDR( SAPI_RETURN_IF_ERROR(
&struct_ptr, &info_ptr, d.width, d.height, d.bit_depth, d.color_type, api.png_set_IHDR(&struct_ptr, &info_ptr, data.width, data.height,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE)); data.bit_depth, data.color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE));
SAPI_RETURN_IF_ERROR(api.png_write_info(&struct_ptr, &info_ptr)); SAPI_RETURN_IF_ERROR(api.png_write_info(&struct_ptr, &info_ptr));
SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr)); SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr));
SAPI_RETURN_IF_ERROR(api.png_write_image_wrapper( SAPI_RETURN_IF_ERROR(api.png_write_image_wrapper(
&struct_ptr, d.row_pointers->PtrBefore(), d.height, d.rowbytes)); &struct_ptr, data.row_pointers->PtrBefore(), data.height, data.rowbytes));
SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr)); SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr));
SAPI_RETURN_IF_ERROR(api.png_write_end(&struct_ptr, &null)); SAPI_RETURN_IF_ERROR(api.png_write_end(&struct_ptr, &null));
@ -189,32 +191,33 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
SAPI_RETURN_IF_ERROR(sandbox.Init()); SAPI_RETURN_IF_ERROR(sandbox.Init());
LibPNGApi api(&sandbox); LibPNGApi api(&sandbox);
Data d; SAPI_ASSIGN_OR_RETURN(Data data, ReadPng(api, sandbox, infile));
SAPI_RETURN_IF_ERROR(ReadPng(api, sandbox, infile, d));
if (d.color_type != PNG_COLOR_TYPE_RGBA && if (data.color_type != PNG_COLOR_TYPE_RGBA &&
d.color_type != PNG_COLOR_TYPE_RGB) { data.color_type != PNG_COLOR_TYPE_RGB) {
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrCat(
infile, " has unexpected color type. Expected RGB or RGBA")); infile, " has unexpected color type. Expected RGB or RGBA"));
} }
size_t channel_count = 3; size_t channel_count = 3;
if (d.color_type == PNG_COLOR_TYPE_RGBA) { if (data.color_type == PNG_COLOR_TYPE_RGBA) {
channel_count = 4; channel_count = 4;
} }
// RGB to BGR // RGB to BGR
for (size_t i = 0; i != d.height; ++i) { for (size_t i = 0; i != data.height; ++i) {
for (size_t j = 0; j != d.width; ++j) { for (size_t j = 0; j != data.width; ++j) {
uint8_t r = (*d.row_pointers)[i * d.rowbytes + j * channel_count]; uint8_t r = (*data.row_pointers)[i * data.rowbytes + j * channel_count];
uint8_t g = (*d.row_pointers)[i * d.rowbytes + j * channel_count + 1]; uint8_t g =
uint8_t b = (*d.row_pointers)[i * d.rowbytes + j * channel_count + 2]; (*data.row_pointers)[i * data.rowbytes + j * channel_count + 1];
(*d.row_pointers)[i * d.rowbytes + j * channel_count] = b; uint8_t b =
(*d.row_pointers)[i * d.rowbytes + j * channel_count + 2] = r; (*data.row_pointers)[i * data.rowbytes + j * channel_count + 2];
(*data.row_pointers)[i * data.rowbytes + j * channel_count] = b;
(*data.row_pointers)[i * data.rowbytes + j * channel_count + 2] = r;
} }
} }
SAPI_RETURN_IF_ERROR(WritePng(api, sandbox, outfile, d)); SAPI_RETURN_IF_ERROR(WritePng(api, sandbox, outfile, data));
return absl::OkStatus(); return absl::OkStatus();
} }