Home
  Software
    Howto
      Freeimage

Simple example of libfreeimage use

#include <FreeImage.h>
#include <stdio.h>
#include <string.h>

#define WIDTH  320
#define HEIGHT 240

BYTE my_image[3 * WIDTH * HEIGHT];

void
set_pixel(int x, int y, BYTE R, BYTE G, BYTE B)
{
  my_image[3*y*WIDTH + 3*x + 0] = B;
  my_image[3*y*WIDTH + 3*x + 1] = G;   
  my_image[3*y*WIDTH + 3*x + 2] = R;   
}

void
set_colors() {
  int x, y;

  // Set everything to black
  bzero(my_image, sizeof(my_image));

  // For each line in the image
  for (y = 0; y < HEIGHT; y++) {
    // Set 1/4 width to RED
    for (x = 0; x < WIDTH/4; x++)
      set_pixel(x, y, 255, 0, 0);
    // Set 1/4 width to GREEN
    for (x = WIDTH/4; x < WIDTH/2; x++)
      set_pixel(x, y, 0, 255, 0);
    // Set 1/4 width to BLUE
    for (x = WIDTH/2; x < 3*WIDTH/4; x++)
      set_pixel(x, y, 0, 0, 255);
    // Set 1/4 width to WHITE
    for (x = 3*WIDTH/4; x < WIDTH; x++)
      set_pixel(x, y, 255, 255, 255);
  }
}

void
save_image(int w, int h, const char *fname)
{
  FreeImage_Initialise();
  FIBITMAP *img = FreeImage_ConvertFromRawBits(my_image,
                w, h,
                3*w,
                24,
                0, 0, 0,
                false);
  printf("Saving %d x %d image [%s]\n", w, h, fname);
  FreeImage_Save(FIF_PNG, img, fname, 0);
  FreeImage_DeInitialise();
}

int
main() {
  set_colors();
  save_image(WIDTH, HEIGHT, "bars.png");
  return 0;
}

Generated image


Warning: mysql_connect(): No such file or directory in /home/jcoppens/jcoppens.com/php/std_lib.php on line 1579
Could not connect to the database manager:
Error reported: No such file or directory


(c) John Coppens ON6JC/LW3HAZ correo