home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / d / desklib / !DeskSrc / FN / Libraries / Clear / c / Load < prev    next >
Encoding:
Text File  |  1996-05-13  |  2.9 KB  |  112 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Clear.c.Load
  12.     Author:  Copyright © 1993, 1994 Jason Howat
  13.     Version: 1.01 (13 May 1994)
  14.     Purpose: Load a Clear file from disk.
  15.     History: 1.00 (16 Dec 1993)   initial version
  16.              1.01 (13 May 1994)   updated to use Desk_Mem_ library
  17. */
  18.  
  19. #include <stdlib.h>
  20.  
  21. #include "Desk.Clear.h"
  22. #include "Desk.File.h"
  23. #include "Desk.Mem.h"
  24. #include "Desk.DeskMem.h"
  25. #include "Desk.JumpRaw.h"
  26.  
  27.  
  28. static Desk_clear_picture *Desk_Clear__AbortLoad(Desk_file_handle in, Desk_clear_picture *temp)
  29. {
  30.   if(in)
  31.     Desk_File_Close(in);
  32.  
  33.   if(temp)
  34.   {
  35.     if(temp->bitmap)
  36.       Desk_Mem_Free((void **)&temp->bitmap);
  37.     if(temp->palette)
  38.       Desk_Mem_Free((void **)&temp->palette);
  39.     Desk_DeskMem_Free(temp);
  40.   }
  41.  
  42.   return NULL;
  43. }
  44.  
  45. Desk_clear_picture *Desk_Clear_Load(char *filename)
  46. {
  47.   Desk_file_handle   in = 0;
  48.   Desk_clear_picture *temp = NULL;
  49.   unsigned      Desk_creator_length = 1;
  50.   unsigned      Desk_bitmap_size;
  51.   int           word;
  52.   unsigned      entry,
  53.                 colours;
  54.   unsigned      i;
  55.   Desk_palette_entry *palette;
  56.  
  57.   Desk_JumpAuto_Try    {
  58.     in = Desk_File_Open(filename, Desk_file_READ);
  59.  
  60.       while(Desk_File_ReadByte(in) > 0)
  61.         Desk_creator_length++;
  62.     
  63.   Desk_File_Seek(in, 0);
  64.  
  65.   temp = Desk_DeskMem_Malloc(sizeof(Desk_clear_picture) + Desk_creator_length);
  66.  
  67.   temp->creator = (char *)&temp[1];
  68.   temp->palette = NULL;
  69.   temp->bitmap = NULL;
  70.  
  71.   Desk_File_ReadBytes(in, temp->creator, Desk_creator_length);
  72.  
  73.   temp->creatorversion = Desk_File_Read32(in);
  74.   temp->width = Desk_File_Read32(in);
  75.   temp->height = Desk_File_Read32(in);
  76.   temp->bpp = Desk_File_Read32(in);
  77.  
  78.   if(temp->bpp > 8)
  79.     Desk_bitmap_size = 3 * temp->width * temp->height;
  80.   else
  81.   {
  82.     colours = 1 << temp->bpp;
  83.  
  84.    Desk_Mem_Alloc((void **)&temp->palette, sizeof(Desk_palette_entry) * colours);
  85.  
  86.     palette = temp->palette;
  87.     for(entry = 0 ; entry < colours ; entry++)
  88.     {
  89.       word = 0;
  90.       for(i = 8 ; i < 25 ; i += 8)
  91.         word += Desk_File_ReadByte(in) << i;
  92.       palette[entry].value = word;
  93.     }
  94.  
  95.     Desk_bitmap_size = temp->width * temp->height;
  96.   }
  97.  
  98.   Desk_Mem_Alloc((void **)&temp->bitmap, Desk_bitmap_size);
  99.  
  100.   Desk_File_ReadBytes(in, temp->bitmap, Desk_bitmap_size);
  101.  
  102.   Desk_File_Close(in);
  103.   }
  104. Desk_JumpAuto_Catch    {
  105.   Desk_Clear__AbortLoad(in, temp);
  106.   Desk_Error2_ReThrow();
  107.     }
  108. Desk_JumpAuto_EndCatch
  109.  
  110.   return temp;
  111. }
  112.