GIF
Implements the GIF file format
Controller: CodeCogs
Contents
Dependents
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.
Version: GIF89a Width: 29 Height: 28 Bits per pixel: 8 Transparency: On Color table size: 64 Background color: 42 Transparent color: 42
#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.
MISSING IMAGE!
378/hat_border.gif cannot be found in /users/378/hat_border.gif. Please contact the submission author.
#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
- LZW and GIF explained by Steve Blackstock, http://www.cis.udel.edu/~amer/CISC651/lzw.and.gif.explained.html
- "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
- The Wikipedia page on the GIF format, http://en.wikipedia.org/wiki/Graphics_Interchange_Format
Class GIF
Members of GIF
GIF
This is the implicit constructor for the GIF class, setting the GIF's parameters to default values.GIF( ) GIF
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.GIF( unsigned char* buffer )[constructor] buffer the buffer containing the GIF data
CreateCanvas
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.voidcreateCanvas( ) SetPixel
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.voidsetPixel( int x int y unsigned char colorIndex ) x the abscissa of the pixel y the ordinate of the pixel colorIndex the index of the color you want the pixel to have
GetPixel
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.unsigned chargetPixel( int x int y ) x the abscissa of the pixel y the ordinate of the pixel
Returns
- the color at the given coordinates, as an index in the color table
ReadFromBuffer
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.voidreadFromBuffer( unsigned char* buffer ) buffer the buffer containing the GIF data
WriteToBuffer
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.intwriteToBuffer( unsignedchar*& buffer ) GetVersion
Call this method to obtain the version of the currently loaded GIF. This can be either GIF87a or GIF89a.std::stringgetVersion( ) Returns
- the version of the currently loaded GIF
GetWidth
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.unsigned shortgetWidth( ) Returns
- the width of the current canvas
SetWidth
Call this method to set the width of the GIF's canvas to the specified value.voidsetWidth( unsigned short width ) width the desired width for the GIF's canvas
GetImageWidth
If a GIF image is loaded into the GIF object, this method returns the width of the image inside the GIF's canvas.unsigned shortgetImageWidth( ) Returns
- the width of the image on the GIF's canvas
SetImageWidth
Call this method to set the width of the GIF's canvas to the specified value.voidsetImageWidth( unsigned short imageWidth ) imageWidth the desired width for the image inside the GIF's canvas
GetHeight
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.unsigned shortgetHeight( ) Returns
- the height of the current canvas
SetHeight
Call this method to set the height of the GIF's canvas to the specified value.voidsetHeight( unsigned short height ) height the desired height for the GIF's canvas
GetImageHeight
If a GIF image is loaded into the GIF object, this method returns the height of the image inside the GIF's canvas.unsigned shortgetImageHeight( ) Returns
- the height of the image on the GIF's canvas
SetImageHeight
Call this method to set the height of the GIF's canvas to the specified value.voidsetImageHeight( unsigned short imageHeight ) imageHeight the desired height for the image inside the GIF's canvas
GetBitsPerPixel
This method returns the color depth for the currently loaded GIF.unsigned chargetBitsPerPixel( ) Returns
- the GIF's color depth
IsTransparent
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.boolisTransparent( ) Returns
- true if the current GIF is transparent, false otherwise
SetTransparency
Call this method to enable or disable transparency for the current GIF.voidsetTransparency( bool transparent ) transparent true if you want to enable transparency, false if not
GetTransparentColor
Call this method to obtain the index of the transparent color in the color table, in case transparency is enabled for the GIF.unsigned chargetTransparentColor( ) Returns
- the index of the transparent color in the color table
SetTransparentColor
Call this method to set the index of the current transparent color in the color table to the one specified through the transparentColorIndex parameter.voidsetTransparentColor( unsigned char transparentColorIndex ) transparentColorIndex the desired index in the color table for the transparent color
GetBackgroundColor
This method returns the index of the currently selected color for the canvas' background.unsigned chargetBackgroundColor( ) Returns
- the index of the current background color
SetBackgroundColor
Call this method to set the index of the background color in the color table to the value specified through the bkColor parameter.voidsetBackgroundColor( unsigned char bkColor ) bkColor the desired index of the background color in the color table
GetImageLeftOffset
This method returns the offset from the left of the canvas, to the image inside the GIF's canvas.unsigned shortgetImageLeftOffset( ) Returns
- the left offset of the image inside the GIF's canvas
SetImageLeftOffset
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.voidsetImageLeftOffset( unsigned short imageLeftOffset ) imageLeftOffset the desired left offset for the image inside the GIF's canvas
GetImageTopOffset
This method returns the offset from the top of the canvas, to the image inside the GIF's canvas.unsigned shortgetImageTopOffset( ) Returns
- the top offset of the image inside the GIF's canvas
SetImageTopOffset
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.voidsetImageTopOffset( unsigned short imageTopOffset ) imageTopOffset the desired top offset for the image inside the GIF's canvas