Topic : DOS Game Programming
Author : Alexander Russell
Page : << Previous 4  Next >>
Go to page :


( y=0; y < shp1->height; y++ )

      {

      x=0;

      while ( x < shp1->width )

         {

         if ( *s0 )

            {

            /* bytes isn't a zero so copy over until end of line or a zero */

            num=0;

            *s1++=1;

            t=s1;    /* save where the count goes */

            s1++;

            size+=2;



            /* copy and count non-zero bytes */

            while ( *s0 && x < shp1->width )

               {

               num++;

               x++;

               *s1++=*s0++;

               size++;

               }



            /* store count */

            *t=num;

            }

         else

            {

            /* count number of zeros to skip and store count */

            num=1;

            *s1++=0;

            s0++;

            x++;

            size++;



            while ( *s0 == 0 && x < shp1->width )

               {

               num++;

               x++;

               s0++;

               }



            *s1++=num;

            size++;

            }

         }

      }



   return size;



}





/* for 256 color PC images ONLY, works with dpaint images

   Make sure b is big enough for unpacked image.

   Don't use this to get ideas for compression as

   there are much better ways to do even simple RLE encoding.

*/

/* ---------------------- unpack_pcx() ------------------ January 4,1995 */

int unpack_pcx(char *fname, unsigned char far *b, unsigned char *pal,

               int *p_width, int *p_height)

{

   int err=0, i;

   unsigned int size;

   pcx_head_t pcx;

   unsigned char far *t1, far *raw;



   raw=far_load(fname, &size);  // load the whole file into mem,



   if ( raw )

      {

      if ( size > PCX_HD_SIZE )

         {

         _fmemcpy(&pcx, raw, PCX_HD_SIZE);  // far memcpy

         // get width and height

         *p_width =pcx.xmax - pcx.xmin + 1;

         *p_height=pcx.ymax - pcx.ymin + 1;

         // get palette

         // the palette is just tacked onto the end of the file

         _fmemcpy(pal, raw+size-PAL_SIZE, PAL_SIZE);



   // must shift the palette values as the VGA only uses 6 bits   

         for ( i=0, t1=pal; i < PAL_SIZE; i++, t1++ )

            *t1>>=2;



         // this type of de-compression is common to most pcx files



         t1=raw+PCX_HD_SIZE; // skip header



         // decompress raw into b -------------------------------

         size-=PCX_HD_SIZE; // skip header

         size-=PAL_SIZE; // don't include palette at end

         while ( size )

            {

            if ( (*t1 & 0xc0) == 0xc0 ) // are the two high bits set?

               {

               // hi bits set, its a run

               i=*t1 & 0x3f;    // mask with 00111111 to get size of run

               t1++;            // next byte is color to repeat

               size--;

               while ( i-- )

                  *b++=*t1;

               t1++;

               size--;

               }

            else

               {

               *b++=*t1++;  // not a run, copy one byte

               size--;

               }

            }



         farfree(raw);

         }

      else

         err=2;

      }

   else

      err=1;



   return(err);

}





int save_bitmap(unsigned char far *buff, unsigned int width,

                unsigned int height, char *fname)

{

   char new_name[25], *t1;

   FILE *fp;

   int err=0;



   // make a new file name, by removing the pcx extension, and

   // adding a m13 extension



   strcpy(new_name, fname);

   t1=new_name;

   while ( *t1 && *t1 != '.' )

      t1++;



   if ( *t1 == '.' )

      {

      t1++;

      strcpy(t1, "m13");

      }

   else

      strcat(new_name, ".m13");



   // write out the file

   fp=fopen(new_name, "wb");

   if ( fp )

      {

      fwrite(&width, 1, 2, fp);

      fwrite(&height, 1, 2, fp);

      far_fwrite(buff, 1, width*height, fp);



      fclose(fp);

      }

   else

      {

      printf("ERROR! creating file: %s\n", new_name);

      err=1;

      }





   return err;



}



int save_bitmap_rle(unsigned char far *buff, unsigned int width,

                unsigned int height, char *fname)

{

   char new_name[25], *t1;

   FILE *fp;

   shape_t in, out;

   unsigned int size;

   int err=0;



   // make a new file name, by removing the pcx extension, and

   // adding a rle extension



   strcpy(new_name, fname);

   t1=new_name;

   while ( *t1 && *t1 != '.' )

      t1++;



   if ( *t1 == '.' )

      {

      t1++;

      strcpy(t1, "rle");

      }

   else

      strcat(new_name, ".rle");



   // as much memory as we will ever need

   out.bitmap=farmalloc(64000u);

   if ( out.bitmap )

      {

      in.width=width;

      in.height=height;

      in.bitmap=buff;





      out.width=in.width;

      out.height=in.height;



      // turn the linear bitmap into a RLE bitmap

      size=pack_shape(&in, &out);

      if ( size )

         {

         // write out the file

         fp=fopen(new_name, "wb");

         if ( fp


Page : << Previous 4  Next >>