I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
COST (GBP)
this unit 9.60
sub units 4.00
+
0

GIF

Implements the GIF file format
Controller: CodeCogs

Dependents

Info

Interface

C++

Overview

This module implements a class that allows you to load, save and manipulate non-animated GIF content. It also allows you to set various parameters of the image, like transparency, color table, and others.

The GIF format uses a lossless data compression algorithm to store its image data. The algorithm is a modification of the classic Lempel-Ziv-Welch (LZW) dictionary method, using variable code sizes during the compression process. The modified LZW algorithm is available as IO/Compression/VariableLZW.

The GIF class can be used in a variety of circumstances, from extracting information about a GIF file, modifying a GIF and saving it, to creating a GIF image from scratch and saving it to disk.

Expand the following example to see how you can display information about a GIF.

#include <codecogs/graphics/formats/gif.h>
#include <iostream>
#include <fstream>
 
int main()
{
  // name of the file to open (without the .gif extension)
  std::string fileName = "hat.gif";
 
  // size of the file to open
  int fileSize = 606;
 
  // create a temporary buffer into which the whole file is loaded
  unsigned char *buffer = new unsigned char[fileSize];
 
  // open the file for reading
  std::ifstream inf(fileName.c_str(), std::ios_base::in | std::ios_base::binary);
 
  // read all its contents into the buffer
  inf.read((char *)buffer, fileSize);
 
  // close the file
  inf.close();
 
  // decode the data in the buffer and store it in a GIF object
  Graphics::Formats::GIF image(buffer);
 
  // the buffer can now be removed from memory
  delete [] buffer;
 
  // display some information about the GIF image
  std::cout << "          Version: " << image.getVersion() << std::endl;
  std::cout << "            Width: " << image.getWidth() << std::endl;
  std::cout << "           Height: " << image.getHeight() << std::endl;
 
  std::cout << std::endl;
 
  std::cout << "   Bits per pixel: " << (int) image.getBitsPerPixel() << std::endl;
  std::cout << "     Transparency: " << (image.isTransparent() ? "On" : "Off") << std::endl;
 
  std::cout << std::endl;
 
  std::cout << " Color table size: " << image.colorTable.size() << std::endl;
  std::cout << " Background color: " << (int) image.getBackgroundColor() << std::endl;
  if (image.isTransparent())
  {
    std::cout << "Transparent color: " << (int) image.getTransparentColor() << std::endl;
  }
 
  std::cout << std::endl;
 
  return 0;
}
Input file:
MISSING IMAGE!

378/hat.gif cannot be found in /users/378/hat.gif. Please contact the submission author.

Output:
Version: GIF89a
            Width: 29
           Height: 28
 
   Bits per pixel: 8
     Transparency: On
 
 Color table size: 64
 Background color: 42
Transparent color: 42

The main input-output methods provided by this class are readFromBuffer and writeToBuffer, while the actual GIF manipulation is achived by changing the colorTable public variable, or by calling the setPixel method. The next example reads a GIF file from the disk, adds a border to it using the setPixel method and then writes the new GIF file to disk.

#include <codecogs/graphics/formats/gif.h>
#include <iostream>
#include <fstream>
 
int main()
{
  // name of the file to open (without the .gif extension)
  std::string fileName = "hat";
 
  // size of the file to open
  int fileSize = 606;
 
  // create a temporary buffer into which the whole file is loaded
  unsigned char *buffer = new unsigned char[fileSize];
 
  // open the file for reading
  std::ifstream inf((fileName + ".gif").c_str(), std::ios_base::in | std::ios_base::binary);
 
  // read all its contents into the buffer
  inf.read((char *)buffer, fileSize);
 
  // close the file
  inf.close();
 
  // decode the data in the buffer and store it in a GIF object
  Graphics::Formats::GIF image(buffer);
 
  // the buffer can now be removed from memory
  delete [] buffer;
 
  // draw a border around the image using the setPixel() method
 
  // store the width and height of the image
  int xmax = image.getWidth(), ymax = image.getHeight();
 
  // index of the color to draw the border with
  unsigned char color = 0;
 
  // draw the horizontal lines
  for (int x = 0; x < xmax; ++x)
  {
    image.setPixel(x, 0, color);
    image.setPixel(x, ymax - 1, color);
  }
 
  // draw the vertical lines
  for (int y = 0; y < ymax; ++y)
  {
    image.setPixel(0, y, color);
    image.setPixel(xmax - 1, y, color);
  }
 
  // encode the GIF image data into the buffer (previously declared)
  // "size" stores the size of the resulting encoded data
  int size = image.writeToBuffer(buffer);
 
  // open the file for writing
  std::ofstream outf((fileName + "_border.gif").c_str(), std::ios_base::out |
std::ios_base::binary);
 
  // write all the data in the buffer into the file
  outf.write((char *)buffer, size);
 
  // close the file
  outf.close();
 
  // the buffer can now be removed from memory
  delete [] buffer;
 
  return 0;
}
Input file:
MISSING IMAGE!

378/hat.gif cannot be found in /users/378/hat.gif. Please contact the submission author.

Output file:
MISSING IMAGE!

378/hat_border.gif cannot be found in /users/378/hat_border.gif. Please contact the submission author.

If you would like to create the whole GIF image from scratch, then you first need to make sure the canvas is set up properly, i.e. you need to set the dimensions of the GIF and call the createCanvas method to create the GIF's canvas on which you can draw. The next example draws a cross on a 50x50 canvas and saves it to disk.

#include <codecogs/graphics/formats/gif.h>
#include <iostream>
#include <fstream>
 
int main()
{
  // create a GIF object
  Graphics::Formats::GIF image;
 
  // add the colors we need to the color table
 
  // enable transparency
  image.setTransparency(true);
 
  // add the background transparent color
  image.colorTable.push_back(Graphics::Formats::RGBValue(0, 0, 0));
 
  // set the transparent color's index (0 in this case)
  image.setTransparentColor(0);
 
  // set the background color to the transparent color
  image.setBackgroundColor(0);
 
  // add the red color to draw with
  image.colorTable.push_back(Graphics::Formats::RGBValue(255, 0, 0));
 
  // set the image dimensions
  image.setWidth(50);
  image.setImageWidth(50);
  image.setHeight(50);
  image.setImageHeight(50);
 
  // create the canvas with the given dimensions
  image.createCanvas();
 
  // draw the cross
  unsigned short xmax = image.getWidth();
  for (unsigned short i = 0; i < xmax; ++i)
  {
    image.setPixel(i, i, 1);
    image.setPixel(xmax - i - 1, i, 1);
  }
 
  // name of the file to write to
  std::string fileName = "cross.gif";
 
  // create a temporary buffer into which
  // the entire image data is temporarily stored
  unsigned char *buffer;
 
  // encode the GIF image data into the buffer
  // "size" stores the size of the resulting encoded data
  int size = image.writeToBuffer(buffer);
 
  // open the file for writing
  std::ofstream outf(fileName.c_str(), std::ios_base::out | std::ios_base::binary);
 
  // write all the data in the buffer into the file
  outf.write((char *)buffer, size);
 
  // close the file
  outf.close();
 
  // the buffer can now be removed from memory
  delete [] buffer;
 
  return 0;
}
Output file:
MISSING IMAGE!

378/cross.gif cannot be found in /users/378/cross.gif. Please contact the submission author.

Authors

Lucian Bentea, August 2009

References

  1. LZW and GIF explained by Steve Blackstock, http://www.cis.udel.edu/~amer/CISC651/lzw.and.gif.explained.html
  2. "Compressed image file formats: JPEG, PNG, GIF, XBM, BMP" by John Miano, Chapter 12, http://books.google.com/books?id=_nJLvY757dQC&dq=Compressed+image+file+formats&printsec=frontcover&source=bn&hl=ro&ei=ImaPSuGxIsiK_Abyh5SvAg&sa=X&oi=book_result&ct=result&resnum=4#v=onepage&q=&f=false
  3. The Wikipedia page on the GIF format, http://en.wikipedia.org/wiki/Graphics_Interchange_Format

Class GIF

Members of GIF

GIF

 
GIF )
This is the implicit constructor for the GIF class, setting the GIF's parameters to default values.

GIF

 
GIFunsigned char*buffer )[constructor]
This version of the constructor allows you to load GIF data as soon as you create an instance of the GIF class.

The data is loaded from the given buffer parameter, as if it were a GIF file on the disk. To load the data, this constructor makes a call to the readFromBuffer method.
bufferthe buffer containing the GIF data

CreateCanvas

 
voidcreateCanvas )
You may call this function after setting up the image's width and height. It will create the canvas with the given dimensions, on which you can draw using the setPixel method.

SetPixel

 
voidsetPixelintx
inty
unsigned charcolorIndex )
Call this method to set the pixel at the x and y coordinates to have the color with index colorIndex in the color table.

Make sure you first call the createCanvas method before setting any pixel on the canvas.
xthe abscissa of the pixel
ythe ordinate of the pixel
colorIndexthe index of the color you want the pixel to have

GetPixel

 
unsigned chargetPixelintx
inty )
Call this method to obtain the color of the pixel at the x and y coordinates, as an index in the color table.

Make sure you first call the createCanvas method before calling this method to return the color at the specified coordinates.
xthe abscissa of the pixel
ythe ordinate of the pixel

Returns

the color at the given coordinates, as an index in the color table

ReadFromBuffer

 
voidreadFromBufferunsigned char*buffer )
This method reads the data in the given buffer parameter, as if it were the contents of a GIF file on the disk. The data is decoded and stored in the GIF object.
bufferthe buffer containing the GIF data

WriteToBuffer

 
intwriteToBufferunsignedchar*& buffer )
This method writes the data from the GIF object into the given buffer parameter, as if it were a GIF file on the disk. The data is encoded and stored in the specified buffer.

GetVersion

 
std::stringgetVersion )
Call this method to obtain the version of the currently loaded GIF. This can be either GIF87a or GIF89a.

Returns

the version of the currently loaded GIF

GetWidth

 
unsigned shortgetWidth )
This method returns the width of the currently active canvas. If a GIF image is loaded into the GIF object, then this method returns the width of the loaded GIF.

Returns

the width of the current canvas

SetWidth

 
voidsetWidthunsigned shortwidth )
Call this method to set the width of the GIF's canvas to the specified value.
widththe desired width for the GIF's canvas

GetImageWidth

 
unsigned shortgetImageWidth )
If a GIF image is loaded into the GIF object, this method returns the width of the image inside the GIF's canvas.

Returns

the width of the image on the GIF's canvas

SetImageWidth

 
voidsetImageWidthunsigned shortimageWidth )
Call this method to set the width of the GIF's canvas to the specified value.
imageWidththe desired width for the image inside the GIF's canvas

GetHeight

 
unsigned shortgetHeight )
This method returns the height of the currently active canvas. If a GIF image is loaded into the GIF object, then this method returns the height of the loaded GIF.

Returns

the height of the current canvas

SetHeight

 
voidsetHeightunsigned shortheight )
Call this method to set the height of the GIF's canvas to the specified value.
heightthe desired height for the GIF's canvas

GetImageHeight

 
unsigned shortgetImageHeight )
If a GIF image is loaded into the GIF object, this method returns the height of the image inside the GIF's canvas.

Returns

the height of the image on the GIF's canvas

SetImageHeight

 
voidsetImageHeightunsigned shortimageHeight )
Call this method to set the height of the GIF's canvas to the specified value.
imageHeightthe desired height for the image inside the GIF's canvas

GetBitsPerPixel

 
unsigned chargetBitsPerPixel )
This method returns the color depth for the currently loaded GIF.

Returns

the GIF's color depth

IsTransparent

 
boolisTransparent )
This method returns a boolean value indicating whether the GIF is transparent. The index of the transparent color in the color table can be obtained or set using the getTransparentColor and setTransparentColor methods, respectively.

Returns

true if the current GIF is transparent, false otherwise

SetTransparency

 
voidsetTransparencybooltransparent )
Call this method to enable or disable transparency for the current GIF.
transparenttrue if you want to enable transparency, false if not

GetTransparentColor

 
unsigned chargetTransparentColor )
Call this method to obtain the index of the transparent color in the color table, in case transparency is enabled for the GIF.

Returns

the index of the transparent color in the color table

SetTransparentColor

 
voidsetTransparentColorunsigned chartransparentColorIndex )
Call this method to set the index of the current transparent color in the color table to the one specified through the transparentColorIndex parameter.
transparentColorIndexthe desired index in the color table for the transparent color

GetBackgroundColor

 
unsigned chargetBackgroundColor )
This method returns the index of the currently selected color for the canvas' background.

Returns

the index of the current background color

SetBackgroundColor

 
voidsetBackgroundColorunsigned charbkColor )
Call this method to set the index of the background color in the color table to the value specified through the bkColor parameter.
bkColorthe desired index of the background color in the color table

GetImageLeftOffset

 
unsigned shortgetImageLeftOffset )
This method returns the offset from the left of the canvas, to the image inside the GIF's canvas.

Returns

the left offset of the image inside the GIF's canvas

SetImageLeftOffset

 
voidsetImageLeftOffsetunsigned shortimageLeftOffset )
Call this method to set the offset from the left of the canvas, to the image inside the GIF's canvas, to the value specified through the imageLeftOffset parameter.
imageLeftOffsetthe desired left offset for the image inside the GIF's canvas

GetImageTopOffset

 
unsigned shortgetImageTopOffset )
This method returns the offset from the top of the canvas, to the image inside the GIF's canvas.

Returns

the top offset of the image inside the GIF's canvas

SetImageTopOffset

 
voidsetImageTopOffsetunsigned shortimageTopOffset )
Call this method to set the offset from the left of the canvas, to the image inside the GIF's canvas, to the value specified through the imageLeftOffset parameter.
imageTopOffsetthe desired top offset for the image inside the GIF's canvas