1717 */
1818
1919#include <assert.h>
20- #include <errno.h>
2120#include <stdbool.h>
2221#include <stdlib.h>
2322#include <string.h>
24- #include <ctype.h>
25-
26- #include <png.h>
2723
24+ #include "include/image.h"
2825#include "include/args.h"
2926#include "include/read_file.h"
30- #include "include/image.h"
3127#include "include/util.h"
3228
33- /* Bytes per pixel of the PNG image (R, G, B) */
34- #define PNG_BPP 3
35-
36- /*----------------------------------------------------------------------------*/
37-
3829bool image_init (Image * image , size_t width , size_t height ) {
3930 assert (image != NULL );
4031
@@ -53,8 +44,6 @@ void image_deinit(Image* image) {
5344 image -> pixels = NULL ;
5445}
5546
56- /*----------------------------------------------------------------------------*/
57-
5847void image_transform_squares (Image * image , size_t square_side ) {
5948 const int square_size = square_side * square_side ;
6049 const size_t total_pixels = image -> width * image -> height ;
@@ -98,88 +87,3 @@ void image_transform_squares(Image* image, size_t square_side) {
9887 free (image -> pixels );
9988 image -> pixels = new_pixels ;
10089}
101-
102- /*----------------------------------------------------------------------------*/
103-
104- void image2png (Image * image , const char * filename , int zoom ) {
105- FILE * fd = fopen (filename , "wb" );
106- if (fd == NULL )
107- DIE ("Can't open file '%s': %s" , filename , strerror (errno ));
108-
109- png_structp png =
110- png_create_write_struct (PNG_LIBPNG_VER_STRING , NULL , NULL , NULL );
111- if (png == NULL )
112- DIE ("Can't create 'png_structp'. Aborting." );
113-
114- png_infop info = png_create_info_struct (png );
115- if (info == NULL )
116- DIE ("Can't create 'png_infop'. Aborting." );
117-
118- /* The actual PNG image dimensions, remember that the Image is unscaled */
119- const size_t png_height = image -> height * zoom ;
120- const size_t png_width = image -> width * zoom ;
121-
122- /* Specify the PNG info */
123- png_init_io (png , fd );
124- png_set_IHDR (png ,
125- info ,
126- png_width ,
127- png_height ,
128- 8 ,
129- PNG_COLOR_TYPE_RGB ,
130- PNG_INTERLACE_NONE ,
131- PNG_COMPRESSION_TYPE_DEFAULT ,
132- PNG_FILTER_TYPE_DEFAULT );
133- png_write_info (png , info );
134-
135- /*
136- * Allocate the PNG rows. Since png_bytep is typedef'd to a pointer, this is
137- * a (void**).
138- */
139- png_bytep * rows = calloc (png_height , sizeof (png_bytep ));
140- if (rows == NULL )
141- DIE ("Failed to allocate PNG rows" );
142-
143- for (size_t y = 0 ; y < png_height ; y ++ ) {
144- rows [y ] = malloc (png_width * PNG_BPP );
145- if (rows [y ] == NULL )
146- DIE ("Failed to allocate PNG row %zu" , y );
147- }
148-
149- /*
150- * Write the 'bytes' array we received into the 'rows' array we just
151- * allocated.
152- *
153- * The outer loops iterate the unscaled pixels, and are needed for accessing
154- * the 'bytes->data' array.
155- */
156- for (size_t y = 0 ; y < image -> height ; y ++ ) {
157- for (size_t x = 0 ; x < image -> width ; x ++ ) {
158- Color color = image -> pixels [image -> width * y + x ];
159-
160- /* Draw a rectangle of side 'zoom' */
161- for (int rect_y = 0 ; rect_y < zoom ; rect_y ++ ) {
162- for (int rect_x = 0 ; rect_x < zoom ; rect_x ++ ) {
163- const png_bytep row = rows [zoom * y + rect_y ];
164-
165- /* Note that we are using RGB, not RGBA */
166- row [PNG_BPP * (zoom * x + rect_x )] = color .r ;
167- row [PNG_BPP * (zoom * x + rect_x ) + 1 ] = color .g ;
168- row [PNG_BPP * (zoom * x + rect_x ) + 2 ] = color .b ;
169- }
170- }
171- }
172- }
173-
174- /* Write the rows into the PNG structure */
175- png_write_image (png , rows );
176- png_write_end (png , NULL );
177-
178- /* Free each pointer of the 'rows' array, and the array itself */
179- for (size_t y = 0 ; y < png_height ; y ++ )
180- free (rows [y ]);
181- free (rows );
182-
183- fclose (fd );
184- png_destroy_write_struct (& png , & info );
185- }
0 commit comments