/*
 * CBLibrary - SprFormats
 * Copyright (C) 2003  Chris Bazley
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* SprFormats.h declares two types and defines many constants for programs that
   manipulate Sprite files or use the sprite facilities provided by RISC OS.

Dependencies: N/A.
Message tokens: N/A.
History:
  CJB: 24-Jun-04: Added extra constant definitions to describe the layout of
                  the sprite info word.
  CJB: 04-Nov-04: Added summary and dependency information.
  CJB: 10-Jul-05: Added definitions of OS_SpriteOp plot codes.
  CJB: 18-Apr-06: Now uses unsigned integers from <stdint.h> within data
                  structures (PRM 5a says that OS_SpriteOp 17 treats offsets as
                  unsigned). Width is important (sprite file has same format).
  CJB: 02-Nov-06: Added new macro SPRITE_RIGHT_BIT.
  CJB: 18-Nov-06: Added symbolic definition of OS_SpriteOp reason code 24.
  CJB: 03-Dec-06: Fixed the SPRITE_RIGHT_BIT macro to work with pixel sizes
                  other than 8 bits.
  CJB: 09-Dec-06: Added conditional definition of sprite file type value.
*/

#ifndef SprFormats_h
#define SprFormats_h

#include <stdint.h>

/* Modifiers for OS_SpriteOp reason code (add one) */
#define SPRITEOP_SYSTEMAREA       0
#define SPRITEOP_USERAREA_SPRNAME (1u << 8)
#define SPRITEOP_USERAREA_SPRPTR  (1u << 9)

/* Error numbers */
#define SPRITE_ERR_CREATEMEM   0x82
#define SPRITE_ERR_MEMORY      0x85
#define SPRITE_ERR_DOESNTEXIST 0x86

/* OS_SpriteOp reason codes */
#define SPRITEOP_READCTRLBLOCK       8
#define SPRITEOP_INIT_AREA           9
#define SPRITEOP_LOAD_AREA           10
#define SPRITEOP_MERGE_AREA          11
#define SPRITEOP_SAVE_AREA           12
#define SPRITEOP_GET_SPR             14
#define SPRITEOP_CREATE              15
#define SPRITEOP_GET_SPR_COORDS      16
#define SPRITEOP_VERIFY_AREA         17
#define SPRITEOP_SELECT_SPRITE       24
#define SPRITEOP_DELETE              25
#define SPRITEOP_RENAME              26
#define SPRITEOP_PLOT                28
#define SPRITEOP_CREATE_MASK         29
#define SPRITEOP_PLOTCOORDS          34
#define SPRITEOP_SETPTRSHAPE         36
#define SPRITEOP_READINFO            40
#define SPRITEOP_READPIXCOL          41
#define SPRITEOP_PLOTSCALED          52
#define SPRITEOP_SWITCHOUTPUT_SPRITE 60
#define SPRITEOP_SWITCHOUTPUT_MASK   61

#define SPRITE_INFO_NOT_MODE_SEL   (1u << 0)
#define SPRITE_INFO_HOZ_DPI_SHIFT  1
#define SPRITE_INFO_HOZ_DPI_MASK   0x00003ffe
#define SPRITE_INFO_VER_DPI_SHIFT  14
#define SPRITE_INFO_VER_DPI_MASK   0x07ffc000
#define SPRITE_INFO_TYPE_SHIFT     27
#define SPRITE_INFO_TYPE_MASK      0xf8000000

#define SPRITE_TYPE_OLD     0
#define SPRITE_TYPE_1BPP    1
#define SPRITE_TYPE_2BPP    2
#define SPRITE_TYPE_4BPP    3
#define SPRITE_TYPE_8BPP    4
#define SPRITE_TYPE_16BPP   5
#define SPRITE_TYPE_32BPP   6
#define SPRITE_TYPE_CMYK    7

/* OS_SpriteOp plot codes */
#define SPRITE_ACTION_OVERWRITE 0
#define SPRITE_ACTION_OR        1
#define SPRITE_ACTION_AND       2
#define SPRITE_ACTION_EOR       3
#define SPRITE_ACTION_INVERT    4
#define SPRITE_ACTION_LEAVE     5
#define SPRITE_ACTION_AND_NOT   6
#define SPRITE_ACTION_OR_NOT    7
#define SPRITE_ACTION_USE_MASK  (1u << 3)

/* Calculate the last bit used (0 - 31) in the last word of each row of pixel
   data for a sprite. Assumes no lefthand wastage. */
#define SPRITE_RIGHT_BIT(width, bpp) (width > 0 ? \
        ((bpp * (width)) % 32 > 0 ? (bpp * (width)) % 32 : 32) - 1 : 0)

#ifndef FILETYPE_SPRITE
#define FILETYPE_SPRITE 0xff9
#endif

/* Structure of sprite area header */
typedef struct
{  uint32_t size;
   uint32_t sprite_count;
   uint32_t first;
   uint32_t used;
   int32_t extension_words[];
} spriteareaheader;

/* Structure of sprite header */
typedef struct
{  uint32_t size;
   char     name[12];
   uint32_t width;
   uint32_t height;
   uint32_t left_bit;
   uint32_t right_bit;
   uint32_t image;
   uint32_t mask;
   uint32_t type;
   uint32_t palette_data[];
} spriteheader;

#endif
