/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Clear.c.Create
    Author:  Copyright  1993, 1994 Jason Howat
    Version: 1.01 (13 May 1994)
    Purpose: Allocate memory for a Clear file.
    History: 1.00 (16 Dec 1993)   initial version
             1.01 (13 May 1994)   updated to use Desk_Mem_ library
*/


#include <stdlib.h>
#include "Desk.Clear.h"
#include "Desk.Mem.h"
#include "Desk.DeskMem.h"

#include "Desk.Clear.h"
#include "ClearDefs.h"




Desk_clear_picture *Desk_Clear_Create(unsigned width, unsigned height, unsigned bpp)
{
  Desk_clear_picture *temp;
  unsigned      Desk_bitmap_size;
  

  temp = Desk_DeskMem_Malloc(sizeof(Desk_clear_picture));
  temp->creator = Desk_clear__creator;
  temp->creatorversion = Desk_clear__creatorversion;
  temp->width = width;
  temp->height = height;
  temp->bpp = bpp;
  if(bpp > 8)
  {
    temp->palette = NULL;
    Desk_bitmap_size = 3 * width * height;
  }
  else
  {
    if(!Desk_Mem_Alloc((void **)&temp->palette, sizeof(Desk_palette_entry) * (1 << bpp)))
    {
      Desk_DeskMem_Free(temp);
      return NULL;
    }

    Desk_bitmap_size = width * height;
  }

  if(!Desk_Mem_Alloc((void **)&temp->bitmap, Desk_bitmap_size))
  {
    if(temp->palette)
      Desk_Mem_Free((void **)temp->palette);
    Desk_DeskMem_Free(temp);
  }

  return temp;
}
