home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / pal / iff15bpp.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  4KB  |  122 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16.  
  17.  
  18. #include "iff.h"
  19.  
  20. #define INDEX_TO_15BPP(i) ((WORD)((((palptr[(i)].r/2)&31)<<10)+(((palptr[(i)].g/2)&31)<<5)+((palptr[(i)].b/2 )&31)))
  21.  
  22. extern int parse_iff(FILE *ifile,struct bitmap_header *bitmap_header);
  23.  
  24.  
  25. // Parse ilbm style data at my_bh->raw_data.
  26. BITMAP15 * IFF_To_15BPP(char * ifilename)
  27. {
  28.     unsigned int * MallocSize;
  29.     int x,y,pl,bc;
  30.     int bytes_per_row,color;
  31.     int mask,first_bit_value;
  32.     FILE *ifile;
  33.     struct bitmap_header iff_bitmap_header;
  34.     struct bitmap_header * my_bh;
  35.     int Process_width,Process_height;
  36.     unsigned char  *p;
  37.     struct pal_entry *palptr;
  38.     int newptr = 0;
  39.     int i;
  40.     BITMAP15 * new;
  41.  
  42.     my_bh = &iff_bitmap_header;
  43.     my_bh->raw_data = NULL;
  44.  
  45.     Process_width = 32767;  // say to process full width of bitmap
  46.     Process_height = 32767; // say to process full height of bitmap
  47.  
  48.     if ((ifile = fopen(ifilename,"rb")) == NULL) {
  49.         printf("Unable to open bitmap file %s.\n", ifilename);
  50.         exit(1);
  51.     }
  52.  
  53.     parse_iff(ifile,&iff_bitmap_header);
  54.  
  55.     palptr=my_bh->palette;
  56.     p=my_bh->raw_data;
  57.  
  58.     printf( "Raw data is at %X\n", (unsigned int)my_bh->raw_data );
  59.     MallocSize = my_bh->raw_data;
  60.     MallocSize--;
  61.     printf( "Raw data size: %d\n", *MallocSize );
  62.  
  63.     if (Process_width > iff_bitmap_header.w)
  64.         Process_width = iff_bitmap_header.w;
  65.  
  66.     if (Process_height > iff_bitmap_header.h)
  67.         Process_height = iff_bitmap_header.h;
  68.  
  69.     //printf( "%d, %d\n", Process_width, Process_height );
  70.  
  71.     new = (BITMAP15 *)malloc( sizeof(BITMAP15)+ (Process_width * Process_height * 2 ));
  72.     if (new==NULL) exit(1);
  73.  
  74.     new->X = 0;
  75.     new->Y = 0;
  76.     new->Width = Process_width;
  77.     new->Height = Process_height;
  78.     new->Rowsize = Process_width*2;
  79.     new->Type = BM_RGB15;
  80.     new->DataPtr = 0;
  81.  
  82.     //printf("Process_width = %i, Process_height = %i\n",Process_width,Process_height);
  83.     first_bit_value = 1 << (my_bh->nplanes-1);
  84.     bytes_per_row = 2*((my_bh->w+15)/16);
  85.     for (y=0; y<Process_height; y++) {
  86.         bc = Process_width;
  87.         p = &my_bh->raw_data[y*bytes_per_row*my_bh->nplanes];
  88.  
  89.         switch (my_bh->type) {
  90.             case PBM_TYPE:
  91.                 for (x=0; x<my_bh->w; x++) {
  92.                     new->Data[newptr++] = INDEX_TO_15BPP(my_bh->raw_data[y*my_bh->w+x]);
  93.                 }
  94.                 break;
  95.             case ILBM_TYPE:
  96.                 for (x=0; x<bytes_per_row; x++) {
  97.                     for (mask=128; mask; mask /=2) {
  98.                         color = 0;
  99.                         for (pl=0; pl<my_bh->nplanes; pl++) {
  100.                             color /= 2;
  101.                             if ( p[pl*bytes_per_row+x] & mask)
  102.                                 color += first_bit_value;
  103.                         }
  104.                         new->Data[newptr++] = INDEX_TO_15BPP(color);
  105.                         bc--;
  106.                         if (!bc)
  107.                             goto line_done;
  108.                     }
  109.                 }
  110. line_done: ;
  111.                 break;
  112.         }
  113.     }
  114.     printf( "Freeing raw data at %X\n", (unsigned int)my_bh->raw_data );
  115.     MallocSize = my_bh->raw_data;
  116.     MallocSize--;
  117.     printf( "Raw data size: %d\n", *MallocSize );
  118.     free( my_bh->raw_data );
  119.     return new;
  120. }
  121.  
  122.