home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fontutils-0.6 / imgrotate / img-input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  2.7 KB  |  116 lines

  1. /* input-img.c: read Interleaf .img files.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "file-input.h"
  22. #include "xstat.h"
  23.  
  24. #include "img-input.h"
  25.  
  26.  
  27. /* Where the input comes from.  */
  28. static FILE *img_input_file;
  29. static string img_input_filename;
  30.  
  31. /* Macros for convenient input.  The routines are defined in kbase.  */
  32. #define IMG_MATCH_BYTE(v)  match_byte (v, img_input_file, img_input_filename)
  33. #define IMG_GET_TWO()  get_two (img_input_file, img_input_filename)
  34. #define IMG_GET_FOUR()  get_four (img_input_file, img_input_filename)
  35.  
  36.  
  37.  
  38. /* Only one file can be open at a time.  We do no path searching.  If
  39.    FILENAME can't be opened, we quit.  */
  40.  
  41. FILE *
  42. open_img_input_file (string filename)
  43. {
  44.   assert (img_input_file == NULL);
  45.   
  46.   img_input_file = xfopen (filename, "r");
  47.   img_input_filename = filename;
  48.   
  49.   return img_input_file;
  50. }
  51.  
  52.  
  53. /* Close the input file.  If it hasn't been opened, we quit.  */
  54.  
  55. void
  56. close_img_input_file ()
  57. {
  58.   assert (img_input_file != NULL);
  59.   
  60.   xfclose (img_input_file, img_input_filename);
  61.   img_input_file = NULL;
  62. }
  63.  
  64.  
  65.  
  66. /* Read the header information.  HEADER_SIZE is the total size in bytes
  67.    before the data, not the size of the structure.  */
  68.  
  69. #define HEADER_SIZE 22
  70.  
  71. img_header_type
  72. get_img_header ()
  73. {
  74.   img_header_type h;
  75.   
  76.   /* The ``magic number''.  */
  77.   IMG_MATCH_BYTE (0211);
  78.   IMG_MATCH_BYTE ('O');
  79.   IMG_MATCH_BYTE ('P');
  80.   IMG_MATCH_BYTE ('S');
  81.   
  82.   /* The version number.  */
  83.   IMG_MATCH_BYTE (0);
  84.   IMG_MATCH_BYTE (2);
  85.   
  86.   h.hres = IMG_GET_TWO ();
  87.   h.vres = IMG_GET_TWO ();
  88.   h.flags = IMG_GET_FOUR ();
  89.   h.width = IMG_GET_TWO ();
  90.   h.height = IMG_GET_TWO ();
  91.   h.d = IMG_GET_TWO ();
  92.   h.format = IMG_GET_TWO ();
  93.  
  94.   return h;
  95. }
  96.  
  97.  
  98.  
  99. /* Read the data.  */
  100.  
  101. one_byte *
  102. read_data ()
  103. {
  104.   one_byte *m;
  105.   unsigned data_size;
  106.   struct stat stats = xstat (img_input_filename);
  107.   
  108.   data_size = stats.st_size - HEADER_SIZE;
  109.   
  110.   m = xmalloc (data_size);
  111.   if (fread (m, data_size, 1, img_input_file) != 1)
  112.     FATAL1 ("read_data: fread of %u bytes failed", data_size);
  113.   
  114.   return m;
  115. }
  116.