#could somebody please give me a example of how to use libpng

7 messages · Page 1 of 1 (latest)

hollow igloo
#

what i want to do is something like this: ``` define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "pnglib/png.h"
#pragma comment(lib, "libs/libpng16.lib")
void write_png_file(char* filename, int width, int height) {

FILE* fp = fopen(filename, "wb");
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr, fp);
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);
png_write_info(png_ptr, info_ptr);


//somehow create the pixels and call the png_write_image function


png_write_end(png_ptr, NULL);

}```

past raven
#

What's the issue?

hollow igloo
#

I just want a simple example of creating procedurally generated pixels and calling the png_write_image function.

hazy shoal
#

Including a library really depends on what you are using the build the program aka what program is invoking the compiler. During compilation, a compiler is passed an argument for where it should look for libraries. For example, gcc -I/path/to/headers. so what build system are you using and if you have libpng locally, can you find a way to include the library and header in the include path of the build system?

swift harness
#

I've never used libpng but you can get an example of what you want from AI if you take it with a grain of salt.

First part:

#include <png.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

void write_png(const char* filename, uint32_t width, uint32_t height, uint8_t* buffer) {
    FILE *fp = fopen(filename, "wb");
    if (!fp) {
        perror("File could not be opened for writing");
        return;
    }

    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if (!png) {
        fclose(fp);
        fprintf(stderr, "Could not allocate write struct\n");
        return;
    }

    png_infop info = png_create_info_struct(png);
    if (!info) {
        png_destroy_write_struct(&png, nullptr);
        fclose(fp);
        fprintf(stderr, "Could not allocate info struct\n");
        return;
    }

    if (setjmp(png_jmpbuf(png))) {
        png_destroy_write_struct(&png, &info);
        fclose(fp);
        fprintf(stderr, "Error during png creation\n");
        return;
    }


meager ravineBOT
#

@swift harness has reached level 9. GG!

swift harness
#
    png_init_io(png, fp);
    png_set_IHDR(png, info, width, height, 8 /* bit_depth */, PNG_COLOR_TYPE_RGB,
                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    png_write_info(png, info);

    png_bytep rows[height];
    for (uint32_t y = 0; y < height; ++y) {
        rows[y] = buffer + y * width * 3;
    }

    png_write_image(png, rows);
    png_write_end(png, nullptr);
    png_destroy_write_struct(&png, &info);
    fclose(fp);
}

uint8_t* generate_pixels(uint32_t width, uint32_t height) {
    uint8_t* buffer = (uint8_t*)malloc(width * height * 3 * sizeof(uint8_t));
    if (!buffer) {
        fprintf(stderr, "Memory allocation failed\n");
        return nullptr;
    }

    for (uint32_t y = 0; y < height; ++y) {
        for (uint32_t x = 0; x < width; ++x) {
            uint8_t r = rand() % 256;
            uint8_t g = rand() % 256;
            uint8_t b = rand() % 256;
            buffer[(y * width + x) * 3 + 0] = r;
            buffer[(y * width + x) * 3 + 1] = g;
            buffer[(y * width + x) * 3 + 2] = b;
        }
    }
    return buffer;
}

int main() {
    uint32_t width = 800, height = 600;
    uint8_t* buffer = generate_pixels(width, height);
    if (!buffer) return 1;

    write_png("output.png", width, height, buffer);
    free(buffer);
    return 0;
}