views:

6

answers:

0

Hey all,

I'm using LibGD to plot a bunch of random coordinates and output a png of the result.

Here's the code:

#include <gd.h>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
 srand(time(NULL));

 gdImagePtr image;
 FILE *pngout;

 int num_pixels;
 int xy;

 cout << "num_pixels: ";
 cin >> num_pixels;
 cout << "xy: ";
 cin >> xy;

 image = gdImageCreate(xy, xy);

 for(int i = 0; i < xy; i++)
 {
  for(int j = 0; j < xy; j++)
  {
   int black = gdImageColorAllocate(image, 0, 0, 0);
   gdImageSetPixel(image, i, j, black);
  }
 }

 for(int i = 0; i < num_pixels; i++)
 {
  int white = gdImageColorAllocate(image, 255, 255, 255);
  gdImageSetPixel(image, rand()%xy, rand()%xy, white);
 }

 pngout = fopen("test.png", "wb");
 gdImagePng(image, pngout);
 fclose(pngout);
 gdImageDestroy(image);

 return 0;

}

Fairly straightforward, or so I thought. Unfortunately, this only works for pngs of dimensions smaller than 15px. When I try to give the program dimensions larger than 15x15, the resulting png is just black. No white pixels.

Is this an error in my logic, or do I not understand how LibGD works? This is my first time using LibGD, but I thought this looked pretty simple.

Thanks in advance!