Code Search for Developers
 
 
  

rle.c from Allegro game programming library at Krugle


Show rle.c syntax highlighted

/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      RLE sprite generation routines.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#include <string.h>

#include "allegro.h"
#include "allegro/internal/aintern.h"



/* al_create_rle_sprite:
 *  Creates a run length encoded sprite based on the specified bitmap.
 *  The returned sprite is likely to be a lot smaller than the original
 *  bitmap, and can be drawn to the al_screen with al_draw_rle_sprite().
 *
 *  The compression is done individually for each al_draw_line of the image.
 *  Format is a series of command bytes, 1-127 marks a run of that many
 *  solid pixels, negative numbers mark a gap of -n pixels, and 0 marks
 *  the end of a al_draw_line (since zero can't occur anywhere else in the data,
 *  this can be used to find the start of a specified al_draw_line when clipping).
 *  For truecolor RLE sprites, the data and command bytes are both in the
 *  same format (16 or 32 bits, 24 bpp data is padded to 32 bit aligment), 
 *  and the mask color (bright pink) is used as the EOL marker.
 */
AL_RLE_SPRITE *al_create_rle_sprite(AL_BITMAP *bitmap)
{
   int depth = al_bitmap_color_depth(bitmap);
   AL_RLE_SPRITE *s;
   int x, y;
   int run;
   int pix;
   int c;

   #define WRITE_TO_SPRITE8(x) {                                             \
      _grow_scratch_mem(c+1);                                                \
      p = (signed char *)_scratch_mem;                                       \
      p[c] = x;                                                              \
      c++;                                                                   \
   }

   #define WRITE_TO_SPRITE16(x) {                                            \
      _grow_scratch_mem((c+1)*sizeof(short));                                \
      p = (signed short *)_scratch_mem;                                      \
      p[c] = x;                                                              \
      c++;                                                                   \
   }

   #define WRITE_TO_SPRITE32(x) {                                            \
      _grow_scratch_mem((c+1)*sizeof(long));                                 \
      p = (signed long *)_scratch_mem;                                       \
      p[c] = x;                                                              \
      c++;                                                                   \
   }

   /* helper for building an RLE run */
   #define DO_RLE(bits)                                                      \
   {                                                                         \
      for (y=0; y<bitmap->h; y++) {                                          \
	 run = -1;                                                           \
	 for (x=0; x<bitmap->w; x++) {                                       \
	    pix = al_get_pixel(bitmap, x, y) & 0xFFFFFF;                         \
	    if (pix != bitmap->vtable->mask_color) {                         \
	       if ((run >= 0) && (p[run] > 0) && (p[run] < 127))             \
		  p[run]++;                                                  \
	       else {                                                        \
		  run = c;                                                   \
		  WRITE_TO_SPRITE##bits(1);                                  \
	       }                                                             \
	       WRITE_TO_SPRITE##bits(al_get_pixel(bitmap, x, y));                \
	    }                                                                \
	    else {                                                           \
	       if ((run >= 0) && (p[run] < 0) && (p[run] > -128))            \
		  p[run]--;                                                  \
	       else {                                                        \
		  run = c;                                                   \
		  WRITE_TO_SPRITE##bits(-1);                                 \
	       }                                                             \
	    }                                                                \
	 }                                                                   \
	 WRITE_TO_SPRITE##bits(bitmap->vtable->mask_color);                  \
      }                                                                      \
   }

   c = 0;

   switch (depth) {

      #ifdef ALLEGRO_COLOR8

	 case 8:
	    {
	       signed char *p = (signed char *)_scratch_mem;
	       DO_RLE(8);
	    }
	    break;

      #endif

      #ifdef ALLEGRO_COLOR16

	 case 15:
	 case 16:
	    {
	       signed short *p = (signed short *)_scratch_mem;
	       DO_RLE(16);
	       c *= sizeof(short);
	    }
	    break;

      #endif

      #if (defined ALLEGRO_COLOR24) || (defined ALLEGRO_COLOR32)

	 case 24:
	 case 32:
	    {
	       signed long *p = (signed long *)_scratch_mem;
	       DO_RLE(32);
	       c *= sizeof(long);
	    }
	    break;

      #endif
   }

   s = malloc(sizeof(AL_RLE_SPRITE) + c);

   if (s) {
      s->w = bitmap->w;
      s->h = bitmap->h;
      s->color_depth = depth;
      s->size = c;
      memcpy(s->dat, _scratch_mem, c);
   }

   return s;
}



/* al_destroy_rle_sprite:
 *  Destroys an RLE sprite structure returned by al_create_rle_sprite().
 */
void al_destroy_rle_sprite(AL_RLE_SPRITE *sprite)
{
   if (sprite)
      free(sprite);
}






See more files for this project here

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  beos/
    baccel.cpp
    bclasses.cpp
    bdispsw.cpp
    bgfx.c
    bgfxapi.cpp
    bgfxdrv.c
    bjoy.c
    bjoyapi.cpp
    bjoydrv.c
    bkey.c
    bkeyapi.cpp
    bkeydrv.c
    bmidi.c
    bmidiapi.cpp
    bmididrv.c
    bmousapi.cpp
    bmousdrv.c
    bmouse.c
    bsnd.c
    bsndapi.cpp
    bsnddrv.c
    bswitch.s
    bsysapi.cpp
    bsysdrv.c
    bsystem.c
    btimeapi.cpp
    btimedrv.c
    btimer.c
  c/
    cblit.h
    cblit16.c
    cblit24.c
    cblit32.c
  dos/
  i386/
  linux/
  mac/
  misc/
  ppc/
  qnx/
  unix/
  win/
  x/
  allegro.c
  blit.c
  bmp.c
  clip3d.c
  clip3df.c
  colblend.c
  color.c
  config.c
  datafile.c
  dataregi.c
  digmid.c
  dispsw.c
  dither.c
  drvlist.c
  file.c
  fli.c
  flood.c
  font.c
  fsel.c
  gfx.c
  glyph.c
  graphics.c
  gsprite.c
  gui.c
  guiproc.c
  inline.c
  joystick.c
  keyboard.c
  lbm.c
  libc.c
  math.c
  math3d.c
  midi.c
  mixer.c
  modesel.c
  mouse.c
  pcx.c
  poly3d.c
  polygon.c
  quantize.c
  quat.c
  readbmp.c
  rle.c
  rotate.c
  scene3d.c
  sound.c
  spline.c
  stream.c
  text.c
  tga.c
  timer.c
  unicode.c
  vtable.c
  vtable15.c
  vtable16.c
  vtable24.c
  vtable32.c
  vtable8.c