Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // https://github.com/nothings/stb/blob/master/stb_image_write.h
- #define STB_IMAGE_WRITE_IMPLEMENTATION
- #include "stb_image_write.h"
- int main() {
- int resx = 640;
- int resy = 480;
- unsigned char* pixels = (unsigned char*)malloc(sizeof(unsigned char) * (3 * resx * resy));
- // Create pixel data using linear gradients for red and green
- // (0,0) is the upper-left corner for most common image formats
- for (int j = 0; j < resy; j++) {
- for (int i = 0; i < resx; i++) {
- int k = j*resx + i;
- pixels[3*k + 0] = 255*i/(resx-1.0);
- pixels[3*k + 1] = 255*j/(resy-1.0);
- pixels[3*k + 2] = 128;
- }
- }
- {
- char str[128];
- sprintf(str, "test.png");
- if (stbi_write_png(str, resx, resy, 3, pixels, 3*resx)) {
- printf("File '%s' successfully saved\n", str); fflush(stdout);
- } else {
- printf("File '%s' error\n", str); fflush(stdout);
- }
- }
- {
- char str[128];
- sprintf(str, "test.bmp");
- if (stbi_write_bmp(str, resx, resy, 3, pixels)) {
- printf("File '%s' successfully saved\n", str); fflush(stdout);
- } else {
- printf("File '%s' error\n", str); fflush(stdout);
- }
- }
- // stb_image_write.h also support .tga and .hdr
- free(pixels);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement