home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xicon05.zip / winread.c < prev    next >
C/C++ Source or Header  |  1993-05-06  |  8KB  |  293 lines

  1. /* This file is winread.c (part of XIcon)
  2.  *
  3.  * Copyright (C) 1993 by Norman Walsh
  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 of the License, or
  8.  *   (at your option) 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.  
  20. #include <stdio.h>
  21. #include "windata.h"
  22. #include "iconio.h"
  23. #include "iconvars.h"
  24. #include "generic.h"
  25. #include "palette.h"
  26. #include "bitmap.h"
  27.  
  28. win_rgb_info read_win_rgb()
  29. {
  30.   win_rgb_info p;
  31.  
  32.   p.red = read_char();
  33.   p.green = read_char();
  34.   p.blue = read_char();
  35.   p.unused = read_char();
  36.  
  37.   if (dump_input)
  38.     printf("\t\tred: %3d green: %3d blue: %3d\n",
  39.        p.red, p.green, p.blue);
  40.   
  41.   return p;
  42. }
  43.  
  44. WinIconDirEntry read_win_direntry()
  45. {
  46.   WinIconDirEntry e;
  47.  
  48.   e.bWidth = read_char();
  49.   e.bHeight = read_char();
  50.   e.bColorCount = read_char();
  51.   e.bReserved = read_char();
  52.   e.wPlanes = read_shortint();
  53.   e.wBitCount = read_shortint();
  54.   e.dwBytesInRes = read_int();
  55.   e.dwImageOffset = read_int();
  56.  
  57.   if (dump_input)
  58.     {
  59.       printf("Windows dir entry:\n");
  60.       printf("Width        (1) :   %d\n", e.bWidth);
  61.       printf("Height       (1) :   %d\n", e.bHeight);
  62.       printf("ColorCount   (1) :   %d\n", e.bColorCount);
  63.       printf("Reserved     (1) :   %d\n", e.bReserved);
  64.       printf("Planes       (2) :   %d\n", e.wPlanes);
  65.       printf("BitCount     (2) :   %d\n", e.wBitCount);
  66.       printf("BytesInRes   (4) :   %d\n", e.dwBytesInRes);
  67.       printf("Image Offset (4) : 0x%04x\n", e.dwImageOffset);
  68.       printf("\n");
  69.     }
  70.  
  71.   return e;
  72. }
  73.  
  74. WinIconHeader read_win_header()
  75. {
  76.   WinIconHeader h;
  77.  
  78.   h.idReserved = read_shortint();
  79.   h.idType = read_shortint();
  80.   h.idCount = read_shortint();
  81.  
  82.   if (dump_input)
  83.     {
  84.       printf("Windows header:\n");
  85.       printf("idReserved   (2) :   %d\n", h.idReserved);
  86.       printf("idType       (2) :   %d\n", h.idType);
  87.       printf("idCount      (2) :   %d\n", h.idCount);
  88.       printf("\n");
  89.     }
  90.  
  91.   return h;
  92. }
  93.  
  94. WinIconBitInfoHdr read_win_bitinfo()
  95. {
  96.   WinIconBitInfoHdr b;
  97.   int count;
  98.  
  99.   b.biSize = read_int();
  100.   b.biWidth = read_int();
  101.   b.biHeight = read_int();
  102.   b.biPlanes = read_shortint();
  103.   b.biBitCount = read_shortint();
  104.   b.biCompression = read_int();
  105.   b.biSizeImage = read_int();
  106.   b.biXPelsPerMeter = read_int();
  107.   b.biYPelsPerMeter = read_int();
  108.   b.biClrUsed = read_int();
  109.   b.biClrImportant = read_int();
  110.  
  111.   b.biClrUsed = 2 << (b.biBitCount - 1);
  112.  
  113.   if (dump_input)
  114.     {
  115.       printf("Windows bit info hdr:\n");
  116.       printf("biSize       (4) :   %d\n", b.biSize);
  117.       printf("biWidth      (4) :   %d\n", b.biWidth);
  118.       printf("biHeight     (4) :   %d\n", b.biHeight);
  119.       printf("biPlanes     (2) :   %d\n", b.biPlanes);
  120.       printf("biBitCount   (2) :   %d\n", b.biBitCount);
  121.       printf("biCompression(4) :   %d\n", b.biCompression);
  122.       printf("biSizeImage  (4) :   %d\n", b.biSizeImage);
  123.       printf("biXPels/m    (4) :   %d\n", b.biXPelsPerMeter);
  124.       printf("biYPels/m    (4) :   %d\n", b.biYPelsPerMeter);
  125.       printf("biClrUsed    (4) :   %d\n", b.biClrUsed);
  126.       printf("biClrImport. (4) :   %d\n", b.biClrImportant);
  127.       printf("\n");
  128.     }
  129.  
  130.   b.p = (win_rgb_info *) malloc (sizeof(win_rgb_info) * b.biClrUsed);
  131.   for (count = 0; count < b.biClrUsed; count++)
  132.     b.p[count] = read_win_rgb();
  133.  
  134.   return b;
  135. }
  136.  
  137. bitmap_info read_win_clr_map (WinIconBitInfoHdr b)
  138. {
  139.   bitmap_info i;
  140.   BYTE *buf, *RowPtr, *sch, *dch;
  141.   int count, place;
  142.  
  143.   i.width = b.biWidth;
  144.   i.height = b.biHeight / 2;
  145.   i.bits_per_pixel = b.biBitCount;
  146.   i.bytewidth = ((i.width * i.bits_per_pixel) + 7) / 8;
  147.   i.size = i.bytewidth * i.height;
  148.   i.num_colors = 2 << (i.bits_per_pixel - 1);
  149.  
  150.   i.palette = (rgb_info *) malloc (sizeof (rgb_info) * i.num_colors);
  151.   for (count = 0; count < i.num_colors; count++)
  152.     {
  153.       i.palette[count].red = b.p[count].red;
  154.       i.palette[count].green = b.p[count].green;
  155.       i.palette[count].blue = b.p[count].blue;
  156.     }
  157.  
  158.   buf = (BYTE *) malloc(i.size);
  159.   i.map = (BYTE *) malloc(i.size);
  160.  
  161.   for (count = 0; count < i.size; count++)
  162.     buf[count] = read_char();
  163.  
  164.   dch = i.map;
  165.   RowPtr = buf + i.size - i.bytewidth;
  166.   while (RowPtr >= buf)
  167.     {
  168.       sch = RowPtr;
  169.       for (place = 0; place < i.bytewidth; place++)
  170.     {
  171.       *dch = *sch;
  172.       dch++;
  173.       sch++;
  174.     }
  175.  
  176.       RowPtr -= i.bytewidth;
  177.     }
  178.  
  179.   free(buf);
  180.  
  181.   if (dump_input)
  182.     {
  183.       printf("ClrMap:\n");
  184.       printf("width, height: %d, %d\n", i.width, i.height);
  185.       printf("bits, size: %d, %d\n", i.bits_per_pixel, i.size);
  186.       printmap(i);
  187.     }
  188.  
  189.   return i;
  190. }
  191.  
  192. bitmap_info read_win_and_map (WinIconBitInfoHdr b)
  193. {
  194.   bitmap_info i;
  195.   BYTE *buf, *RowPtr, *sch, *dch;
  196.   int count, place;
  197.  
  198.   i.width = b.biWidth;
  199.   i.height = b.biHeight / 2;
  200.   i.bits_per_pixel = 1;
  201.   i.bytewidth = (i.width + 7) / 8;
  202.   i.size = i.bytewidth * i.height;
  203.   i.num_colors = 2;
  204.   i.palette = bw_palette;
  205.  
  206.   buf = (BYTE *) malloc(i.size);
  207.   i.map = (BYTE *) malloc(i.size);
  208.  
  209.   for (count = 0; count < i.size; count++)
  210.     buf[count] = read_char();
  211.  
  212.   dch = i.map;
  213.   RowPtr = buf + i.size - i.bytewidth;
  214.   while (RowPtr >= buf)
  215.     {
  216.       sch = RowPtr;
  217.       for (place = 0; place < i.bytewidth; place++)
  218.     {
  219.       *dch = *sch;
  220.       dch++;
  221.       sch++;
  222.     }
  223.  
  224.       RowPtr -= i.bytewidth;
  225.     }
  226.  
  227.   free(buf);
  228.  
  229.   if (dump_input)
  230.     {
  231.       printf("AndMap:\n");
  232.       printf("width, height: %d, %d\n", i.width, i.height);
  233.       printf("bits, size: %d, %d\n", i.bits_per_pixel, i.size);
  234.       printmap(i);
  235.     }
  236.  
  237.   return i;
  238. }
  239.  
  240. bitmap_info win_empty_bw_map(WinIconBitInfoHdr b)
  241. {
  242.   bitmap_info m;
  243.  
  244.   m = init_empty_map(b.biWidth, b.biHeight / 2, 1);
  245.   m.palette = bw_palette;
  246.  
  247.   return m;
  248. }
  249.  
  250. void read_win_file()
  251. {
  252.   WinIconHeader Win_header;
  253.   WinIconDirEntry *Win_direntries;
  254.   WinIconBitInfoHdr *Win_bitinfo;
  255.   int count, row, col;
  256.   BYTE ch;
  257.  
  258.   fseek (input, 0, SEEK_SET);
  259.  
  260.   Win_header = read_win_header();
  261.  
  262.   Win_direntries = (WinIconDirEntry *) malloc(sizeof(WinIconDirEntry) *
  263.                           Win_header.idCount);
  264.  
  265.   Win_bitinfo = (WinIconBitInfoHdr *) malloc(sizeof(WinIconBitInfoHdr) *
  266.                          Win_header.idCount);
  267.  
  268.   for (count = 0; count < Win_header.idCount; count++)
  269.     Win_direntries[count] = read_win_direntry();
  270.  
  271.   generic_count = Win_header.idCount;
  272.   generic = (generic_info *) malloc(sizeof(generic_info) * 
  273.                     Win_header.idCount);
  274.  
  275.   for (count = 0; count < Win_header.idCount; count++)
  276.     {
  277.       fseek (input, Win_direntries[count].dwImageOffset, SEEK_SET);
  278.       Win_bitinfo[count] = read_win_bitinfo();
  279.       generic[count].clr_map = read_win_clr_map(Win_bitinfo[count]);
  280.       generic[count].and_map = read_win_and_map(Win_bitinfo[count]);
  281.       generic[count].xor_map = win_empty_bw_map(Win_bitinfo[count]);
  282.  
  283.       for (row = 1; row <= generic[count].and_map.height; row++)
  284.     for (col = 1; col <= generic[count].clr_map.width; col++)
  285.       {
  286.         ch = get_bit (generic[count].and_map, row, col);
  287.         if (ch)
  288.           set_bit (generic[count].clr_map, row, col, (BYTE) 0xFF);
  289.       }
  290.     }
  291. }
  292.  
  293.