Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / MFC

Drawing transparent bitmaps using CImage

Rate me:
Please Sign up or sign in to vote.
4.92/5 (21 votes)
17 Sep 20017 min read 565.2K   13.1K   160  
Universal implementation of transparent drawing of bitmap files (BMP, JPEG, GIF, PNG). Based on the method described by Chris Becke and Raja Segar.
// imagif.h : interface of the CImageGIF class

/*
 * Purpose:	GIF Image Class Loader and Writer
 */
/* === C R E D I T S  &  D I S C L A I M E R S ==============
 * CImageICO (c) 07/Aug/2001 <ing.davide.pizzolato@libero.it>
 * Permission is given by the author to freely redistribute and include
 * this code in any program as long as this credit is given where due.
 *
 * original CImageGIF  and CImageIterator implementation are:
 * Copyright:	(c) 1995, Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
 *
 * 6/15/97 Randy Spann: Added GIF87a writing support
 *         R.Spann@ConnRiver.net
 *
 * DECODE.C - An LZW decoder for GIF
 * Copyright (C) 1987, by Steven A. Bennett
 * Copyright (C) 1994, C++ version by Alejandro Aguilar Sierra
 *
 * In accordance with the above, I want to credit Steve Wilhite who wrote
 * the code which this is heavily inspired by...
 *
 * GIF and 'Graphics Interchange Format' are trademarks (tm) of
 * Compuserve, Incorporated, an H&R Block Company.
 *
 * Release Notes: This file contains a decoder routine for GIF images
 * which is similar, structurally, to the original routine by Steve Wilhite.
 * It is, however, somewhat noticably faster in most cases.
 *
 * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 * THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
 * CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
 * THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
 * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
 * PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 * THIS DISCLAIMER.
 *
 * Use at your own risk!
 * ==========================================================
 */

#ifndef __GIF_H__
#define __GIF_H__

#include "..\include\cimage.h"

typedef short int       code_int;   
typedef long int        count_int;
typedef unsigned char   pixval;

/* Various error codes used by decoder */
#define OUT_OF_MEMORY -10
#define BAD_CODE_SIZE -20
#define READ_ERROR -1
#define WRITE_ERROR -2
#define OPEN_ERROR -3
#define CREATE_ERROR -4
#define MAX_CODES   4095

class CImageIterator;
class CImageGIF: public CImage
{
protected:

   enum
   {
      GIFBUFTAM = 16384,
      TRANSPARENCY_CODE = 0xF9
   };
   int  m_ibf;// = GIFBUFTAM+1;
   BYTE m_buf[GIFBUFTAM];

	int          Width, Height;
	int             curx, cury;
	int           BitsPerPixel;
	long             CountDown;
	unsigned long    cur_accum;
	int              cur_bits;
	unsigned char    *m_buffer;
	int iypos, istep, iheight, ipass;
   BOOL interlaced;
// Implementation
	void BumpPixel ();
	int GIFNextPixel ();
	void Putword (int w, FILE* fp );
	void compress (int init_bits, FILE* outfile);
	void output (code_int code );
	void cl_block ();
	void cl_hash (count_int hsize);
	void writeerr ();
	void char_init ();
	void char_out (int c);
	void flush_char ();
	short init_exp(short size);
	short get_next_code(FILE*);
	short decoder(FILE*, CImageIterator* iter, short linewidth, INT&  bad_code_count);
	int get_byte(FILE*);
	int out_line(CImageIterator* iter, unsigned char *pixels, int linelen);

	short curr_size;                     /* The current code size */
	short clear;                         /* Value for a clear code */
	short ending;                        /* Value for a ending code */
	short newcodes;                      /* First available code */
	short top_slot;                      /* Highest code for current size */
	short slot;                          /* Last read code */

	/* The following static variables are used
	* for seperating out codes */
	short navail_bytes;              /* # bytes left in block */
	short nbits_left;                /* # bits left in current BYTE */
	BYTE b1;                           /* Current BYTE */
	BYTE byte_buff[257];               /* Current block */
	BYTE *pbytes;                      /* Pointer to next BYTE in block */
	/* The reason we have these seperated like this instead of using
	* a structure like the original Wilhite code did, is because this
	* stuff generally produces significantly faster code when compiled...
	* This code is full of similar speedups...  (For a good book on writing
	* C for speed or for space optomisation, see Efficient C by Tom Plum,
	* published by Plum-Hall Associates...)
	*/
	BYTE stack[MAX_CODES + 1];            /* Stack for storing pixels */
	BYTE suffix[MAX_CODES + 1];           /* Suffix table */
	USHORT prefix[MAX_CODES + 1];           /* Prefix linked list */
public:
   CImageGIF(): CImage() {}

// Implementation
public:
	virtual BOOL Read(FILE*);
	virtual BOOL Write(FILE*);
};

#endif // __GIF_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions