IFF15BPP.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "iff.h"
  17. #define INDEX_TO_15BPP(i) ((WORD)((((palptr[(i)].r/2)&31)<<10)+(((palptr[(i)].g/2)&31)<<5)+((palptr[(i)].b/2 )&31)))
  18. extern int parse_iff(FILE *ifile,struct bitmap_header *bitmap_header);
  19. // Parse ilbm style data at my_bh->raw_data.
  20. BITMAP15 * IFF_To_15BPP(char * ifilename)
  21. {
  22. unsigned int * MallocSize;
  23. int x,y,pl,bc;
  24. int bytes_per_row,color;
  25. int mask,first_bit_value;
  26. FILE *ifile;
  27. struct bitmap_header iff_bitmap_header;
  28. struct bitmap_header * my_bh;
  29. int Process_width,Process_height;
  30. unsigned char *p;
  31. struct pal_entry *palptr;
  32. int newptr = 0;
  33. int i;
  34. BITMAP15 * new;
  35. my_bh = &iff_bitmap_header;
  36. my_bh->raw_data = NULL;
  37. Process_width = 32767; // say to process full width of bitmap
  38. Process_height = 32767; // say to process full height of bitmap
  39. if ((ifile = fopen(ifilename,"rb")) == NULL) {
  40. printf("Unable to open bitmap file %s.\n", ifilename);
  41. exit(1);
  42. }
  43. parse_iff(ifile,&iff_bitmap_header);
  44. palptr=my_bh->palette;
  45. p=my_bh->raw_data;
  46. printf( "Raw data is at %X\n", (unsigned int)my_bh->raw_data );
  47. MallocSize = my_bh->raw_data;
  48. MallocSize--;
  49. printf( "Raw data size: %d\n", *MallocSize );
  50. if (Process_width > iff_bitmap_header.w)
  51. Process_width = iff_bitmap_header.w;
  52. if (Process_height > iff_bitmap_header.h)
  53. Process_height = iff_bitmap_header.h;
  54. //printf( "%d, %d\n", Process_width, Process_height );
  55. new = (BITMAP15 *)malloc( sizeof(BITMAP15)+ (Process_width * Process_height * 2 ));
  56. if (new==NULL) exit(1);
  57. new->X = 0;
  58. new->Y = 0;
  59. new->Width = Process_width;
  60. new->Height = Process_height;
  61. new->Rowsize = Process_width*2;
  62. new->Type = BM_RGB15;
  63. new->DataPtr = 0;
  64. //printf("Process_width = %i, Process_height = %i\n",Process_width,Process_height);
  65. first_bit_value = 1 << (my_bh->nplanes-1);
  66. bytes_per_row = 2*((my_bh->w+15)/16);
  67. for (y=0; y<Process_height; y++) {
  68. bc = Process_width;
  69. p = &my_bh->raw_data[y*bytes_per_row*my_bh->nplanes];
  70. switch (my_bh->type) {
  71. case PBM_TYPE:
  72. for (x=0; x<my_bh->w; x++) {
  73. new->Data[newptr++] = INDEX_TO_15BPP(my_bh->raw_data[y*my_bh->w+x]);
  74. }
  75. break;
  76. case ILBM_TYPE:
  77. for (x=0; x<bytes_per_row; x++) {
  78. for (mask=128; mask; mask /=2) {
  79. color = 0;
  80. for (pl=0; pl<my_bh->nplanes; pl++) {
  81. color /= 2;
  82. if ( p[pl*bytes_per_row+x] & mask)
  83. color += first_bit_value;
  84. }
  85. new->Data[newptr++] = INDEX_TO_15BPP(color);
  86. bc--;
  87. if (!bc)
  88. goto line_done;
  89. }
  90. }
  91. line_done: ;
  92. break;
  93. }
  94. }
  95. printf( "Freeing raw data at %X\n", (unsigned int)my_bh->raw_data );
  96. MallocSize = my_bh->raw_data;
  97. MallocSize--;
  98. printf( "Raw data size: %d\n", *MallocSize );
  99. free( my_bh->raw_data );
  100. return new;
  101. }