Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // https://github.com/nothings/stb/blob/master/stb_image_write.h
  5. #define STB_IMAGE_WRITE_IMPLEMENTATION
  6. #include "stb_image_write.h"
  7.  
  8. int main() {
  9.     int resx = 640;
  10.     int resy = 480;
  11.     unsigned char* pixels = (unsigned char*)malloc(sizeof(unsigned char) * (3 * resx * resy));
  12.  
  13.     // Create pixel data using linear gradients for red and green
  14.     // (0,0) is the upper-left corner for most common image formats
  15.     for (int j = 0; j < resy; j++) {
  16.         for (int i = 0; i < resx; i++) {
  17.             int k = j*resx + i;
  18.             pixels[3*k + 0] = 255*i/(resx-1.0);
  19.             pixels[3*k + 1] = 255*j/(resy-1.0);
  20.             pixels[3*k + 2] = 128;
  21.         }
  22.     }
  23.  
  24.     {
  25.         char str[128];
  26.         sprintf(str, "test.png");
  27.  
  28.         if (stbi_write_png(str, resx, resy, 3, pixels, 3*resx)) {
  29.             printf("File '%s' successfully saved\n", str); fflush(stdout);
  30.         } else {
  31.             printf("File '%s' error\n", str); fflush(stdout);
  32.         }
  33.     }
  34.  
  35.     {
  36.         char str[128];
  37.         sprintf(str, "test.bmp");
  38.  
  39.         if (stbi_write_bmp(str, resx, resy, 3, pixels)) {
  40.             printf("File '%s' successfully saved\n", str); fflush(stdout);
  41.         } else {
  42.             printf("File '%s' error\n", str); fflush(stdout);
  43.         }
  44.     }
  45.  
  46.     // stb_image_write.h also support .tga and .hdr
  47.    
  48.     free(pixels);
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement