home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / utils / fbm2fl03.lha / fppfile.c < prev    next >
C/C++ Source or Header  |  1993-10-24  |  5KB  |  244 lines

  1. /****************************************************************
  2.  * fppfile.c
  3.  ****************************************************************/
  4.  
  5. /******
  6.   Copyright (C) 1993 by Klaus Ehrenfried. 
  7.  
  8.   Permission to use, copy, modify, and distribute this software
  9.   is hereby granted, provided that the above copyright notice appears 
  10.   in all copies and that the software is available to all free of charge. 
  11.   The author disclaims all warranties with regard to this software, 
  12.   including all implied warranties of merchant-ability and fitness. 
  13.   The code is simply distributed as it is.
  14. *******/
  15.  
  16. #include <stdio.h>
  17. #include <memory.h>
  18. #include "fbm.h"
  19. #include "fpfli.h"
  20. #include <stdlib.h>
  21.  
  22. #define ERRMSG "Image has wrong format !\n"
  23.  
  24. int read_bitmap(FBM *image, char *fname);
  25. int free_fbm(FBM *image);
  26.  
  27. /****************************************************************
  28.  * check_image
  29.  ****************************************************************/
  30.  
  31. static int check_image(FBM *image)
  32. {
  33.   int c_len, test;
  34.  
  35.   fprintf(stdout," Image: %dx%d\n",image->hdr.cols,image->hdr.rows);
  36.  
  37.   if (image->hdr.planes != 1)
  38.     {
  39.       fprintf(stderr, ERRMSG);
  40.       fprintf(stderr,"No of planes: %d <> 1\n",image->hdr.planes);
  41.       return(0);
  42.     }
  43.  
  44.   if (image->hdr.physbits != 8)
  45.     {
  46.       fprintf(stderr, ERRMSG);
  47.       fprintf(stderr,"No of physbits: %d <> 8\n",image->hdr.physbits);
  48.       return(0);
  49.     }
  50.  
  51.   c_len=image->hdr.clrlen;
  52.   test = c_len % 3;
  53.  
  54.   if (test != 0)
  55.     {
  56.       fprintf(stderr, ERRMSG);
  57.       fprintf(stderr,"Funny length of color map: %d\n",c_len);
  58.       return(0);
  59.     }
  60.  
  61.   test=c_len/3;
  62.  
  63.   if (test > FLI_MAX_COLORS)
  64.     {
  65.       fprintf(stderr, ERRMSG);
  66.       fprintf(stderr,"Number of colors: %d > MAX\n",test);
  67.       return(0);
  68.     }
  69.   return(1);
  70. }
  71.  
  72. /****************************************************************
  73.  * get_image
  74.  ****************************************************************/
  75.  
  76. int get_image(char *fname,UBYTE *data,LONG color[],int without_data)
  77. {
  78.   FBM image;                    /* Image */
  79.   int ncolor, n2color;
  80.   UBYTE *fbm_cm, *fbm_bm, *pdest, *psource;
  81.   LONG rgb_value;
  82.   int i, j, len, unass, nhelp, image_width;
  83.   int idstart, idend, jdstart, jdend, isstart, jsstart;
  84.   int x_origin, y_origin;
  85.   int histogram[FLI_MAX_COLORS];
  86.  
  87.   image.bm = image.cm = (unsigned char *) NULL;
  88.  
  89.   fprintf(stdout," Load:  %s\n",fname);
  90.   if (!read_bitmap (&image, fname))
  91.     {
  92.       fprintf(stderr," Error reading bitmap from file %s\n",fname);
  93.       exitialise(1);
  94.       exit(1);
  95.     }
  96.  
  97.   if (check_image(&image) == 0)
  98.     {
  99.       free_fbm(&image);
  100.       exitialise(1);
  101.       exit(1);
  102.     }
  103.  
  104.   ncolor=(image.hdr.clrlen)/3;
  105.   fbm_cm=image.cm;
  106.   n2color=ncolor+ncolor;
  107.  
  108.   for (j=0; j < FLI_MAX_COLORS; j++)
  109.     {
  110.       histogram[j]=0;
  111.  
  112.       if (j < ncolor)
  113.     {
  114.       rgb_value=(long int) fbm_cm[j+n2color];
  115.       rgb_value=256L * rgb_value + (long int) fbm_cm[j+ncolor];
  116.       rgb_value=256L * rgb_value + (long int) fbm_cm[j];
  117.       color[j]=rgb_value;
  118.     }
  119.       else
  120.     {
  121.       color[j]=-1;
  122.     }
  123.     }
  124.  
  125.   if (border_color != 0)
  126.     {
  127.       rgb_value=color[0];
  128.       color[0]=color[border_color];
  129.       color[border_color]=rgb_value;
  130.     }
  131.  
  132.   if (without_data) 
  133.     {
  134.       if (color[0] == -1) color[0]=0;
  135.       free_fbm(&image);
  136.       return(1);
  137.     }
  138.  
  139.   fbm_bm=image.bm;
  140.   image_width = image.hdr.rowlen;
  141.  
  142.   /* compute histogram */
  143.   if (border_color == 0)
  144.     {
  145.       for (i=0; i < image.hdr.plnlen; i++)
  146.     histogram[*(fbm_bm++)]++;
  147.     }
  148.   else
  149.     {
  150.       for (i=0; i < image.hdr.plnlen; i++)
  151.     {
  152.       if (*fbm_bm == 0)
  153.         *fbm_bm = (UBYTE)  border_color;
  154.       else if (*fbm_bm == border_color)
  155.         *fbm_bm = (UBYTE) 0;
  156.  
  157.       histogram[*(fbm_bm++)]++;
  158.     }
  159.     }
  160.  
  161.   unass=0;
  162.   for (j=ncolor; j < FLI_MAX_COLORS; j++)
  163.     {
  164.       if (histogram[j] != 0)
  165.     {
  166.       color[j]=0;
  167.       unass++;
  168.     }
  169.     }
  170.  
  171.   if (unass != 0)
  172.     {
  173.       fprintf(stderr,"Warning: %d unassigned color(s) referenced\n",unass);
  174.     }
  175.  
  176.   if (color[0] == -1) color[0]=0;
  177.  
  178.   memset(data, 0, fli_size);
  179.  
  180.   if (Xorigin_flag == 1)
  181.     {
  182.       x_origin = Xorigin;
  183.     }
  184.   else
  185.     {
  186.       nhelp=fli_width - image.hdr.cols;
  187.       x_origin = nhelp/2;
  188.     }
  189.  
  190.   if (x_origin >= 0)
  191.     {
  192.       idstart = x_origin;
  193.       isstart = 0;
  194.     }
  195.   else
  196.     {
  197.       idstart = 0;
  198.       isstart = -x_origin;
  199.     }
  200.   nhelp = x_origin + image.hdr.cols;
  201.   idend = (nhelp < fli_width) ? nhelp : fli_width;
  202.  
  203.   if (Yorigin_flag == 1)
  204.     {
  205.       y_origin = Yorigin;
  206.     }
  207.   else
  208.     {
  209.       nhelp=fli_height - image.hdr.rows;
  210.       y_origin = nhelp/2;
  211.     }
  212.  
  213.   if (y_origin >= 0)
  214.     {
  215.       jdstart = y_origin;
  216.       jsstart = 0;
  217.     }
  218.   else
  219.     {
  220.       jdstart = 0;
  221.       jsstart = -y_origin;
  222.     }
  223.   nhelp = y_origin + image.hdr.rows;
  224.   jdend = (nhelp < fli_height) ? nhelp : fli_height;
  225.  
  226.   psource = image.bm + (jsstart * image_width + isstart);
  227.   pdest = data + (jdstart * fli_width + idstart);
  228.  
  229.   len = idend - idstart;
  230.  
  231.   if (len > 0)
  232.     {
  233.       for (j=jdstart; j < jdend; j++)
  234.     {
  235.       memcpy(pdest, psource, len);
  236.       psource += image_width;
  237.       pdest += fli_width;
  238.     }
  239.     }
  240.  
  241.   free_fbm(&image);
  242.   return(1);
  243. }
  244.