Skip to content

Commit

Permalink
Completed png parser plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemanga committed Oct 16, 2023
1 parent bbc1c4c commit 598e307
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 33 deletions.
40 changes: 14 additions & 26 deletions plugins/parsepng/parsepng.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "API.hpp"
#include <cstdint>
#include <png.h>
#include <cstring>
#include <fmt.hpp>
#include <sys/unistd.h>

bool loadPNGImage(const char* name) {
SurfaceId loadPNGImage(const char* name) {
png_image image; /* The control structure used by libpng */
/* Initialize the 'png_image' structure. */
memset(&image, 0, (sizeof image));
Expand All @@ -16,9 +18,11 @@ bool loadPNGImage(const char* name) {
* textual message in the 'png_image' structure:
*/
fprintf(stderr, "pngtopng: error: %s\n", image.message);
return false;
return SurfaceId(0);
}

uint32_t width{image.width}, height{image.height};

/* Set the format in which to read the PNG file; this code chooses a
* simple sRGB format with a non-associated alpha channel, adequate to
* store most images.
Expand Down Expand Up @@ -59,29 +63,20 @@ bool loadPNGImage(const char* name) {
*
* to find the maximum size of the colormap in bytes.
*/
if (png_image_finish_read(&image, NULL/*background*/, buffer.data(), 0/*row_stride*/, NULL/*colormap*/) != 0)
if (!png_image_finish_read(&image, NULL/*background*/, buffer.data(), 0/*row_stride*/, NULL/*colormap*/))
{
return true;
// /* Now write the image out to the second argument. In the write
// * call 'convert_to_8bit' allows 16-bit data to be squashed down to
// * 8 bits; this isn't necessary here because the original read was
// * to the 8-bit format.
// */
// if (png_image_write_to_file(&image, argv[2], 0/*convert_to_8bit*/,
// buffer, 0/*row_stride*/, NULL/*colormap*/) != 0)
// {
// /* The image has been written successfully. */
// exit(0);
// }
} else {
/* Calling png_image_free is optional unless the simplified API was
* not run to completion. In this case, if there wasn't enough
* memory for 'buffer', we didn't complete the read, so we must
* free the image:
*/
png_image_free(&image);
return false;
return SurfaceId(0);
}

auto surface = createSurface(width, height);
Surface_write(surface, 0, 0, width, height, (Color*)buffer.data());
return surface;
}


Expand All @@ -90,14 +85,7 @@ int main(int argc, const char* argv[]) {
printf("parseobj error: expected 2 arguments, got %d.\n", argc);
return 1;
}
std::string answer = fmt("{} {} ", argv[1], getpid());

bool ok = loadPNGImage(argv[0]);
if (!ok) {
system((answer + "0 \"Could not open file\"").c_str());
return 1;
}

system((answer + "1").c_str());
auto ok = loadPNGImage(argv[0]);
message("{} {:#x} {:#x}", argv[1], getpid(), ok);
return 0;
}
Binary file modified plugins/parsepng/parsepng.drt
Binary file not shown.
9 changes: 2 additions & 7 deletions plugins/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

using namespace std::literals;

bool loadPNGImage(const char* name) {
return message("parsepng {} {}", name, getpid()) != 0;
}

int main(int argc, const char* argv[]) {
auto jpgparsejob = message("parsejpg {} {}", "test.jpg", getpid());
log("Loading png image: {}", loadPNGImage("test.png"));
auto jpgparsejob = message("parsepng {} {}", "test.png", getpid());

auto tex = createSurface(256, 256);
Surface_fill(tex, 32, 128, 255, 255);
Expand Down Expand Up @@ -101,7 +96,7 @@ int main(int argc, const char* argv[]) {
}

t += 0.001f;
Node_setPosition(node, x, sinf(t) * 5, z);
Node_setPosition(node, x, sinf(t) * 2, z);

if (int(obj)) {
Node_rotate(obj, 0.001f, 1, 0, 0);
Expand Down
Binary file modified plugins/test/test.drt
Binary file not shown.

0 comments on commit 598e307

Please sign in to comment.