Browse Source

random png generation

cmte 5 years ago
parent
commit
727943d26e
5 changed files with 169 additions and 0 deletions
  1. 2 0
      libpng/.gitignore
  2. 13 0
      libpng/Makefile
  3. 84 0
      libpng/img.c
  4. 33 0
      libpng/img.h
  5. 37 0
      libpng/png.c

+ 2 - 0
libpng/.gitignore

@@ -0,0 +1,2 @@
+bin/*
+images/*

+ 13 - 0
libpng/Makefile

@@ -0,0 +1,13 @@
+CC=/usr/bin/gcc
+CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD} -lm -lpng
+OUT=bin
+OBJ=$(OUT)/obj
+OBJS=$(OBJ)/img.o
+
+
+all: img
+	$(CC) $(CFLAGS) $(OBJS) png.c -o png
+
+img:
+	$(CC) $(CFLAGS)	-c img.c -o $(OBJ)/img.o
+

+ 84 - 0
libpng/img.c

@@ -0,0 +1,84 @@
+#include "img.h"
+
+
+image initialize_png(char *title, char *filename, int width, int height)
+{
+	image ret;
+	FILE *fp = NULL;
+
+	fp = fopen(filename, "wb");
+
+	png_structp png_ptr =
+	    png_create_write_struct(PNG_LIBPNG_VER_STRING,
+				    NULL,
+				    NULL,
+				    NULL);
+
+	if (png_ptr == NULL) {
+		fprintf(stderr, "Could not create write struct. libpng\n");
+	}
+
+
+	png_infop info_ptr = png_create_info_struct(png_ptr);
+	if (info_ptr == NULL) {
+		fprintf(stderr, "Could not create info struct. libpng\n");
+	}
+
+	if (setjmp(png_jmpbuf(png_ptr))) {
+		fprintf(stderr, "Error during png creation\n");
+	}
+
+	png_init_io(png_ptr, fp);
+
+	//Write header
+	png_set_IHDR(png_ptr,
+		     info_ptr,
+		     width,
+		     height,
+		     8,
+		     PNG_COLOR_TYPE_RGB,
+		     PNG_INTERLACE_NONE,
+		     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+	if (title != NULL) {
+		png_text title_text;
+		title_text.compression = PNG_TEXT_COMPRESSION_NONE;
+		title_text.key = "Title";
+		title_text.text = title;
+		png_set_text(png_ptr, info_ptr, &title_text, 1);
+	}
+
+	png_write_info(png_ptr, info_ptr);
+
+	ret.png_ptr = png_ptr;
+	ret.info_ptr = info_ptr;
+	ret.width = width;
+	ret.height = height;
+	ret.fp = fp;
+	return ret;
+}
+
+void write_image(image * img, pix_row * img_array)
+{
+
+	png_bytep row = NULL;
+	int x = 0, y = 0;
+	row = (png_bytep) malloc(3 * img->width * sizeof(png_byte));
+	for (y = 0; y < img->height; y++) {
+		for (x = 0; x < img->width; x++) {
+			row[x * 3 + 0] = img_array[x].p[y].r;	//red
+			row[x * 3 + 1] = img_array[x].p[y].g;	// green
+			row[x * 3 + 2] = img_array[x].p[y].b;	// blue
+		}
+		png_write_row(img->png_ptr, row);
+	}
+	png_write_end(img->png_ptr, img->info_ptr);
+	free(row);
+}
+
+void finish_image(image * img)
+{
+	fclose(img->fp);
+	png_free_data(img->png_ptr, img->info_ptr, PNG_FREE_ALL, -1);
+	png_destroy_write_struct(&img->png_ptr, NULL);
+
+}

+ 33 - 0
libpng/img.h

@@ -0,0 +1,33 @@
+#ifndef IMG_H
+#define IMG_H
+#include <png.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+typedef struct pix {
+	int r;
+	int g; 
+	int b;
+	int a;
+}pix;
+
+typedef struct pix_row {
+	int lenght;
+	pix* p;
+}pix_row;
+
+typedef struct image {
+
+	png_structp png_ptr;
+	png_infop info_ptr;
+	int width;
+	int height;
+	FILE *fp;
+} image;
+
+
+image initialize_png(char* title, char* filename, int width, int height);
+void write_image(image * img, pix_row * img_array);
+void finish_image(image * img);
+
+#endif

+ 37 - 0
libpng/png.c

@@ -0,0 +1,37 @@
+
+#define _XOPEN_SOURCE
+#include "img.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+
+int main(){
+	pix_row rows[1920];
+	pix p;
+	char filename[60];
+	int i,j,x,y,count;
+	image img;
+	p.r = 200;
+	p.g = 200;
+	p.b = 200;
+	
+	for (count=0; count<(3600*60);count++){
+
+		for (i=0;i<1920;i++){
+			rows[i].p = malloc (1080 * sizeof(pix));
+			for (j=0 ;j<1080; j++){
+				p.r = (int) (drand48() * 255);
+				p.g = (int) (drand48() * 255);
+				p.b = (int) (drand48() * 255);
+				rows[i].p[j] = p;
+			}
+		}
+		sprintf(filename, "images/image%5d.png", count);
+		img = initialize_png("image010", filename, 1920, 1080);
+		write_image(&img, rows);
+		finish_image(&img);
+	}
+
+	return (0);
+
+}