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

  1. /* This file is os2write.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 "icondata.h"
  22. #include "iconvars.h"
  23. #include "iconio.h"
  24. #include "os2data.h"
  25. #include "os2write.h"
  26.  
  27. int os2_map_size(bitmap_info map)
  28. {
  29.   int bytewidth;
  30.  
  31.   bytewidth = ((map.width*map.bits_per_pixel) + 7) / 8;
  32.  
  33.   while (bytewidth % 4 != 0)
  34.     bytewidth++;
  35.  
  36.   return bytewidth * map.height;
  37. }
  38.  
  39. void write_os2_map (bitmap_info map)
  40. {
  41.   BYTE *rowptr, *ch;
  42.   int  padbytes, count;
  43.  
  44.   count = map.bytewidth;
  45.   padbytes = 0;
  46.   while (count % 4 != 0)
  47.     {
  48.       count++;
  49.       padbytes++;
  50.     }
  51.  
  52.   rowptr = map.map + map.size - map.bytewidth;
  53.   while (rowptr >= map.map)
  54.     {
  55.       for (count = 0, ch = rowptr; count < map.bytewidth; count++, ch++)
  56.     write_char(*ch);
  57.  
  58.       for (count = 0; count < padbytes; count++)
  59.     write_char(0);
  60.       
  61.       rowptr -= map.bytewidth;
  62.     }
  63. }
  64.  
  65. void write_os2_icon12(os2_12_icon_info icon)
  66. {
  67.   int color_count, count;
  68.   BYTE *ch;
  69.  
  70.   write_shortint(icon.header.flag);
  71.   write_int(icon.header.cbsize);
  72.   write_int(icon.header.next);
  73.   write_shortint(icon.header.display_width);
  74.   write_shortint(icon.header.display_height);
  75.  
  76.   write_shortint(icon.andxor_map.flag);
  77.   write_int(icon.andxor_map.cbsize);
  78.   write_shortint(icon.andxor_map.hotx);
  79.   write_shortint(icon.andxor_map.hoty);
  80.   write_int(icon.andxor_map.bitmap_offset);
  81.   write_int(icon.andxor_map.bitmap_size);
  82.   write_shortint(icon.andxor_map.width);
  83.   write_shortint(icon.andxor_map.height);
  84.   write_shortint(icon.andxor_map.planes);
  85.   write_shortint(icon.andxor_map.bits_per_pixel);
  86.  
  87.   color_count = 2 << (icon.andxor_map.bits_per_pixel - 1);
  88.   ch = (char *) icon.andxor_map.p;
  89.   for (count = 0; count < (sizeof(os2_12_rgb_info) * color_count); count++)
  90.     {
  91.       write_char(*ch);
  92.       ch++;
  93.     }
  94.  
  95.   write_shortint(icon.clr_map.flag);
  96.   write_int(icon.clr_map.cbsize);
  97.   write_shortint(icon.clr_map.hotx);
  98.   write_shortint(icon.clr_map.hoty);
  99.   write_int(icon.clr_map.bitmap_offset);
  100.   write_int(icon.clr_map.bitmap_size);
  101.   write_shortint(icon.clr_map.width);
  102.   write_shortint(icon.clr_map.height);
  103.   write_shortint(icon.clr_map.planes);
  104.   write_shortint(icon.clr_map.bits_per_pixel);
  105.  
  106.   color_count = 2 << (icon.clr_map.bits_per_pixel - 1);
  107.   ch = (char *) icon.clr_map.p;
  108.   for (count = 0; count < (sizeof(os2_12_rgb_info) * color_count); count++)
  109.     {
  110.       write_char(*ch);
  111.       ch++;
  112.     }
  113. }
  114.  
  115. void write_os2_file12()
  116. {
  117.   int *info_offsets, *and_map_offsets, *clr_map_offsets;
  118.   int cur_offset;
  119.   os2_12_icon_info icon;
  120.   int count;
  121.  
  122.   fseek(output, 0, SEEK_SET);
  123.  
  124.   info_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  125.   and_map_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  126.   clr_map_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  127.  
  128.   /* Figure out what the offsets will be for the headers ... */
  129.   cur_offset = 0;
  130.   for (count = 0; count < generic_count; count++)
  131.     {
  132.       info_offsets[count] = cur_offset;
  133.  
  134.       cur_offset += 26+26+14;                    /* sizeof constant parts */
  135.       cur_offset += sizeof(os2_12_rgb_info)*2;   /* sizeof b/w palette */
  136.       cur_offset += sizeof(os2_12_rgb_info)*generic[count].clr_map.num_colors;
  137.                                                  /* sizeof color palette */
  138.     }
  139.  
  140.   /* And figure out what the offsets will be for the bitmaps ... */
  141.   for (count = 0; count < generic_count; count++)
  142.     {
  143.       and_map_offsets[count] = cur_offset;
  144.       
  145.       cur_offset += os2_map_size(generic[count].and_map);
  146.       cur_offset += os2_map_size(generic[count].xor_map);
  147.  
  148.       clr_map_offsets[count] = cur_offset;
  149.  
  150.       cur_offset += os2_map_size(generic[count].clr_map);
  151.     }
  152.  
  153.   info_offsets[generic_count] = 0;
  154.   and_map_offsets[generic_count] = 0;
  155.   clr_map_offsets[generic_count] = 0;
  156.  
  157.   /*  Write the headers to the file ... */
  158.   for (count = 0; count < generic_count; count++)
  159.     {
  160.       icon.header.flag = 0x4142;
  161.       icon.header.cbsize = 0x28;
  162.       icon.header.next = info_offsets[count+1];
  163.       icon.header.display_width = 0;
  164.       icon.header.display_height = 0;
  165.  
  166.       icon.andxor_map.flag = 0x4943;
  167.       icon.andxor_map.cbsize = 0x1A;
  168.       icon.andxor_map.hotx = generic[count].and_map.width / 2;
  169.       icon.andxor_map.hoty = generic[count].and_map.height / 2;
  170.       icon.andxor_map.bitmap_offset = and_map_offsets[count];
  171.       icon.andxor_map.bitmap_size = 12;
  172.       icon.andxor_map.width = generic[count].and_map.width;
  173.       icon.andxor_map.height = generic[count].and_map.height * 2;
  174.       icon.andxor_map.planes = 1;
  175.       icon.andxor_map.bits_per_pixel = 1;
  176.       icon.andxor_map.p = (os2_12_rgb_info *) generic[count].and_map.palette;
  177.  
  178.       icon.clr_map.flag = 0x4943;
  179.       icon.clr_map.cbsize = 0x1A;
  180.       icon.clr_map.hotx = generic[count].clr_map.width / 2;
  181.       icon.clr_map.hoty = generic[count].clr_map.height / 2;
  182.       icon.clr_map.bitmap_offset = clr_map_offsets[count];
  183.       icon.clr_map.bitmap_size = 12;
  184.       icon.clr_map.width = generic[count].clr_map.width;
  185.       icon.clr_map.height = generic[count].clr_map.height;
  186.       icon.clr_map.planes = 1;
  187.       icon.clr_map.bits_per_pixel = generic[count].clr_map.bits_per_pixel;
  188.       icon.clr_map.p = (os2_12_rgb_info *) generic[count].clr_map.palette;
  189.  
  190.       write_os2_icon12(icon);
  191.     }
  192.  
  193.   for (count = 0; count < generic_count; count++)
  194.     {
  195.       write_os2_map(generic[count].xor_map);
  196.       write_os2_map(generic[count].and_map);
  197.       write_os2_map(generic[count].clr_map);
  198.     }
  199. }
  200.  
  201. void write_os2_icon20(os2_20_icon_info icon)
  202. {
  203.   int color_count, count;
  204.   BYTE *ch;
  205.  
  206.   write_shortint(icon.header.flag);
  207.   write_int(icon.header.cbsize);
  208.   write_int(icon.header.next);
  209.   write_shortint(icon.header.display_width);
  210.   write_shortint(icon.header.display_height);
  211.  
  212.   write_shortint(icon.andxor_map.flag);
  213.   write_int(icon.andxor_map.cbsize);
  214.   write_shortint(icon.andxor_map.hotx);
  215.   write_shortint(icon.andxor_map.hoty);
  216.   write_int(icon.andxor_map.bitmap_offset);
  217.   write_int(icon.andxor_map.length);
  218.   write_int(icon.andxor_map.width);
  219.   write_int(icon.andxor_map.height);
  220.   write_shortint(icon.andxor_map.planes);
  221.   write_shortint(icon.andxor_map.bits_per_pixel);
  222.   write_int(icon.andxor_map.compression);
  223.   write_int(icon.andxor_map.bitmap_size);
  224.   write_int(icon.andxor_map.devXres);
  225.   write_int(icon.andxor_map.devYres);
  226.   write_int(icon.andxor_map.clrUsed);
  227.   write_int(icon.andxor_map.clrImportant);
  228.   write_shortint(icon.andxor_map.units);
  229.   write_shortint(icon.andxor_map.reserved);
  230.   write_shortint(icon.andxor_map.recording);
  231.   write_shortint(icon.andxor_map.rendering);
  232.   write_int(icon.andxor_map.size1);
  233.   write_int(icon.andxor_map.size2);
  234.   write_int(icon.andxor_map.color_encoding);
  235.   write_int(icon.andxor_map.app_identifier);
  236.  
  237.   color_count = 2 << (icon.andxor_map.bits_per_pixel - 1);
  238.   ch = (char *) icon.andxor_map.p;
  239.   for (count = 0; count < (sizeof(os2_20_rgb_info) * color_count); count++)
  240.     {
  241.       write_char(*ch);
  242.       ch++;
  243.     }
  244.  
  245.   write_shortint(icon.clr_map.flag);
  246.   write_int(icon.clr_map.cbsize);
  247.   write_shortint(icon.clr_map.hotx);
  248.   write_shortint(icon.clr_map.hoty);
  249.   write_int(icon.clr_map.bitmap_offset);
  250.   write_int(icon.clr_map.length);
  251.   write_int(icon.clr_map.width);
  252.   write_int(icon.clr_map.height);
  253.   write_shortint(icon.clr_map.planes);
  254.   write_shortint(icon.clr_map.bits_per_pixel);
  255.   write_int(icon.clr_map.compression);
  256.   write_int(icon.clr_map.bitmap_size);
  257.   write_int(icon.clr_map.devXres);
  258.   write_int(icon.clr_map.devYres);
  259.   write_int(icon.clr_map.clrUsed);
  260.   write_int(icon.clr_map.clrImportant);
  261.   write_shortint(icon.clr_map.units);
  262.   write_shortint(icon.clr_map.reserved);
  263.   write_shortint(icon.clr_map.recording);
  264.   write_shortint(icon.clr_map.rendering);
  265.   write_int(icon.clr_map.size1);
  266.   write_int(icon.clr_map.size2);
  267.   write_int(icon.clr_map.color_encoding);
  268.   write_int(icon.clr_map.app_identifier);
  269.  
  270.   color_count = 2 << (icon.clr_map.bits_per_pixel - 1);
  271.   ch = (char *) icon.clr_map.p;
  272.   for (count = 0; count < (sizeof(os2_20_rgb_info) * color_count); count++)
  273.     {
  274.       write_char(*ch);
  275.       ch++;
  276.     }
  277. }
  278.  
  279. os2_20_rgb_info *conv_palette (bitmap_info map)
  280. {
  281.   os2_20_rgb_info *p;
  282.   int count;
  283.  
  284.   p = (os2_20_rgb_info *) malloc (sizeof (os2_20_rgb_info) * 
  285.                   map.num_colors);
  286.  
  287.   for (count = 0; count < map.num_colors; count++)
  288.     {
  289.       p[count].red = map.palette[count].red;
  290.       p[count].green = map.palette[count].green;
  291.       p[count].blue = map.palette[count].blue;
  292.       p[count].unused = 0;
  293.     }
  294.  
  295.   return p;
  296. }
  297.  
  298. void write_os2_file20()
  299. {
  300.   int *info_offsets, *and_map_offsets, *clr_map_offsets;
  301.   int cur_offset;
  302.   os2_20_icon_info icon;
  303.   int count;
  304.  
  305.   fseek(output, 0, SEEK_SET);
  306.  
  307.   info_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  308.   and_map_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  309.   clr_map_offsets = (int *) malloc (sizeof (int) * (generic_count+1));
  310.  
  311.   /* Figure out what the offsets will be for the headers ... */
  312.   cur_offset = 0;
  313.   for (count = 0; count < generic_count; count++)
  314.     {
  315.       info_offsets[count] = cur_offset;
  316.  
  317.       cur_offset += 78+78+14;                    /* sizeof constant parts */
  318.       cur_offset += sizeof(os2_20_rgb_info)*2;   /* sizeof b/w palette */
  319.       cur_offset += sizeof(os2_20_rgb_info)*generic[count].clr_map.num_colors;
  320.                                                  /* sizeof color palette */
  321.     }
  322.  
  323.   /* And figure out what the offsets will be for the bitmaps ... */
  324.   for (count = 0; count < generic_count; count++)
  325.     {
  326.       and_map_offsets[count] = cur_offset;
  327.       
  328.       cur_offset += os2_map_size(generic[count].and_map);
  329.       cur_offset += os2_map_size(generic[count].xor_map);
  330.  
  331.       clr_map_offsets[count] = cur_offset;
  332.  
  333.       cur_offset += os2_map_size(generic[count].clr_map);
  334.     }
  335.  
  336.   info_offsets[generic_count] = 0;
  337.   and_map_offsets[generic_count] = 0;
  338.   clr_map_offsets[generic_count] = 0;
  339.  
  340.   /*  Write the headers to the file ... */
  341.   for (count = 0; count < generic_count; count++)
  342.     {
  343.       icon.header.flag = 0x4142;
  344.       icon.header.cbsize = 0x5C;
  345.       icon.header.next = info_offsets[count+1];
  346.       icon.header.display_width = 0;
  347.       icon.header.display_height = 0;
  348.  
  349.       icon.andxor_map.flag = 0x4943;
  350.       icon.andxor_map.cbsize = 0x4E;
  351.       icon.andxor_map.hotx = generic[count].and_map.width / 2;
  352.       icon.andxor_map.hoty = generic[count].and_map.height / 2;
  353.       icon.andxor_map.bitmap_offset = and_map_offsets[count];
  354.       icon.andxor_map.length = 64;
  355.       icon.andxor_map.width = generic[count].and_map.width;
  356.       icon.andxor_map.height = generic[count].and_map.height * 2;
  357.       icon.andxor_map.planes = 1;
  358.       icon.andxor_map.bits_per_pixel = 1;
  359.       icon.andxor_map.compression = 0;
  360.       icon.andxor_map.bitmap_size =   os2_map_size(generic[count].and_map) 
  361.                                 + os2_map_size(generic[count].xor_map);
  362.       icon.andxor_map.devXres = 0;
  363.       icon.andxor_map.devYres = 0;
  364.       icon.andxor_map.clrUsed = 2;
  365.       icon.andxor_map.clrImportant = 2;
  366.       icon.andxor_map.units = 0;
  367.       icon.andxor_map.reserved = 0;
  368.       icon.andxor_map.recording = 0;
  369.       icon.andxor_map.rendering = 0;
  370.       icon.andxor_map.size1 = 0;
  371.       icon.andxor_map.size2 = 0;
  372.       icon.andxor_map.color_encoding = 0;
  373.       icon.andxor_map.app_identifier = 0;
  374.       icon.andxor_map.p = conv_palette(generic[count].and_map);
  375.  
  376.       icon.clr_map.flag = 0x4943;
  377.       icon.clr_map.cbsize = 0x4E;
  378.       icon.clr_map.hotx = generic[count].clr_map.width / 2;
  379.       icon.clr_map.hoty = generic[count].clr_map.height / 2;
  380.       icon.clr_map.bitmap_offset = clr_map_offsets[count];
  381.       icon.clr_map.length = 64;
  382.       icon.clr_map.width = generic[count].clr_map.width;
  383.       icon.clr_map.height = generic[count].clr_map.height;
  384.       icon.clr_map.planes = 1;
  385.       icon.clr_map.bits_per_pixel = generic[count].clr_map.bits_per_pixel;
  386.       icon.clr_map.compression = 0;
  387.       icon.clr_map.bitmap_size = os2_map_size(generic[count].clr_map);
  388.       icon.clr_map.devXres = 0;
  389.       icon.clr_map.devYres = 0;
  390.       icon.clr_map.clrUsed = generic[count].clr_map.num_colors;
  391.  
  392.       if (icon.clr_map.clrUsed <= 2)
  393.         icon.clr_map.clrImportant = icon.clr_map.clrUsed;
  394.       else
  395.         icon.clr_map.clrImportant = 0;
  396.  
  397.       icon.clr_map.units = 0;
  398.       icon.clr_map.reserved = 0;
  399.       icon.clr_map.recording = 0;
  400.       icon.clr_map.rendering = 0;
  401.       icon.clr_map.size1 = 0;
  402.       icon.clr_map.size2 = 0;
  403.       icon.clr_map.color_encoding = 0;
  404.       icon.clr_map.app_identifier = 0;
  405.       icon.clr_map.p = conv_palette(generic[count].clr_map);
  406.  
  407.       write_os2_icon20(icon);
  408.     }
  409.  
  410.   for (count = 0; count < generic_count; count++)
  411.     {
  412.       write_os2_map(generic[count].xor_map);
  413.       write_os2_map(generic[count].and_map);
  414.       write_os2_map(generic[count].clr_map);
  415.     }
  416. }
  417.