Skip to content
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

Added support for saving as PNG #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.ppm
example
*.png
example
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Simple 2D Graphics Library for C.

## Dependencies

*libpng*

```console
$ sudo apt-get install libpng-dev *(or equivalent)*
```

## Quick Start

```console
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
#!/bin/bash

set -xe

cc -Wall -Wextra -ggdb -o example example.c
cc -Wall -Wextra -ggdb -o example -lpng example.c
23 changes: 23 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ bool checker_example(void)
return false;
}

const char *file_path_png = "checker.png";
err = olivec_save_to_png_file(pixels, WIDTH, HEIGHT, file_path_png);
if (err) {
fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path_png, strerror(errno));
return false;
}

return true;
}

Expand Down Expand Up @@ -73,6 +80,14 @@ bool circle_example(void)
fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path, strerror(errno));
return false;
}

const char *file_path_png = "circle.png";
err = olivec_save_to_png_file(pixels, WIDTH, HEIGHT, file_path_png);
if (err) {
fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path_png, strerror(errno));
return false;
}

return true;
}

Expand Down Expand Up @@ -118,6 +133,14 @@ bool lines_example(void)
fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path, strerror(errno));
return false;
}

const char *file_path_png = "lines.png";
err = olivec_save_to_png_file(pixels, WIDTH, HEIGHT, file_path_png);
if (err) {
fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path_png, strerror(errno));
return false;
}

return true;
}

Expand Down
2 changes: 2 additions & 0 deletions imgs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.png

75 changes: 75 additions & 0 deletions olive.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <png.h>

typedef int Errno;

#define return_defer(value) do { result = (value); goto defer; } while (0)
#define return_defer_png(value) do { result = (value); goto deferpng; } while (0)
#define OLIVEC_SWAP(T, a, b) do { T t = a; a = b; b = t; } while (0)
#define OLIVEC_SIGN(T, x) ((T)((x) > 0) - (T)((x) < 0))
#define OLIVEC_ABS(T, x) (OLIVEC_SIGN(T, x)*(x))
Expand All @@ -19,6 +21,79 @@ void olivec_fill(uint32_t *pixels, size_t width, size_t height, uint32_t color)
}
}

Errno olivec_save_to_png_file(uint32_t *pixels, size_t width, size_t height, const char *file_path)
{
int result = 0;
png_byte **row_pointers = NULL;
png_structp png = NULL;
png_infop info = NULL;
FILE *f = NULL;
int pixel_size = 3;
int depth = 8;
size_t x, y;

f = fopen(file_path, "wb");
if (f == NULL) return_defer_png(errno);

png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL) return_defer_png(errno);

info = png_create_info_struct(png);
if (info == NULL) return_defer_png(errno);

if (setjmp(png_jmpbuf(png)))
{
return_defer_png(errno);
}

// Output is 8 bit depth, RGB format.
png_set_IHDR(
png,
info,
width, height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);

row_pointers = png_malloc(png, height * sizeof(png_byte*));

uint32_t* p = (uint32_t*)pixels;

for (y = 0; y < height; y++)
{
png_byte *row = png_malloc (png, sizeof(uint8_t) * width * pixel_size);
row_pointers[y] = row;
for (x = 0; x < width; x++)
{
int ix = y * width + x;
uint32_t pixel = p[ix];

*row++ = (pixel & 0x0000FF) >> 0;
*row++ = (pixel & 0x00FF00) >> 8;
*row++ = (pixel & 0xFF0000) >> 16;
}
}

png_init_io(png, f);
png_set_rows(png, info, row_pointers);
png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
png_write_end(png, NULL);

for (y = 0; y < height; y++)
{
png_free(png, row_pointers[y]);
}

png_destroy_write_struct(&png, &info);

deferpng:
if (f) fclose(f);
return result;
}

Errno olivec_save_to_ppm_file(uint32_t *pixels, size_t width, size_t height, const char *file_path)
{
int result = 0;
Expand Down