home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / d / desklib / !DeskSrc / FN / Libraries / Clear / c0 / Load < prev    next >
Encoding:
Text File  |  1996-05-13  |  2.9 KB  |  111 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.  
  26.  
  27. static Desk_clear_picture *Desk_Clear__AbortLoad(Desk_file_handle in, Desk_clear_picture *temp)
  28. {
  29.   if(in)
  30.     Desk_File_Close(in);
  31.  
  32.   if(temp)
  33.   {
  34.     if(temp->bitmap)
  35.       Desk_Mem_Free((void **)&temp->bitmap);
  36.     if(temp->palette)
  37.       Desk_Mem_Free((void **)&temp->palette);
  38.     Desk_DeskMem_Free(temp);
  39.   }
  40.  
  41.   return NULL;
  42. }
  43.  
  44. Desk_clear_picture *Desk_Clear_Load(char *filename)
  45. {
  46.   Desk_file_handle   in = 0;
  47.   Desk_clear_picture *temp = NULL;
  48.   unsigned      Desk_creator_length = 1;
  49.   unsigned      Desk_bitmap_size;
  50.   int           word;
  51.   unsigned      entry,
  52.                 colours;
  53.   unsigned      i;
  54.   Desk_palette_entry *palette;
  55.  
  56.   Desk_JumpAuto_Try    {
  57.     in = Desk_File_Open(filename, Desk_file_READ);
  58.  
  59.       while(Desk_File_ReadByte(in) > 0)
  60.         Desk_creator_length++;
  61.     
  62.   Desk_File_Seek(in, 0))
  63.  
  64.   temp = Desk_DeskMem_Malloc(sizeof(Desk_clear_picture) + Desk_creator_length);
  65.  
  66.   temp->creator = (char *)&temp[1];
  67.   temp->palette = NULL;
  68.   temp->bitmap = NULL;
  69.  
  70.   Desk_File_ReadBytes(in, temp->creator, Desk_creator_length);
  71.  
  72.   temp->creatorversion = Desk_File_Read32(in);
  73.   temp->width = Desk_File_Read32(in);
  74.   temp->height = Desk_File_Read32(in);
  75.   temp->bpp = Desk_File_Read32(in);
  76.  
  77.   if(temp->bpp > 8)
  78.     Desk_bitmap_size = 3 * temp->width * temp->height;
  79.   else
  80.   {
  81.     colours = 1 << temp->bpp;
  82.  
  83.    Desk_Mem_Alloc((void **)&temp->palette, sizeof(Desk_palette_entry) * colours);
  84.  
  85.     palette = temp->palette;
  86.     for(entry = 0 ; entry < colours ; entry++)
  87.     {
  88.       word = 0;
  89.       for(i = 8 ; i < 25 ; i += 8)
  90.         word += Desk_File_ReadByte(in) << i;
  91.       palette[entry].value = word;
  92.     }
  93.  
  94.     Desk_bitmap_size = temp->width * temp->height;
  95.   }
  96.  
  97.   Desk_Mem_Alloc((void **)&temp->bitmap, Desk_bitmap_size);
  98.  
  99.   Desk_File_ReadBytes(in, temp->bitmap, Desk_bitmap_size) || Desk_file_lasterror);
  100.  
  101.   Desk_File_Close(in);
  102.   }
  103. Desk_JumpAuto_Catch    {
  104.   Desk_Clear__AbortLoad(in, temp);
  105.   Desk_Error2_ReThrow();
  106.     }
  107. Desk_JumpAuto_EndCatch
  108.  
  109.   return temp;
  110. }
  111.