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

  1. /* This file is macread.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 "macdata.h"
  22. #include "iconio.h"
  23. #include "iconvars.h"
  24. #include "generic.h"
  25. #include "palette.h"
  26. #include "bitmap.h"
  27.  
  28. int cvtICNp;
  29. int cvticsp;
  30. int cvticl4;
  31. int cvtics4;
  32. int MacPalette;
  33.  
  34. int Mac_typeCount;
  35. RsrcTypeStruct *Mac_types;
  36. RsrcRefStruct  *Mac_refs;
  37. MacDataStruct  *Mac_data;
  38.  
  39. RsrcHdrStruct read_mac_header()
  40. {
  41.   RsrcHdrStruct Header;
  42.   int count;
  43.  
  44.   Header.DataOffset = read_macint();
  45.   Header.MapOffset  = read_macint();
  46.   Header.DataLen    = read_macint();
  47.   Header.MapLen     = read_macint();
  48.  
  49.   for (count = 0; count < 96; count++)
  50.     Header.OSReserved[count] = read_macchar();
  51.  
  52.   for (count = 0; count < 128; count++)
  53.     Header.AppReserved[count] = read_macchar();
  54.  
  55.   return Header;
  56. }
  57.  
  58. RsrcMapStruct read_mac_map()
  59. {
  60.   RsrcMapStruct Map;
  61.   int count;
  62.  
  63.   for (count = 0; count < 4; count++)
  64.     Map.MapCopy[count] = read_macint();
  65.  
  66.   Map.NextMap = read_macint();
  67.   Map.FileRef = read_macshortint();
  68.   Map.FileAttr = read_macshortint();
  69.   Map.TypeOffset = read_macshortint();
  70.   Map.NameOffset = read_macshortint();
  71.  
  72.   return Map;
  73. }
  74.  
  75. RsrcTypeStruct read_mac_type()
  76. {
  77.   RsrcTypeStruct Type;
  78.   int count;
  79.  
  80.   for (count = 0; count < 4; count++)
  81.     Type.Name[count] = read_macchar();
  82.  
  83.   Type.Count = read_macshortint();
  84.   Type.RefOffset = read_macshortint();
  85.  
  86.   return Type;
  87. }
  88.  
  89. RsrcRefStruct read_mac_ref()
  90. {
  91.   RsrcRefStruct Ref;
  92.   int count;
  93.  
  94.   Ref.Ident = read_macshortint();
  95.   Ref.NameOffset = read_macshortint();
  96.   Ref.Attr = read_macchar();
  97.   Ref.DataOffset = read_macint3();
  98.  
  99.   return Ref;
  100. }
  101.  
  102. enum MacResourceTypes MacResType(RsrcTypeStruct t)
  103. {
  104.   if (strncmp (t.Name, "ICN#", 4) == 0)
  105.     return Mac_ICNp;
  106.  
  107.   if (strncmp (t.Name, "ics#", 4) == 0)
  108.     return Mac_icsp;
  109.  
  110.   if (strncmp (t.Name, "icl4", 4) == 0)
  111.     return Mac_icl4;
  112.  
  113.   if (strncmp (t.Name, "ics4", 4) == 0)
  114.     return Mac_ics4;
  115.  
  116.   return Mac_xxxx;
  117. }
  118.  
  119. MacDataStruct *find_mac_data(int nth, enum MacResourceTypes type)
  120. {
  121.   int count, place;
  122.  
  123.   if (nth == 0)
  124.     return NULL;
  125.  
  126.   for (count = 0; count < Mac_typeCount; count++)
  127.     {
  128.       if (MacResType(Mac_types[count]) == type)
  129.     nth--;
  130.       
  131.       if (nth == 0)
  132.     return &Mac_data[count];
  133.     }
  134.  
  135.   return NULL;
  136. }
  137.  
  138. bitmap_info mac_init_generic(int width, 
  139.                  int height, 
  140.                  int bits, 
  141.                  BYTE *dataptr)
  142. {
  143.   bitmap_info m;
  144.  
  145.   m.width = width;
  146.   m.height = height;
  147.   m.bits_per_pixel = bits;
  148.   m.num_colors = 2 << (bits - 1);
  149.  
  150.   m.bytewidth = ((m.width*m.bits_per_pixel) + 7) / 8;
  151.   m.size = (m.bytewidth * m.height);
  152.  
  153.   if (bits == 1)
  154.     m.palette = bw_palette;
  155.   else
  156.     if (MacPalette)
  157.       m.palette = mac_palette;
  158.     else
  159.       m.palette = pc_palette;
  160.  
  161.   m.map = dataptr;
  162.  
  163.   return m;
  164. }
  165.  
  166. bitmap_info mac_empty_map(int width, int height, int bits)
  167. {
  168.   bitmap_info m;
  169.   BYTE *ch;
  170.   int count;
  171.  
  172.   m.width = width;
  173.   m.height = height;
  174.   m.bits_per_pixel = bits;
  175.   m.num_colors = 2 << (bits - 1);
  176.  
  177.   m.bytewidth = ((m.width*m.bits_per_pixel) + 7) / 8;
  178.   m.size = (m.bytewidth * m.height);
  179.  
  180.   if (bits == 1)
  181.     m.palette = bw_palette;
  182.   else
  183.     if (MacPalette)
  184.       m.palette = mac_palette;
  185.     else
  186.       m.palette = pc_palette;
  187.   
  188.   m.map = (BYTE *) malloc(m.size);
  189.   
  190.   for (count = 0, ch = m.map;
  191.        count < m.size;
  192.        count++, ch++)
  193.     *ch = 0;
  194.  
  195.   return m;
  196. }
  197.  
  198. void dump_mac_header(RsrcHdrStruct h, RsrcMapStruct m)
  199. {
  200.   printf("Mac header:\t\tMac map:\n");
  201.   printf("DataOffset (4): 0x%04x\tNextMap    (4) : 0x%04x\n", 
  202.      h.DataOffset, m.NextMap);
  203.   printf("MapOffset  (4): 0x%04x\tFileRef    (2) :   %4d\n", 
  204.      h.MapOffset, m.FileRef);
  205.   printf("DataLen    (4):   %4d\tFileAttr   (2) :   %4d\n", 
  206.      h.DataLen, m.FileAttr);
  207.   printf("MapLen     (4):   %4d\tTypeOffset (2) :   %4d\n",
  208.      h.MapLen, m.TypeOffset);
  209.   printf("\t\t\tNameOffset (2) :   %4d\n", 
  210.      m.NameOffset);
  211.   printf("\n");
  212. }
  213.  
  214. void dump_mac_info(RsrcTypeStruct t, RsrcRefStruct r)
  215. {
  216.   printf("Mac type:\t\tMac ref:\n");
  217.   printf("Name      (4) :   %c%c%c%c\tIdent      (2) : 0x%04x\n",
  218.      t.Name[0], t.Name[1], t.Name[2], t.Name[3], r.Ident);
  219.   printf("Count     (2) :   %4d\tNameOffset (2) : 0x%04x\n",
  220.      t.Count, r.NameOffset);
  221.   printf("RefOffset (2) : 0x%04x\tAttr       (1) : %d\n",
  222.      t.RefOffset, r.Attr);
  223.   printf("\t\t\tDataOffset (2) : 0x%04x\n", 
  224.      r.DataOffset);
  225.   printf("\n");
  226. }
  227.  
  228. void dump_maps(generic_info g)
  229. {
  230.   printf("Dump of 'and' map:\n");
  231.   printmap(g.and_map);
  232.   printf("\nDump of 'xor' map:\n");
  233.   printmap(g.xor_map);
  234.   printf("\nDump of 'clr' map:\n");
  235.   printmap(g.clr_map);
  236.   printf("\n");
  237. }
  238.  
  239. void fix_transparency (bitmap_info and_map, bitmap_info clr_map)
  240. {
  241.   int row, col;
  242.  
  243.   for (row = 1; row <= and_map.height; row++)
  244.     for (col = 1; col <= and_map.width; col++)
  245.       if (get_bit(and_map, row, col))
  246.     set_bit (clr_map, row, col, 0xFF);
  247. }
  248.  
  249. void fix_small_map (bitmap_info and_map, bitmap_info clr_map)
  250. {
  251.   int row, col;
  252.   BYTE ab, cb;
  253.  
  254.   for (row = 1; row <= and_map.height; row++)
  255.     for (col = 1; col <= and_map.width; col++)
  256.       {
  257.     ab = get_bit(and_map, row, col);
  258.     cb = get_bit(clr_map, row, col);
  259.     set_bit(and_map, row, col, ab | cb);
  260.     set_bit(clr_map, row, col, 0);
  261.       }
  262. }
  263.  
  264. void read_mac_file()
  265. {
  266.   RsrcHdrStruct Mac_header;
  267.   RsrcMapStruct Mac_map;
  268.   int count, place;
  269.   int ICNpcnt, icspcnt, icl4cnt, ics4cnt;
  270.   int ICNpcur, icspcur, icl4cur, ics4cur;
  271.   MacDataStruct *dataptr, *maskptr;
  272.   BYTE XlateArr[16], ch;
  273.   int row, col;
  274.  
  275.   XlateArr[0] = 0;
  276.   XlateArr[1] = 14;
  277.   XlateArr[2] = 12;
  278.   XlateArr[3] = 12;
  279.   XlateArr[4] = 13;
  280.   XlateArr[5] = 1;
  281.   XlateArr[6] = 9;
  282.   XlateArr[7] = 3;
  283.   XlateArr[8] = 10;
  284.   XlateArr[9] = 2;
  285.   XlateArr[10] = 4;
  286.   XlateArr[11] = 6;
  287.   XlateArr[12] = 8;
  288.   XlateArr[13] = 8;
  289.   XlateArr[14] = 7;
  290.   XlateArr[15] = 15;
  291.  
  292.   fseek(input, 0, SEEK_SET);
  293.  
  294.   Mac_header = read_mac_header();
  295.  
  296.   fseek(input, Mac_header.MapOffset, SEEK_SET);
  297.  
  298.   Mac_map = read_mac_map();
  299.  
  300.   if (dump_input)
  301.     dump_mac_header(Mac_header, Mac_map);
  302.  
  303.   fseek(input, Mac_header.MapOffset + Mac_map.TypeOffset, SEEK_SET);
  304.  
  305.   Mac_typeCount = read_macshortint()+1;
  306.   Mac_types = (RsrcTypeStruct *) malloc (Mac_typeCount*sizeof(RsrcTypeStruct));
  307.  
  308.   for (count = 0; count < Mac_typeCount; count++)
  309.     Mac_types[count] = read_mac_type();
  310.  
  311.   Mac_refs = (RsrcRefStruct *) malloc (Mac_typeCount*sizeof(RsrcRefStruct));
  312.  
  313.   for (count = 0; count < Mac_typeCount; count++)
  314.     {
  315.       fseek(input,
  316.             Mac_header.MapOffset
  317.             + Mac_map.TypeOffset
  318.             + Mac_types[count].RefOffset,
  319.             SEEK_SET);
  320.       Mac_refs[count] = read_mac_ref();
  321.     }
  322.  
  323.   if (dump_input)
  324.     for (count = 0; count < Mac_typeCount; count++)
  325.       dump_mac_info(Mac_types[count], Mac_refs[count]);
  326.  
  327.   ICNpcnt = icspcnt = icl4cnt = ics4cnt = 0;
  328.   ICNpcur = icspcur = icl4cur = ics4cur = 0;
  329.  
  330.   for (count = 0; count < Mac_typeCount; count++)
  331.     switch (MacResType (Mac_types[count]))
  332.       {
  333.       case Mac_ICNp:
  334.     if (cvtICNp)
  335.       ICNpcnt++;
  336.     break;
  337.       case Mac_icsp:
  338.     if (cvticsp)
  339.       icspcnt++;
  340.     break;
  341.       case Mac_icl4:
  342.     if (cvticl4)
  343.       icl4cnt++;
  344.     break;
  345.       case Mac_ics4:
  346.     if (cvtics4)
  347.       ics4cnt++;
  348.     break;
  349.       default:
  350.     break;
  351.       }
  352.  
  353.   generic_count = ICNpcnt + icspcnt + icl4cnt + ics4cnt;
  354.  
  355.   Mac_data = (MacDataStruct *) malloc (sizeof(MacDataStruct) * Mac_typeCount);
  356.   dataptr  = Mac_data;
  357.  
  358.   for (count = 0; count < Mac_typeCount; count++)
  359.     {
  360.       if (MacResType (Mac_types[count]) == Mac_ICNp
  361.       || MacResType (Mac_types[count]) == Mac_icsp
  362.       || MacResType (Mac_types[count]) == Mac_icl4
  363.       || MacResType (Mac_types[count]) == Mac_ics4)
  364.     {
  365.       fseek (input, 
  366.          Mac_header.DataOffset+Mac_refs[count].DataOffset, 
  367.          SEEK_SET);
  368.       
  369.       dataptr->size = read_macint();
  370.       dataptr->map  = (BYTE *) malloc (dataptr->size);
  371.       
  372.       fread (dataptr->map, 1, dataptr->size, input);
  373.  
  374.       if (MacResType (Mac_types[count]) == Mac_ICNp)
  375.         {
  376.           for (place = 128; place < 256; place++)
  377.         dataptr->map[place] ^= 0xFF;
  378.         }
  379.       else
  380.         if (MacResType (Mac_types[count]) == Mac_icsp)
  381.           {
  382.         for (place = 32; place < 64; place++)
  383.           dataptr->map[place] ^= 0xFF;
  384.           }
  385.     }
  386.       dataptr++;
  387.     }
  388.  
  389.   generic = (generic_info *) malloc (sizeof(generic_info) * generic_count);
  390.  
  391.   for (count = 0; count < generic_count; /* nop */)
  392.     {
  393.       init_generic(count);
  394.  
  395.       if (icl4cur < icl4cnt)
  396.     {
  397.       dataptr = find_mac_data (++icl4cur, Mac_icl4);
  398.       maskptr = find_mac_data (icl4cur, Mac_ICNp);
  399.  
  400.       generic[count].and_map = mac_init_generic(32, 32, 1, maskptr->map + 128);
  401.       generic[count].xor_map = mac_empty_map(32, 32, 1);
  402.       generic[count].clr_map = mac_init_generic(32, 32, 4, dataptr->map);
  403.  
  404.       if (dump_input)
  405.         dump_maps(generic[count]);
  406.  
  407.       /* Translate the colors in the map */
  408.       if (!MacPalette)
  409.         for (row = 1; row <= generic[count].clr_map.height; row++)
  410.           {
  411.         for (col = 1; col <= generic[count].clr_map.width; col++)
  412.           {
  413.             ch = get_bit(generic[count].clr_map, row, col);
  414.             ch = XlateArr[ch];
  415.             set_bit(generic[count].clr_map, row, col, ch);
  416.           }
  417.           }
  418.  
  419.       fix_transparency (generic[count].and_map, generic[count].clr_map);
  420.       
  421.       count++;
  422.     }
  423.  
  424.       if (ICNpcur < ICNpcnt)
  425.     {
  426.       dataptr = find_mac_data (++ICNpcur, Mac_ICNp);
  427.       maskptr = dataptr;
  428.  
  429.       generic[count].and_map = mac_init_generic(32, 32, 1, maskptr->map + 128);
  430.       generic[count].xor_map = mac_empty_map(32, 32, 1);
  431.       generic[count].clr_map = mac_init_generic(32, 32, 1, dataptr->map);
  432.  
  433.       if (dump_input)
  434.         dump_maps(generic[count]);
  435.  
  436.       for (row = 1; row <= generic[count].clr_map.height; row++)
  437.         for (col = 1; col <= generic[count].clr_map.width; col++)
  438.           if (get_bit (generic[count].clr_map, row, col))
  439.         set_bit (generic[count].clr_map, row, col, 0);
  440.           else
  441.         set_bit (generic[count].clr_map, row, col, 1);
  442.  
  443.       fix_transparency (generic[count].and_map, generic[count].clr_map);
  444.       
  445.       count++;
  446.     }
  447.  
  448.       if (ics4cur < ics4cnt)
  449.     {
  450.       dataptr = find_mac_data (++ics4cur, Mac_ics4);
  451.       maskptr = find_mac_data (ics4cur, Mac_icsp);
  452.  
  453.       generic[count].and_map = mac_init_generic(16, 16, 1, maskptr->map + 32);
  454.       generic[count].xor_map = mac_empty_map(16, 16, 1);
  455.       generic[count].clr_map = mac_init_generic(16, 16, 4, dataptr->map);
  456.  
  457.       if (dump_input)
  458.         dump_maps(generic[count]);
  459.  
  460.       /* Translate the colors in the map */
  461.       if (!MacPalette)
  462.         for (row = 1; row <= generic[count].clr_map.height; row++)
  463.           for (col = 1; col <= generic[count].clr_map.width; col++)
  464.         {
  465.           ch = get_bit(generic[count].clr_map, row, col);
  466.           ch = XlateArr[ch];
  467.           set_bit(generic[count].clr_map, row, col, ch);
  468.         }
  469.  
  470.       fix_transparency (generic[count].and_map, generic[count].clr_map);
  471.       
  472.       count++;
  473.     }
  474.       
  475.       if (icspcur < icspcnt)
  476.     {
  477.       dataptr = find_mac_data (++icspcur, Mac_icsp);
  478.       maskptr = dataptr;
  479.  
  480.       generic[count].and_map = mac_init_generic(16, 16, 1, maskptr->map + 32);
  481.       generic[count].xor_map = mac_empty_map(16, 16, 1);
  482.       generic[count].clr_map = mac_init_generic(16, 16, 1, dataptr->map);
  483.  
  484.       if (dump_input)
  485.         dump_maps(generic[count]);
  486.  
  487.       for (row = 1; row <= generic[count].clr_map.height; row++)
  488.         for (col = 1; col <= generic[count].clr_map.width; col++)
  489.           if (get_bit (generic[count].clr_map, row, col))
  490.         set_bit (generic[count].clr_map, row, col, 0);
  491.           else
  492.         set_bit (generic[count].clr_map, row, col, 1);
  493.  
  494.       fix_transparency (generic[count].and_map, generic[count].clr_map);
  495.       fix_small_map (generic[count].and_map, generic[count].clr_map);
  496.       
  497.       count++;
  498.     }
  499.     }
  500. }
  501.