home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmspr.c < prev    next >
C/C++ Source or Header  |  1996-12-04  |  21KB  |  779 lines

  1. /*
  2.  
  3. gbmspr.c - Archimedes Sprite from RiscOS Format support
  4.  
  5. In Archimedes terminology, a sprite is a bitmap with an optional mask plane.
  6.  
  7. Reads a sprite from file created by *ScreenSave or *SSave command.
  8. Will also write such a file containing a single sprite.
  9.  
  10. Input options: index=# (default: 0)
  11.  
  12. */
  13.  
  14. /*...sincludes:0:*/
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <memory.h>
  21. #include <malloc.h>
  22. #include "gbm.h"
  23. #include "gbmhelp.h"
  24.  
  25. /*...vgbm\46\h:0:*/
  26. /*...vgbmhelp\46\h:0:*/
  27.  
  28. #ifndef min
  29. #define    min(a,b)    (((a)<(b))?(a):(b))
  30. #endif
  31. /*...e*/
  32. /*...suseful:0:*/
  33. #define    low_byte(w)    ((byte)  ((w)&0x00ff)    )
  34. #define    high_byte(w)    ((byte) (((w)&0xff00)>>8))
  35. #define    make_word(a,b)    (((word)a) + (((word)b) << 8))
  36.  
  37. /*...sread_word:0:*/
  38. static BOOLEAN read_word(int fd, word *w)
  39.     {
  40.     byte low = 0, high = 0;
  41.  
  42.     gbm_file_read(fd, (char *) &low, 1);
  43.     gbm_file_read(fd, (char *) &high, 1);
  44.     *w = (word) (low + ((word) high << 8));
  45.     return TRUE;
  46.     }
  47. /*...e*/
  48. /*...sread_dword:0:*/
  49. static BOOLEAN read_dword(int fd, dword *d)
  50.     {
  51.     word low, high;
  52.  
  53.     read_word(fd, &low);
  54.     read_word(fd, &high);
  55.     *d = low + ((dword) high << 16);
  56.     return TRUE;
  57.     }
  58. /*...e*/
  59. /*...swrite_word:0:*/
  60. static BOOLEAN write_word(int fd, word w)
  61.     {
  62.     byte low  = (byte) w;
  63.     byte high = (byte) (w >> 8);
  64.  
  65.     return gbm_file_write(fd, &low, 1) == 1 && gbm_file_write(fd, &high, 1) == 1;
  66.     }
  67. /*...e*/
  68. /*...swrite_dword:0:*/
  69. static BOOLEAN write_dword(int fd, dword d)
  70.     {
  71.     return write_word(fd, (word) d) && write_word(fd, (word) (d >> 16));
  72.     }
  73. /*...e*/
  74. /*...e*/
  75.  
  76. static GBMFT spr_gbmft =
  77.     {
  78.     "Sprite",
  79.     "Archimedes Sprite from RiscOS",
  80.     "SPR SPRITE",
  81.     GBM_FT_R1|GBM_FT_R4|GBM_FT_R8|
  82.     GBM_FT_W1|GBM_FT_W4|GBM_FT_W8,
  83.     };
  84.  
  85. #define    GBM_ERR_SPR_FIRST    ((GBM_ERR) 1400)
  86. #define    GBM_ERR_SPR_MODE    ((GBM_ERR) 1401)
  87. #define    GBM_ERR_SPR_OFFSET    ((GBM_ERR) 1402)
  88. #define    GBM_ERR_SPR_PAL8    ((GBM_ERR) 1403)
  89.  
  90. typedef struct
  91.     {
  92.     long pos_palette, pos_image, pos_mask;
  93.     dword bytes_per_line, first_bit, last_bit, actual_bpp;
  94.     } SPR_PRIV;
  95.  
  96. /*...sbpp_of_mode\44\ mode_of_bpp:0:*/
  97. static int bpp_of_mode[] =
  98.     {
  99.     1,    /*  0: 640x256 */
  100.     2,    /*  1: 320x256 */
  101.     4,    /*  2: 160x256 */
  102.     -1,    /*  3: Text only */
  103.     1,    /*  4: 320x256 */
  104.     2,    /*  5: 160x256 */
  105.     2,    /*  6: 160x256 */
  106.     -1,    /*  7: Teletext */
  107.     2,    /*  8: 640x256 */
  108.     4,    /*  9: 320x256 */
  109.     8,    /* 10: 160x256 */
  110.     2,    /* 11: 640x250 */
  111.     4,    /* 12: 640x256 */
  112.     8,    /* 13: 320x256 */
  113.     4,    /* 14: 640x250 */
  114.     8,    /* 15: 640x256 */
  115.     4,    /* 16: 1056x250 */
  116.     4,    /* 17: 1056x256 */
  117.     1,    /* 18: 640x512 multisync-monitor */
  118.     2,    /* 19: 640x512 multisync-monitor */
  119.     4,    /* 20: 640x512 multisync-monitor */
  120.     8,    /* 21: 640x512 multisync-monitor */
  121.     -1,    /* 22: ? */
  122.     1,    /* 23: 1152x896 61.2Hz-hires-montor */
  123.     8,    /* 24: 1056x256 */
  124.     1,    /* 25: 640x480 multisync-or-60Hz-VGA-monitor */
  125.     2,    /* 26: 640x480 multisync-or-60Hz-VGA-monitor */
  126.     4,    /* 27: 640x480 multisync-or-60Hz-VGA-monitor */
  127.     8,    /* 28: 640x480 multisync-or-60Hz-VGA-monitor */
  128.     };
  129.  
  130. #define    N_MODES    29
  131.  
  132. /* Return highest resolution mode for given bits per pixel. */
  133.  
  134. static int mode_of_bpp[] = { -1,23,-1,-1,17,-1,-1,-1,24 };
  135. /*...e*/
  136. /*...squick tables:0:*/
  137. /* These are to account for the reverse ordering of pixels in a scan line. */
  138.  
  139. static byte nibble_swap[0x100] =
  140.     {
  141.     0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,
  142.     0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0,
  143.     0x01,0x11,0x21,0x31,0x41,0x51,0x61,0x71,
  144.     0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
  145.     0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
  146.     0x82,0x92,0xa2,0xb2,0xc2,0xd2,0xe2,0xf2,
  147.     0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x73,
  148.     0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,
  149.     0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x74,
  150.     0x84,0x94,0xa4,0xb4,0xc4,0xd4,0xe4,0xf4,
  151.     0x05,0x15,0x25,0x35,0x45,0x55,0x65,0x75,
  152.     0x85,0x95,0xa5,0xb5,0xc5,0xd5,0xe5,0xf5,
  153.     0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x76,
  154.     0x86,0x96,0xa6,0xb6,0xc6,0xd6,0xe6,0xf6,
  155.     0x07,0x17,0x27,0x37,0x47,0x57,0x67,0x77,
  156.     0x87,0x97,0xa7,0xb7,0xc7,0xd7,0xe7,0xf7,
  157.     0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78,
  158.     0x88,0x98,0xa8,0xb8,0xc8,0xd8,0xe8,0xf8,
  159.     0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x79,
  160.     0x89,0x99,0xa9,0xb9,0xc9,0xd9,0xe9,0xf9,
  161.     0x0a,0x1a,0x2a,0x3a,0x4a,0x5a,0x6a,0x7a,
  162.     0x8a,0x9a,0xaa,0xba,0xca,0xda,0xea,0xfa,
  163.     0x0b,0x1b,0x2b,0x3b,0x4b,0x5b,0x6b,0x7b,
  164.     0x8b,0x9b,0xab,0xbb,0xcb,0xdb,0xeb,0xfb,
  165.     0x0c,0x1c,0x2c,0x3c,0x4c,0x5c,0x6c,0x7c,
  166.     0x8c,0x9c,0xac,0xbc,0xcc,0xdc,0xec,0xfc,
  167.     0x0d,0x1d,0x2d,0x3d,0x4d,0x5d,0x6d,0x7d,
  168.     0x8d,0x9d,0xad,0xbd,0xcd,0xdd,0xed,0xfd,
  169.     0x0e,0x1e,0x2e,0x3e,0x4e,0x5e,0x6e,0x7e,
  170.     0x8e,0x9e,0xae,0xbe,0xce,0xde,0xee,0xfe,
  171.     0x0f,0x1f,0x2f,0x3f,0x4f,0x5f,0x6f,0x7f,
  172.     0x8f,0x9f,0xaf,0xbf,0xcf,0xdf,0xef,0xff,
  173.     };
  174. static byte bit_swap[0x100] =
  175.     {
  176.     0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,
  177.     0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
  178.     0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8,
  179.     0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8,
  180.     0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4,
  181.     0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4,
  182.     0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec,
  183.     0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc,
  184.     0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2,
  185.     0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2,
  186.     0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea,
  187.     0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa,
  188.     0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6,
  189.     0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6,
  190.     0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee,
  191.     0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe,
  192.     0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1,
  193.     0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1,
  194.     0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9,
  195.     0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9,
  196.     0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5,
  197.     0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5,
  198.     0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed,
  199.     0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd,
  200.     0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3,
  201.     0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3,
  202.     0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb,
  203.     0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb,
  204.     0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7,
  205.     0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7,
  206.     0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef,
  207.     0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff,
  208.     };
  209.  
  210. static byte pair_swap[0x10] =
  211.     {
  212.     0x00,0x10,0x20,0x30,0x01,0x11,0x21,0x31,
  213.     0x02,0x12,0x22,0x32,0x03,0x13,0x23,0x33,
  214.     };
  215. /*...e*/
  216.  
  217. /*...sspr_qft:0:*/
  218. GBM_ERR spr_qft(GBMFT *gbmft)
  219.     {
  220.     *gbmft = spr_gbmft;
  221.     return GBM_ERR_OK;
  222.     }
  223. /*...e*/
  224. /*...sspr_rhdr:0:*/
  225. GBM_ERR spr_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
  226.     {
  227.     SPR_PRIV *priv = (SPR_PRIV *) gbm->priv;
  228.     const char *index;
  229.     int i;
  230.     dword num_sprites, offset_sprite, pos_sprite;
  231.     dword dword_w, scans_h, first_bit, last_bit, bits_per_line;
  232.     dword offset_image, offset_mask, mode;
  233.  
  234.     fn=fn; /* Suppress 'unref arg' compiler warnings */
  235.  
  236.     if ( (index = gbm_find_word_prefix(opt, "index=")) != NULL )
  237.         sscanf(index + 6, "%d", &i);
  238.     else
  239.         i = 0;
  240.  
  241.     read_dword(fd, &num_sprites);
  242.     if ( (dword) i >= num_sprites )
  243.         return GBM_ERR_SPR_OFFSET;
  244.     read_dword(fd, &offset_sprite);
  245.     pos_sprite = gbm_file_lseek(fd, offset_sprite - 4L, SEEK_SET);
  246.  
  247.     while ( i-- > 0 )
  248.         {
  249.         read_dword(fd, &offset_sprite);
  250.         pos_sprite = gbm_file_lseek(fd, pos_sprite + offset_sprite, SEEK_SET);
  251.         }
  252.  
  253.     gbm_file_lseek(fd, 16, SEEK_CUR);    /* Skip 4 byte next-ptr + 12 byte name */
  254.     read_dword(fd, &dword_w);    /* Width in dwords - 1 */
  255.     read_dword(fd, &scans_h);    /* Scan lines high - 1 */
  256.     read_dword(fd, &first_bit);    /* First bit used (left end of row) */
  257.     read_dword(fd, &last_bit);    /* Last bit used (right end of row) */
  258.     read_dword(fd, &offset_image);    /* Offset of image data */
  259.     read_dword(fd, &offset_mask);    /* Offset of mask plane or above if none */
  260.     read_dword(fd, &mode);        /* Mode sprite defined in */
  261.  
  262.     if ( first_bit & 7 )
  263.         return GBM_ERR_SPR_FIRST;
  264.     if ( mode >= N_MODES )
  265.         return GBM_ERR_SPR_MODE;
  266.     if ( (gbm->bpp = priv->actual_bpp = bpp_of_mode[mode]) == -1 )
  267.         return GBM_ERR_SPR_MODE;
  268.  
  269.     gbm->h = (int) (scans_h+1);
  270.     if ( gbm->h < 0 || gbm->h > 10000 )
  271.         return GBM_ERR_BAD_SIZE;
  272.  
  273.     if ( gbm->bpp == 2 )
  274.         gbm->bpp = 4;
  275.  
  276.     bits_per_line = ((dword_w+1) * 32) - first_bit - (32 - (last_bit+1));
  277.     gbm->w = (int) (bits_per_line / priv->actual_bpp);
  278.     if ( gbm->w < 0 || gbm->w > 10000 )
  279.         return GBM_ERR_BAD_SIZE;
  280.  
  281.     priv->pos_palette    = tell(fd);
  282.     priv->pos_image      = (long) pos_sprite + offset_image;
  283.     priv->pos_mask       = (long) pos_sprite + offset_mask;
  284.     priv->bytes_per_line = (dword_w + 1) * 4;
  285.     priv->first_bit      = first_bit;
  286.     priv->last_bit       = last_bit;
  287.  
  288.     return GBM_ERR_OK;
  289.     }
  290. /*...e*/
  291. /*...sspr_rpal:0:*/
  292. /*...sread_pal:0:*/
  293. /* Palette entry is 2 dwords, which are same if no flashing */
  294. /* We will simply use first dword in each case */
  295.  
  296. static BOOLEAN read_pal(int fd, GBMRGB *gbmrgb)
  297.     {
  298.     byte pal[8];
  299.     if ( gbm_file_read(fd, pal, 8) != 8 )
  300.         return FALSE;
  301.     gbmrgb->r = pal[1];
  302.     gbmrgb->g = pal[2];
  303.     gbmrgb->b = pal[3];
  304.     return TRUE;
  305.     }
  306. /*...e*/
  307.  
  308. /*...sgbmrgb_1bpp:0:*/
  309. /*
  310. I do not expect the palette to be missing from a file defined in a mode with 2
  311. colours because the Wimp uses 16 colours, and I am led to beleive it is the
  312. only thing that saves files without the palette. However, if I am wrong...
  313. */
  314.  
  315. static GBMRGB gbmrgb_1bpp[2] =
  316.     {
  317.     {0xff,0xff,0xff},    /* 0=White */
  318.     {0x00,0x00,0x00},    /* 1=black */
  319.     };
  320. /*...e*/
  321. /*...sgbmrgb_2bpp:0:*/
  322. /*
  323. I do not expect the palette to be missing from a file defined in a mode with 4
  324. colours because the Wimp uses 16 colours, and I am led to beleive it is the
  325. only thing that saves files without the palette. However, if I am wrong...
  326. */
  327.  
  328. static GBMRGB gbmrgb_2bpp[4] =
  329.     {
  330.     {0xff,0xff,0xff},
  331.     {0xaa,0xaa,0xaa},
  332.     {0x55,0x55,0x55},
  333.     {0x00,0x00,0x00},
  334.     };
  335. /*...e*/
  336. /*...sgbmrgb_4bpp:0:*/
  337. /*
  338. This is the default Wimp defined 16 colour palette.
  339. The exact r,g,b values are not known so some nice bold examples of each,
  340. according to their descriptions in the RiscOS books have been found manually.
  341. */
  342.  
  343. static GBMRGB gbmrgb_4bpp[16] =
  344.     {
  345.     {0xff,0xff,0xff},    /* 0=white */
  346.     {0xdb,0xdb,0xdb},    /* 1=grey */
  347.     {0xb6,0xb6,0xb6},    /* 2=grey */
  348.     {0x92,0x92,0x92},    /* 3=grey */
  349.     {0x6d,0x6d,0x6d},    /* 4=grey */
  350.     {0x49,0x49,0x49},    /* 5=grey */
  351.     {0x24,0x24,0x24},    /* 6=grey */
  352.     {0x00,0x00,0x00},    /* 7=black */
  353.     {0x00,0x00,0xff},    /* 8=dark blue */
  354.     {0xff,0xff,0x00},    /* 9=yellow */
  355.     {0x00,0xff,0x00},    /* a=green */
  356.     {0xff,0x00,0x00},    /* b=red */
  357.     {0xff,0xd8,0xd8},    /* c=cream */
  358.     {0x40,0x80,0x40},    /* d=army green */
  359.     {0xff,0x9c,0x00},    /* e=orange */
  360.     {0x00,0xb9,0xff},    /* f=light blue */
  361.     };
  362. /*...e*/
  363. /*...sexpand_0x10:0:*/
  364. /*
  365. The Archimedes does not have 0x100 palette registers, it has 0x10 VIDC registers.
  366. Given an 8 bit pixel :-
  367.     Bits 3-0 are bits 3-0 of palette index
  368.     Bit 4 overrides red bit 7
  369.     Bit 5 overrides green bit 6
  370.     Bit 6 overrides green bit 7
  371.     Bit 7 overrides blue bit 7
  372. So we duplicate the 0x10 values we have read, and make the others from them.
  373. */
  374.  
  375. static void expand_0x10(GBMRGB *gbmrgb)
  376.     {
  377.     int i, bank;
  378.  
  379.     for ( bank = 0x10; bank < 0x100; bank += 0x10 )
  380.         {
  381.         byte override_r = ((bank & 0x10) << 3);
  382.         byte override_g = ((bank & 0x60) << 1);
  383.         byte override_b =  (bank & 0x80)      ;
  384.         for ( i = 0; i < 0x10; i++ )
  385.             {
  386.             gbmrgb[bank + i].r = ((gbmrgb[i].r & 0x7f) | override_r);
  387.             gbmrgb[bank + i].g = ((gbmrgb[i].g & 0x3f) | override_g);
  388.             gbmrgb[bank + i].b = ((gbmrgb[i].b & 0x7f) | override_b);
  389.             }
  390.         }
  391.     }
  392. /*...e*/
  393. /*...sexpand_0x40:0:*/
  394. /*
  395. The Archimedes does not have 0x100 palette registers, it has 0x10.
  396. Planning for the future, files can be written with 0x40 palette entries.
  397. Given an 8 bit pixel :-
  398.     Bits 5-0 are bits 5-0 of palette index
  399.     Bit 6 overrides green bit 7
  400.     Bit 7 overrides blue bit 7
  401. So we duplicate the 0x40 values we have read, and make the others from them.
  402. Now, although we have seen files with 0x40 entries, when you take the first
  403. 0x10, and perform expand_0x10 on them, the result is the same as this routine.
  404. Clearly Acorn are looking forward to a day when the VIDC chip has 0x40
  405. registers.
  406. */
  407.  
  408. static void expand_0x40(GBMRGB *gbmrgb)
  409.     {
  410.     int i, bank;
  411.  
  412.     for ( bank = 0; bank < 0x100; bank += 0x40 )
  413.         {
  414.         byte override_g = ((bank & 0x40) << 1);
  415.         byte override_b =  (bank & 0x80)      ;
  416.         for ( i = 0; i < 0x40; i++ )
  417.             {
  418.             gbmrgb[bank + i].r =   gbmrgb[i].r;
  419.             gbmrgb[bank + i].g = ((gbmrgb[i].g & 0x7f) | override_g);
  420.             gbmrgb[bank + i].b = ((gbmrgb[i].b & 0x7f) | override_b);
  421.             }
  422.         }
  423.     }
  424. /*...e*/
  425.  
  426. GBM_ERR spr_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
  427.     {
  428.     SPR_PRIV *priv = (SPR_PRIV *) gbm->priv;
  429.     int npal = (priv->pos_image - priv->pos_palette) / 8;
  430.  
  431.     if ( npal == 0 )
  432. /*...sno palette in file\44\ use the default Wimp one:16:*/
  433. switch ( priv->actual_bpp )
  434.     {
  435.     case 1: memcpy(gbmrgb, gbmrgb_1bpp, sizeof(gbmrgb_1bpp)); break;
  436.     case 2: memcpy(gbmrgb, gbmrgb_2bpp, sizeof(gbmrgb_2bpp)); break;
  437.     case 4: memcpy(gbmrgb, gbmrgb_4bpp, sizeof(gbmrgb_4bpp)); break;
  438.     case 8:
  439. /*...sdefault palette:32:*/
  440. {
  441. int i;
  442. for ( i = 0; i < 0x10; i++ )
  443.     {
  444.     byte tint = ((i & 0x03) << 4);
  445.     gbmrgb[i].r = tint + ((i & 0x04) << 4);
  446.     gbmrgb[i].g = tint;
  447.     gbmrgb[i].b = tint + ((i & 0x08) << 3);
  448.     }
  449. expand_0x10(gbmrgb);
  450. }
  451. /*...e*/
  452.         break;
  453.     }
  454.  
  455. /*...e*/
  456.     else
  457. /*...sread palette from disk:16:*/
  458. {
  459. int i;
  460. gbm_file_lseek(fd, priv->pos_palette, SEEK_SET);
  461. for ( i = 0; i < npal; i++ )
  462.     if ( !read_pal(fd, gbmrgb + i) )
  463.         return GBM_ERR_READ;
  464.  
  465. if ( gbm->bpp == 8 )
  466.     /* Handle getting too few palette entries */
  467.     {
  468.     if ( npal == 0x40 )
  469.         expand_0x40(gbmrgb);
  470.     else if ( npal == 0x10 )
  471.         expand_0x10(gbmrgb);
  472.     else
  473.         return GBM_ERR_SPR_PAL8;
  474.     }
  475. }
  476. /*...e*/
  477.  
  478.     return GBM_ERR_OK;
  479.     }
  480. /*...e*/
  481. /*...sspr_rdata:0:*/
  482. GBM_ERR spr_rdata(int fd, GBM *gbm, byte *data)
  483.     {
  484.     int stride = ((gbm->bpp * gbm->w + 31) / 32) * 4;
  485.     SPR_PRIV *priv = (SPR_PRIV *) gbm->priv;
  486.     int scan_stride = priv->bytes_per_line;
  487.     int scan_first = ((priv->first_bit) >> 3);
  488.     int scan_bytes, i, j;
  489.     byte *datal = data + (gbm->h - 1) * stride;
  490.  
  491.     gbm_file_lseek(fd, priv->pos_image, SEEK_SET);
  492.  
  493.     switch ( priv->actual_bpp )
  494.         {
  495. /*...s8 \45\ read mapped pixels:16:*/
  496. case 8:
  497.     scan_bytes = gbm->w;
  498.     for ( j = 0; j < gbm->h; j++, datal -= stride )
  499.         {
  500.         gbm_file_lseek(fd, priv->pos_image + j * scan_stride + scan_first, SEEK_SET);
  501.         if ( gbm_file_read(fd, datal, scan_bytes) != scan_bytes )
  502.             return GBM_ERR_READ;
  503.         }
  504.     break;
  505. /*...e*/
  506. /*...s4 \45\ read pixels\44\ nibble swapping:16:*/
  507. case 4:
  508.     scan_bytes = ((unsigned)(gbm->w + 1) >> 1);
  509.     for ( j = 0; j < gbm->h; j++, datal -= stride )
  510.         {
  511.         gbm_file_lseek(fd, priv->pos_image + j * scan_stride + scan_first, SEEK_SET);
  512.         if ( gbm_file_read(fd, datal, scan_bytes) != scan_bytes )
  513.             return GBM_ERR_READ;
  514.         for ( i = 0; i < scan_bytes; i++ )
  515.             datal[i] = nibble_swap[datal[i]];
  516.         }
  517.     break;
  518. /*...e*/
  519. /*...s2 \45\ read pixels\44\ bit\45\pair reversing and expanding:16:*/
  520. /*
  521. Data is coming in a 2bpp, but we don't actually support this.
  522. So we will expand the data to 4bpp as we read it.
  523. We will do this inline, by reading into the second half.
  524. */
  525.  
  526. case 2:
  527.     scan_bytes = ((unsigned)(gbm->w + 3) >> 2);
  528.     for ( j = 0; j < gbm->h; j++, datal -= stride )
  529.         {
  530.         gbm_file_lseek(fd, priv->pos_image + j * scan_stride + scan_first, SEEK_SET);
  531.         if ( gbm_file_read(fd, datal + scan_bytes, scan_bytes) != scan_bytes )
  532.             return GBM_ERR_READ;
  533.         for ( i = 0; i < scan_bytes; i++ )
  534.             {
  535.             datal[i * 2    ] = pair_swap[datal[scan_bytes + i] & 0x0f];
  536.             datal[i * 2 + 1] = pair_swap[datal[scan_bytes + i] >> 4  ];
  537.             }
  538.         }
  539.     break;
  540. /*...e*/
  541. /*...s1 \45\ read pixels\44\ bit reversing:16:*/
  542. case 1:
  543.     scan_bytes = ((unsigned)(gbm->w + 7) >> 3);
  544.     for ( j = 0; j < gbm->h; j++, datal -= stride )
  545.         {
  546.         gbm_file_lseek(fd, priv->pos_image + j * scan_stride + scan_first, SEEK_SET);
  547.         if ( gbm_file_read(fd, datal, scan_bytes) != scan_bytes )
  548.             return GBM_ERR_READ;
  549.         for ( i = 0; i < scan_bytes; i++ )
  550.             datal[i] = bit_swap[datal[i]];
  551.         }
  552.     break;
  553. /*...e*/
  554.         }
  555.  
  556.     return GBM_ERR_OK;
  557.     }
  558. /*...e*/
  559. /*...sspr_w:0:*/
  560. /*
  561. We have a problem here for 256 colour modes.
  562. The 256 colours we are asked to write can be any colours and unrelated.
  563. There are only 16 VIDC palette registers, and the other 4 bits in an 8 bit
  564. byte override specific palette entry bits.
  565. We will ignore all but the top 4 bits of each colour component. ie: just 7-4.
  566. Therefore we will write a specifically fixed palette :-
  567.     Bits 1 to 0 will be bits 5 and 4 for all 3 guns.
  568.     Bit 2 will be red bit 6
  569.     Bit 3 will be blue bit 6
  570.     Bit 4 will be red bit 7
  571.     Bit 5 will be green bit 6
  572.     Bit 6 will be green bit 7
  573.     Bit 7 will be blue bit 7
  574. This is the default palette used by RiscOS.
  575. We will map all incoming palette entrys first, to give a quick lookup table.
  576. */
  577.  
  578. /*...swrite_pal:0:*/
  579. static BOOLEAN write_pal(int fd, GBMRGB gbmrgb)
  580.     {
  581.     byte pal[8];
  582.     int j;
  583.     pal[0] = pal[4] = 0x00;
  584.     pal[1] = pal[5] = (byte) (gbmrgb.r & 0xf0);
  585.     pal[2] = pal[6] = (byte) (gbmrgb.g & 0xf0); 
  586.     pal[3] = pal[7] = (byte) (gbmrgb.b & 0xf0); 
  587.     for ( j = 0; j < 8; j++ )
  588.         pal[j] += (pal[j] >> 4);
  589.     return gbm_file_write(fd, pal, 8) == 8;
  590.     }
  591. /*...e*/
  592.  
  593. GBM_ERR spr_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
  594.     {
  595.     int stride = ((gbm->bpp * gbm->w + 31) / 32) * 4;
  596.     int i, j, npal = ( 1 << gbm->bpp );
  597.     dword dword_w, last_bit, offset_image, mode;
  598.     char name[12];
  599.     byte qpal[0x100], *buf;
  600.     BOOLEAN ok;
  601.  
  602.     opt=opt; /* Suppress 'unref arg' warning */
  603.  
  604.     if ( gbm->bpp == 24 )
  605.         return GBM_ERR_NOT_SUPP;
  606.  
  607.     if ( npal == 0x100 )
  608.         npal = 0x10;
  609.  
  610.     ok  = write_dword(fd, 1);        /* Number of sprites */
  611.     ok &= write_dword(fd, 12 + 4);        /* Offset to sprite */
  612.     ok &= write_dword(fd, (12 + 44 + npal * 8 + stride * gbm->h) + 4); 
  613.                         /* Offset to free word (beyond sprites) */
  614.     ok &= write_dword(fd, 44 + npal * 8 + stride * gbm->h);
  615.  
  616. /*...sbuild a name:8:*/
  617. /*
  618. We will use the supplied filename to build the name field.
  619. We will ignore the path part (if any present).
  620. Also we will chop of the file extension of .spr if present.
  621. (This change was by customer request).
  622. Also, map to lower case to keep RiscOS programs happy.
  623. This isn't documented, but appears to be the case.
  624. */
  625.  
  626. {
  627. const char *p = fn;
  628. char *q;
  629. if ( (q = strrchr(p, '\\')) != NULL )
  630.     p = q + 1;
  631. if ( (q = strrchr(p, '/')) != NULL )
  632.     p = q + 1;
  633. memset(name, 0, 12);
  634. strncpy(name, p, 11);
  635. if ( (q = strrchr(name, '.')) != NULL && gbm_same(q + 1, "spr", 4) )
  636.     memset(q, '\0', 4);
  637. for ( q = name; (q - name) < 12 && *q != '\0'; q++ )
  638.     *q = tolower(*q);
  639. }
  640. /*...e*/
  641.     ok &= ( gbm_file_write(fd, name, 12) == 12 );
  642.  
  643.     dword_w = ((gbm->w * gbm->bpp + 31) / 32) - 1;
  644.     ok &= write_dword(fd, dword_w);        /* Width in dwords - 1 */
  645.     ok &= write_dword(fd, gbm->h - 1);    /* Scan lines high - 1 */
  646.     ok &= write_dword(fd, 0);        /* First bit used (left end of row) */
  647.     last_bit = ((gbm->w * gbm->bpp - 1) & 0x1f);
  648.     ok &= write_dword(fd, last_bit);    /* Last bit used (right end of row) */
  649.     offset_image = ( 44 + npal * 8 );
  650.     ok &= write_dword(fd, offset_image);    /* Offset of image data */
  651.     ok &= write_dword(fd, offset_image);    /* Offset of mask plane or above if none */
  652.     mode = mode_of_bpp[gbm->bpp];
  653.     ok &= write_dword(fd, mode);        /* Mode sprite defined in */
  654.  
  655.     if ( !ok )
  656.         return GBM_ERR_WRITE;
  657.  
  658.     /* Write palette */
  659.  
  660.     switch ( gbm->bpp )
  661.         {
  662. /*...s8 \45\ write RiscOS default palette\44\ and work out mapping:16:*/
  663. case 8:
  664.  
  665.     /* Write the fixed RiscOS default palette */
  666.     for ( i = 0; i < 0x10; i++ )
  667.         {
  668.         GBMRGB gbmrgb_def;
  669.         byte tint = ((i & 0x03) << 4);
  670.         gbmrgb_def.r = tint + ((i & 0x04) << 4);
  671.         gbmrgb_def.g = tint;
  672.         gbmrgb_def.b = tint + ((i & 0x08) << 3);
  673.         if ( !write_pal(fd, gbmrgb_def) )
  674.             return GBM_ERR_WRITE;
  675.         }
  676.  
  677.     /* Determine palette mapping */
  678.     for ( i = 0; i < 0x100; i++ )
  679.         {
  680.         byte r = gbmrgb[i].r;
  681.         byte g = gbmrgb[i].g;
  682.         byte b = gbmrgb[i].b;
  683.         byte k32 = ((((r & 0x30) + (g & 0x30) + (b & 0x30)) / 3) & 0x30);
  684.         qpal[i] =  (b & 0x80U)       +
  685.               ((g & 0xc0U) >> 1) +
  686.               ((r & 0x80U) >> 3) +
  687.               ((b & 0x40U) >> 3) +
  688.               ((r & 0x40U) >> 4) +
  689.                (k32 >> 4);
  690.         }
  691.     break;
  692. /*...e*/
  693. /*...s4\44\1 \45\ write 16 or 2 entry palette:16:*/
  694. case 4:
  695. case 1:
  696.     /* Write the palette */
  697.     for ( i = 0; i < (1 << gbm->bpp); i++ )
  698.         if ( !write_pal(fd, gbmrgb[i]) )
  699.             return GBM_ERR_WRITE;
  700.     break;
  701. /*...e*/
  702.         }
  703.  
  704.     /* Write data */
  705.  
  706.     if ( (buf = malloc((size_t) stride)) == NULL )
  707.         return GBM_ERR_MEM;
  708.     memset(buf, 0, stride);
  709.  
  710.     data += (gbm->h - 1) * stride; /* Start at the top */
  711.     switch ( gbm->bpp )
  712.         {
  713. /*...s8 \45\ write mapped pixels\44\ funny order:16:*/
  714. case 8:
  715.     for ( j = 0; j < gbm->h; j++, data -= stride )
  716.         {
  717.         for ( i = 0; i < stride; i++ )
  718.             buf[i] = qpal[data[i]];
  719.         if ( gbm_file_write(fd, buf, stride) != stride )
  720.             {
  721.             free(buf);
  722.             return GBM_ERR_WRITE;
  723.             }
  724.         }
  725.     break;
  726. /*...e*/
  727. /*...s4 \45\ write pixels\44\ funny order:16:*/
  728. case 4:
  729.     for ( j = 0; j < gbm->h; j++, data -= stride )
  730.         {
  731.         for ( i = 0; i < stride; i++ )
  732.             buf[i] = nibble_swap[data[i]];
  733.         if ( gbm_file_write(fd, buf, stride) != stride )
  734.             {
  735.             free(buf);
  736.             return GBM_ERR_WRITE;
  737.             }
  738.         }
  739.     break;
  740. /*...e*/
  741. /*...s1 \45\ write pixels\44\ funny order:16:*/
  742. case 1:
  743.     for ( j = 0; j < gbm->h; j++, data -= stride )
  744.         {
  745.         for ( i = 0; i < stride; i++ )
  746.             buf[i] = bit_swap[data[i]];
  747.         if ( gbm_file_write(fd, buf, stride) != stride )
  748.             {
  749.             free(buf);
  750.             return GBM_ERR_WRITE;
  751.             }
  752.         }
  753.     break;
  754. /*...e*/
  755.         }
  756.  
  757.     free(buf);
  758.  
  759.     return GBM_ERR_OK;
  760.     }
  761. /*...e*/
  762. /*...sspr_err:0:*/
  763. const char *spr_err(GBM_ERR rc)
  764.     {
  765.     switch ( (int) rc )
  766.         {
  767.         case GBM_ERR_SPR_FIRST:
  768.             return "sprite has first bit that is not a multiple of 8";
  769.         case GBM_ERR_SPR_MODE:
  770.             return "sprite defined in unknown mode";
  771.         case GBM_ERR_SPR_OFFSET:
  772.             return "less sprites in file than index requested";
  773.         case GBM_ERR_SPR_PAL8:
  774.             return "8 bit file does not have 0, 16 or 64 palette entries in it";
  775.         }
  776.     return NULL;
  777.     }
  778. /*...e*/
  779.