home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / graphic / xicon / src / os2read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-04  |  13.7 KB  |  497 lines

  1. /* This file is os2read.c (part of XIcon)
  2.  *
  3.  * Copyright (C) 1993,94 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 "icondata.h"
  22. #include "iconvars.h"
  23. #include "os2data.h"
  24. #include "iconio.h"
  25.  
  26. #define MAX_ICONS 100
  27. generic_info *g;
  28.  
  29. int two_to_the (int power) 
  30. {
  31.   int x = 1;
  32.   while (power > 0) {
  33.     x <<= 1;
  34.     power--;
  35.   }
  36.  
  37.   return x;
  38. }
  39.  
  40. os2_12_bitmap_info read_os2_12_bitmap_info() 
  41. {
  42.   os2_12_bitmap_info i;
  43.   int palette_entries;
  44.   int count;
  45.  
  46.   i.flag = read_shortint();
  47.   i.cbsize = read_int();
  48.   i.hotx = read_shortint();
  49.   i.hoty = read_shortint();
  50.   i.bitmap_offset = read_int();
  51.   i.bitmap_size = read_int();
  52.   i.width = read_shortint();
  53.   i.height = read_shortint();
  54.   i.planes = read_shortint();
  55.   i.bits_per_pixel = read_shortint();
  56.  
  57.   palette_entries = two_to_the(i.bits_per_pixel) * i.planes;
  58.   i.p = (os2_12_rgb_info *) malloc(palette_entries * sizeof(os2_12_rgb_info));
  59.  
  60.   for (count = 0; count < palette_entries; count++) {
  61.     i.p[count].blue = read_char();
  62.     i.p[count].green = read_char();
  63.     i.p[count].red = read_char();
  64.   }
  65.  
  66.   return i;
  67. }
  68.  
  69. os2_20_bitmap_info read_os2_20_bitmap_info() 
  70. {
  71.   os2_20_bitmap_info i;
  72.   int palette_entries;
  73.   int count;
  74.  
  75.   i.flag = read_shortint();
  76.   i.cbsize = read_int();
  77.   i.hotx = read_shortint();
  78.   i.hoty = read_shortint();
  79.   i.bitmap_offset = read_int();
  80.   i.length = read_int();
  81.   i.width = read_int();
  82.   i.height = read_int();
  83.   i.planes = read_shortint();
  84.   i.bits_per_pixel = read_shortint();
  85.   i.compression = read_int();
  86.   i.bitmap_size = read_int();
  87.   i.devXres = read_int();
  88.   i.devYres = read_int();
  89.   i.clrUsed = read_int();
  90.   i.clrImportant = read_int();
  91.   i.units = read_shortint();
  92.   i.reserved = read_shortint();
  93.   i.recording = read_shortint();
  94.   i.rendering = read_shortint();
  95.   i.size1 = read_int();
  96.   i.size2 = read_int();
  97.   i.color_encoding = read_int();
  98.   i.app_identifier = read_int();
  99.  
  100.   palette_entries = two_to_the(i.bits_per_pixel) * i.planes;
  101.   i.p = (os2_20_rgb_info *) malloc(palette_entries * sizeof(os2_20_rgb_info));
  102.  
  103.   for (count = 0; count < palette_entries; count++) {
  104.     i.p[count].blue = read_char();
  105.     i.p[count].green = read_char();
  106.     i.p[count].red = read_char();
  107.     i.p[count].unused = read_char();
  108.   }
  109.  
  110.   return i;
  111. }
  112.  
  113. /*
  114. void dump_os2_icon_header (os2_icon_hdr header)
  115. {
  116.   printf ("Header:\n");
  117.   printf ("\tflag  : %d\n", header.flag);
  118.   printf ("\tcbsize: %d\n", header.cbsize);
  119.   printf ("\tnext  : %d\n", header.next);
  120.   printf ("\tdisplay_width : %d\n", header.display_width);
  121.   printf ("\tdisplay_height: %d\n", header.display_height);
  122. }
  123.  
  124. void dump_os2_12_palette (int num_entries, os2_12_rgb_info *p)
  125. {
  126.   return;
  127. }
  128.  
  129. void dump_os2_12_bitmap_info (os2_12_bitmap_info i)
  130. {
  131.   printf ("Info 12:\n");
  132.   printf ("\tflag          : %d\n", i.flag);
  133.   printf ("\tcbsize        : %d\n", i.cbsize);
  134.   printf ("\thotx, hoty    : %d, %d\n", i.hotx, i.hoty);
  135.   printf ("\tbitmap_offset : %x\n", i.bitmap_offset);
  136.   printf ("\tbitmap_size   : %d\n", i.bitmap_size);
  137.   printf ("\twidth         : %d\n", i.width);
  138.   printf ("\theight        : %d\n", i.height);
  139.   printf ("\tplanes        : %d\n", i.planes);
  140.   printf ("\tbits_per_pixel: %d\n", i.bits_per_pixel);
  141.  
  142.   dump_os2_12_palette (i.planes*two_to_the(i.bits_per_pixel), i.p);
  143. }
  144.  
  145. void dump_os2_12_icon_info(os2_12_icon_info i)
  146. {
  147.   printf("OS/2 1.2 Icon:\n");
  148.  
  149.   dump_os2_icon_header(i.header);
  150.  
  151.   printf ("OS/2 1.2 andxor_map ");
  152.   dump_os2_12_bitmap_info (i.andxor_map);
  153.  
  154.   printf ("OS/2 1.2 clr_map ");
  155.   dump_os2_12_bitmap_info (i.clr_map);
  156.  
  157. }
  158.  
  159. void dump_os2_20_bitmap_info (os2_20_bitmap_info i)
  160. {
  161.   printf ("Info 20:\n");
  162.   printf ("\tflag        : %d\n", i.flag);
  163.   printf ("\tcbsize        : %d\n", i.cbsize);
  164.   printf ("\thotx        : %d\n", i.hotx);
  165.   printf ("\thoty        : %d\n", i.hoty);
  166.   printf ("\tbitmap_offset    : %x\n", i.bitmap_offset);
  167.   printf ("\tlength        : %d\n", i.length);
  168.   printf ("\twidth        : %d\n", i.width);
  169.   printf ("\theight        : %d\n", i.height);
  170.   printf ("\tplanes        : %d\n", i.planes);
  171.   printf ("\tbits_per_pixel    : %d\n", i.bits_per_pixel);
  172.   printf ("\tcompression    : %d\n", i.compression);
  173.   printf ("\tbitmap_size    : %d\n", i.bitmap_size);
  174.   printf ("\tdevXres        : %d\n", i.devXres);
  175.   printf ("\tdevYres        : %d\n", i.devYres);
  176.   printf ("\tclrUsed        : %d\n", i.clrUsed);
  177.   printf ("\tclrImportant    : %d\n", i.clrImportant);
  178.   printf ("\tunits        : %d\n", i.units);
  179.   printf ("\treserved        : %d\n", i.reserved);
  180.   printf ("\trecording        : %d\n", i.recording);
  181.   printf ("\trendering        : %d\n", i.rendering);
  182.   printf ("\tsize1        : %d\n", i.size1);
  183.   printf ("\tsize2        : %d\n", i.size2);
  184.   printf ("\tcolor_encoding    : %d\n", i.color_encoding);
  185.   printf ("\tapp_identifier    : %d\n", i.app_identifier);
  186. }
  187. */
  188.  
  189. os2_arr_hdr read_os2_arr_hdr ()
  190. {
  191.   os2_arr_hdr bm_array_hdr;
  192.  
  193.   bm_array_hdr.flag = read_shortint();
  194.   bm_array_hdr.cbsize = read_int();
  195.   bm_array_hdr.next = read_int();
  196.   bm_array_hdr.display_width = read_shortint();
  197.   bm_array_hdr.display_height = read_shortint();
  198.   
  199.   return bm_array_hdr;
  200. }
  201.  
  202. void flip_a_map (bitmap_info *map)
  203. {
  204.   char *buf, *sch, *dch, *RowPtr;
  205.   int place;
  206.   
  207.   buf = (char *) malloc (map->size);
  208.  
  209.   dch = buf;
  210.   RowPtr = map->map + map->size - map->bytewidth;
  211.   while (RowPtr >= (char *)map->map)
  212.     {
  213.       sch = RowPtr;
  214.       for (place = 0; place < map->bytewidth; place++)
  215.     {
  216.       *dch = *sch;
  217.       dch++;
  218.       sch++;
  219.     }
  220.  
  221.       RowPtr -= map->bytewidth;
  222.     }
  223.  
  224.   RowPtr = map->map;
  225.   map->map = buf;
  226.   free (RowPtr);
  227. }
  228.  
  229. void flip_generic_map () 
  230. {
  231.   flip_a_map (&g->and_map);
  232. /*
  233.   printf ("And map (flipped):\n");
  234.   dump_bitmap(g->and_map);
  235. */
  236.  
  237.   flip_a_map (&g->xor_map);
  238. /*
  239.   printf ("Xor map (flipped):\n");
  240.   dump_bitmap(g->xor_map);
  241. */
  242.  
  243.   flip_a_map (&g->clr_map);
  244. /*
  245.   printf ("Clr map (flipped):\n");
  246.   dump_bitmap(g->clr_map);
  247. */
  248. }
  249.  
  250. void read_generic_bitmap (bitmap_info map)
  251. {
  252.   int row, col, count, padbytes;
  253.   char *ch;
  254.  
  255.   count = map.bytewidth;
  256.   padbytes = 0;
  257.   while (count % 4 != 0) {
  258.     padbytes++;
  259.     count++;
  260.   }
  261.  
  262.   ch = map.map;
  263.   for (row = 0; row < map.height; row++) {
  264.     for (col = 0; col < map.bytewidth; col++) {
  265.       *ch = read_char();
  266.       ch++;
  267.     }
  268.  
  269.     for (count = 0; count < padbytes; count++) {
  270.       read_char();
  271.     }
  272.   }
  273. }
  274.  
  275. void add_os2_12_generic_bitmap (os2_12_bitmap_info andxor_bitmap12, 
  276.                 os2_12_bitmap_info clr_bitmap12)
  277. {
  278.   int count;
  279.   char *ch;
  280.   rgb_info *rgb;
  281.   os2_12_rgb_info *rgb12;
  282.   int padbytes, padcount;
  283.  
  284.   g->and_map.width = andxor_bitmap12.width;
  285.   g->and_map.height = andxor_bitmap12.height / 2;
  286.   g->and_map.bits_per_pixel = andxor_bitmap12.bits_per_pixel;
  287.   g->and_map.bytewidth = ((andxor_bitmap12.width * andxor_bitmap12.bits_per_pixel)+7) / 8;
  288.   g->and_map.size = g->and_map.bytewidth * g->and_map.height;
  289.  
  290.   g->and_map.num_colors
  291.     = two_to_the(andxor_bitmap12.bits_per_pixel) * andxor_bitmap12.planes;
  292.  
  293.   g->and_map.palette = (rgb_info *) malloc(g->and_map.num_colors * 
  294.                          sizeof(rgb_info));
  295.   rgb12 = andxor_bitmap12.p;
  296.   rgb = g->and_map.palette;
  297.   for (count = 0; count < g->and_map.num_colors; count++) {
  298.     rgb->blue = rgb12->blue;
  299.     rgb->red = rgb12->red;
  300.     rgb->green = rgb12->green;
  301.     rgb++;
  302.     rgb12++;
  303.   }
  304.  
  305.   g->and_map.map = NULL;
  306.   g->xor_map = g->and_map;
  307.  
  308.   g->clr_map.width = clr_bitmap12.width;
  309.   g->clr_map.height = clr_bitmap12.height;
  310.   g->clr_map.bits_per_pixel = clr_bitmap12.bits_per_pixel;
  311.  
  312.   g->clr_map.bytewidth = ((clr_bitmap12.width * clr_bitmap12.bits_per_pixel)+7) / 8;
  313.   g->clr_map.size = g->clr_map.bytewidth * g->clr_map.height;
  314.  
  315.   g->clr_map.num_colors
  316.     = two_to_the(clr_bitmap12.bits_per_pixel) * clr_bitmap12.planes;
  317.  
  318.   g->clr_map.palette = (rgb_info *) malloc(g->clr_map.num_colors * 
  319.                          sizeof(rgb_info));
  320.   rgb12 = clr_bitmap12.p;
  321.   rgb = g->clr_map.palette;
  322.   for (count = 0; count < g->clr_map.num_colors; count++) {
  323.     rgb->blue = rgb12->blue;
  324.     rgb->red = rgb12->red;
  325.     rgb->green = rgb12->green;
  326.     rgb++;
  327.     rgb12++;
  328.   }
  329.  
  330.   g->clr_map.map = NULL;
  331.   
  332.   fseek(input, andxor_bitmap12.bitmap_offset, SEEK_SET);
  333.   g->and_map.map = (char *) malloc(g->and_map.size);
  334.   read_generic_bitmap(g->and_map);
  335.  
  336.   g->xor_map.map = (char *) malloc(g->xor_map.size);
  337.   read_generic_bitmap(g->xor_map);
  338.  
  339.   fseek(input, clr_bitmap12.bitmap_offset, SEEK_SET);
  340.   g->clr_map.map = (char *) malloc(g->clr_map.size);
  341.   read_generic_bitmap(g->clr_map);
  342.  
  343.   flip_generic_map();
  344.   g++;
  345. }
  346.  
  347. void add_os2_20_generic_bitmap (os2_20_bitmap_info andxor_bitmap20, 
  348.                 os2_20_bitmap_info clr_bitmap20)
  349. {
  350.   int count;
  351.   char *ch;
  352.   rgb_info *rgb;
  353.   os2_20_rgb_info *rgb20;
  354.   int bmlength;
  355.  
  356.   g->and_map.width = andxor_bitmap20.width;
  357.   g->and_map.height = andxor_bitmap20.height / 2;
  358.   g->and_map.bits_per_pixel = andxor_bitmap20.bits_per_pixel;
  359.   g->and_map.bytewidth = ((andxor_bitmap20.width * andxor_bitmap20.bits_per_pixel)+7) / 8;
  360.   g->and_map.size = g->and_map.bytewidth * g->and_map.height;
  361.   g->and_map.num_colors
  362.     = two_to_the(andxor_bitmap20.bits_per_pixel) * andxor_bitmap20.planes;
  363.  
  364.   g->and_map.palette = (rgb_info *) malloc(g->and_map.num_colors * 
  365.                          sizeof(rgb_info));
  366.   rgb20 = andxor_bitmap20.p;
  367.   rgb = g->and_map.palette;
  368.   for (count = 0; count < g->and_map.num_colors; count++) {
  369.     rgb->blue = rgb20->blue;
  370.     rgb->red = rgb20->red;
  371.     rgb->green = rgb20->green;
  372.     rgb++;
  373.     rgb20++;
  374.   }
  375.  
  376.   g->and_map.map = NULL;
  377.   g->xor_map = g->and_map;
  378.  
  379.   g->clr_map.width = clr_bitmap20.width;
  380.   g->clr_map.height = clr_bitmap20.height;
  381.   g->clr_map.bits_per_pixel = clr_bitmap20.bits_per_pixel;
  382.  
  383.   g->clr_map.bytewidth = ((clr_bitmap20.width * clr_bitmap20.bits_per_pixel)+7) / 8;
  384.   g->clr_map.size = g->clr_map.bytewidth * g->clr_map.height;
  385.  
  386.   g->clr_map.num_colors
  387.     = two_to_the(clr_bitmap20.bits_per_pixel) * clr_bitmap20.planes;
  388.  
  389.   g->clr_map.palette = (rgb_info *) malloc(g->clr_map.num_colors * 
  390.                          sizeof(rgb_info));
  391.   rgb20 = clr_bitmap20.p;
  392.   rgb = g->clr_map.palette;
  393.   for (count = 0; count < g->clr_map.num_colors; count++) {
  394.     rgb->blue = rgb20->blue;
  395.     rgb->red = rgb20->red;
  396.     rgb->green = rgb20->green;
  397.     rgb++;
  398.     rgb20++;
  399.   }
  400.  
  401.   g->clr_map.map = NULL;
  402.  
  403.   fseek(input, andxor_bitmap20.bitmap_offset, SEEK_SET);
  404.   g->and_map.map = (char *) malloc(g->and_map.size);
  405.   read_generic_bitmap (g->and_map);
  406.  
  407.   g->xor_map.map = (char *) malloc(g->xor_map.size);
  408.   read_generic_bitmap (g->xor_map);
  409.  
  410.   fseek(input, clr_bitmap20.bitmap_offset, SEEK_SET);
  411.   g->clr_map.map = (char *) malloc(g->clr_map.size);
  412.   read_generic_bitmap (g->clr_map);
  413.  
  414.   flip_generic_map();
  415.   g++;
  416. }
  417.  
  418. void read_os2_icon_file () 
  419. {
  420.   os2_arr_hdr bm_array_hdr;
  421.   os2_file_hdr bm_file_hdr;
  422.   os2_12_bitmap_info andxor_bitmap12, clr_bitmap12;
  423.   os2_20_bitmap_info andxor_bitmap20, clr_bitmap20;
  424.   int count, cur_pos;
  425.   char *ch;
  426.   USHORT flag;
  427.  
  428.   generic_count = 0;
  429.   generic = (generic_info *) malloc(MAX_ICONS * sizeof(generic_info));
  430.   g = generic;
  431.  
  432. /* Does not handle arrays of OS/2 2.0 icons yet!!! */
  433.  
  434.   fseek(input, 0, SEEK_SET);
  435.   flag = read_shortint();
  436.  
  437.   fseek(input, 0, SEEK_SET);
  438.   if (flag == 0x4142) {
  439.     bm_array_hdr = read_os2_arr_hdr();
  440.  
  441.     cur_pos = ftell(input);
  442.     andxor_bitmap12 = read_os2_12_bitmap_info();
  443.  
  444.     if (andxor_bitmap12.cbsize == 26) {
  445.       clr_bitmap12 = read_os2_12_bitmap_info();
  446.       generic_count++;
  447.       add_os2_12_generic_bitmap(andxor_bitmap12, clr_bitmap12);
  448.     } else {
  449.       fseek(input, cur_pos, SEEK_SET);
  450.       andxor_bitmap20 = read_os2_20_bitmap_info ();
  451.       clr_bitmap20 = read_os2_20_bitmap_info();
  452.       generic_count++;
  453.       add_os2_20_generic_bitmap(andxor_bitmap20, clr_bitmap20);
  454.     }
  455.  
  456.     while (bm_array_hdr.next) {
  457.       fseek(input, bm_array_hdr.next, SEEK_SET);
  458.       bm_array_hdr = read_os2_arr_hdr();
  459.       cur_pos = ftell(input);
  460.       andxor_bitmap12 = read_os2_12_bitmap_info();
  461.       if (andxor_bitmap12.flag = 0x4943) {
  462.     if (generic_count+1 >= MAX_ICONS) {
  463.       fprintf(stderr,"Compile time constant exceeded, too many icons.\n");
  464.       return;
  465.     }
  466.  
  467.     if (andxor_bitmap12.cbsize == 26) {
  468.       clr_bitmap12 = read_os2_12_bitmap_info();
  469.       generic_count++;
  470.       add_os2_12_generic_bitmap(andxor_bitmap12, clr_bitmap12);
  471.     } else {
  472.       fseek(input, cur_pos, SEEK_SET);
  473.       andxor_bitmap20 = read_os2_20_bitmap_info ();
  474.       clr_bitmap20 = read_os2_20_bitmap_info();
  475.       generic_count++;
  476.       add_os2_20_generic_bitmap(andxor_bitmap20, clr_bitmap20);
  477.     }
  478.       }
  479.     }
  480.   } else {
  481.     andxor_bitmap12 = read_os2_12_bitmap_info ();
  482.  
  483.     if (andxor_bitmap12.cbsize == 26) {
  484.       clr_bitmap12 = read_os2_12_bitmap_info();
  485.       generic_count++;
  486.       add_os2_12_generic_bitmap(andxor_bitmap12, clr_bitmap12);
  487.     } else {
  488.       fseek(input, 0, SEEK_SET);
  489.       andxor_bitmap20 = read_os2_20_bitmap_info ();
  490.       clr_bitmap20 = read_os2_20_bitmap_info();
  491.       generic_count++;
  492.       add_os2_20_generic_bitmap(andxor_bitmap20, clr_bitmap20);
  493.     }
  494.   }
  495. }
  496.  
  497.