home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / CDRom / PHOTOC12.LZX / src / photocdaga / color.c next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  4.7 KB  |  252 lines

  1. /* hpcdtoppm (Hadmut's pcdtoppm) v0.6
  2. *  Copyright (c) 1992, 1993, 1994 by Hadmut Danisch (danisch@ira.uka.de).
  3. *  Permission to use and distribute this software and its
  4. *  documentation for noncommercial use and without fee is hereby granted,
  5. *  provided that the above copyright notice appear in all copies and that
  6. *  both that copyright notice and this permission notice appear in
  7. *  supporting documentation. It is not allowed to sell this software in 
  8. *  any way. This software is not public domain.
  9. */
  10.  
  11.  
  12. /* NOTE: Don't use this file for compiling hpcdtoppm, it is heavily */
  13. /* modified.  (By me, Günther Röhrich)                              */
  14. /* EMAIL: Guenther@studbox.uni-stuttgart.de  */
  15.  
  16. #include "hpcdtoppm.h"
  17.  
  18. extern sINT RGB_BitSh1,RGB_Maximum1;
  19. extern sINT RGB_F_LL;
  20. extern sINT RGB_F_C1,RGB_O_C1;
  21. extern sINT RGB_F_C2,RGB_O_C2;
  22. extern sINT RGB_F_G1,RGB_F_G2,RGB_O_G;
  23. extern uBYTE RGB_corr0[],RGB_corr1[],RGB_corr2[];
  24.  
  25.  
  26. static uBYTE *RGB_corr=0;
  27. static sINT T_L[256],T_R[256],T_G[256],T_g[256],T_B[256];
  28.  
  29. #define slen 3072
  30.  
  31.  
  32.  
  33. static void initcorr(void)
  34.  { 
  35.   switch(corrmode)
  36.    {case C_LINEAR: RGB_corr=RGB_corr0; break;
  37.     case C_DARK:   RGB_corr=RGB_corr1; break;
  38.     case C_BRIGHT: RGB_corr=RGB_corr2; break;
  39.     default: error(E_INTERN);
  40.    }
  41.  }
  42.  
  43.  
  44.  
  45. static void initctable(void)
  46.  {sINT i;
  47.   static sINT init=0;
  48.  
  49.   if(init) return;
  50.  
  51.   init=1;
  52.  
  53.   initcorr();
  54.  
  55.   for(i=0;i<256;i++)
  56.    {  T_L[i] = i * RGB_F_LL;
  57.       T_R[i] = i * RGB_F_C2 + RGB_O_C2;
  58.       T_G[i] = i * RGB_F_G1;
  59.       T_g[i] = i * RGB_F_G2 + RGB_O_G;
  60.       T_B[i] = i * RGB_F_C1 + RGB_O_C1;      
  61.    }
  62.   
  63.  }
  64.  
  65.  
  66. static void ycctorgb(implane *l,implane *c1,implane *c2)
  67.  {dim x,y,w,h;
  68.   uBYTE *pl,*pc1,*pc2;
  69.   sINT red,green,blue;
  70.   sINT L;
  71.  
  72.   melde("ycctorgb\n");
  73.   initctable();
  74.  
  75.   w=l->iwidth;
  76.   h=l->iheight;
  77.  
  78.   for(y=0;y<h;y++)
  79.    {
  80.     pl =  l->im + y *  l->mwidth;
  81.     pc1= c1->im + y * c1->mwidth;
  82.     pc2= c2->im + y * c2->mwidth;
  83.  
  84.     for(x=0;x<w;x++)
  85.      {
  86.       L    =  T_L[*pl]; 
  87.       red  = (L + T_R[*pc2]             )>>RGB_BitSh1;
  88.       green= (L + T_G[*pc1] + T_g[*pc2] )>>RGB_BitSh1; 
  89.       blue = (L + T_B[*pc1]             )>>RGB_BitSh1;
  90.  
  91.       red   = TRIF(red,  0,RGB_Maximum1,0,red,  RGB_Maximum1);
  92.       green = TRIF(green,0,RGB_Maximum1,0,green,RGB_Maximum1);
  93.       blue  = TRIF(blue ,0,RGB_Maximum1,0,blue, RGB_Maximum1);
  94.  
  95.       *(pl++ )=RGB_corr[red]; 
  96.       *(pc1++)=RGB_corr[green]; 
  97.       *(pc2++)=RGB_corr[blue];
  98.      }
  99.    }
  100.  }
  101. #undef BitShift
  102.  
  103.  
  104.  
  105. static uBYTE f1[slen],f2[slen];
  106.  
  107. static void sharpit(implane *l)
  108.  {sINT x,y,h,w,mw,akk;
  109.   uBYTE *old,*akt,*ptr,*work,*help,*optr=0;
  110.  
  111.   melde("sharpit\n");
  112.  
  113.   if((!l) || (!l->im)) error(E_INTERN);
  114.   if(l->iwidth > slen) error(E_INTERN);
  115.  
  116.   old=f1; akt=f2;
  117.   h=l->iheight;
  118.   w=l->iwidth;
  119.   mw=l->mwidth;
  120.  
  121.   for(y=1;y<h-1;y++)
  122.    {
  123.     ptr=l->im+ y*mw;
  124.     optr=ptr-mw;
  125.     work=akt;
  126.  
  127.     *(work++)= *(ptr++);
  128.     for(x=1;x<w-1;x++)
  129.      {  akk = 5*((sINT)ptr[0])- ((sINT)ptr[1])  - ((sINT)ptr[-1]) 
  130.                               - ((sINT)ptr[mw]) - ((sINT)ptr[-mw]);
  131.         NORM(akk);
  132.         *(work++)=akk;
  133.         ptr++;
  134.      }
  135.  
  136.     *(work++)= *(ptr++);
  137.  
  138.     if(y>1) 
  139.       for(x=0;x<w;x++)
  140.         optr[x] = old[x];
  141.  
  142.     help=old;old=akt;akt=help;
  143.      
  144.    }
  145.  
  146.  
  147.  
  148.   akt=optr+mw;
  149.   for(x=0;x<w;x++)
  150.     *(akt++) = *(old++);
  151.  }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. static void initmtable(void)
  160.  {sINT i,h;
  161.   static sINT init=0;
  162.  
  163.   if(init) return;
  164.  
  165.   init=1;
  166.  
  167.   initcorr();
  168.  
  169.   for(i=0;i<256;i++)
  170.    {  h = (i * RGB_F_LL)>>RGB_BitSh1;
  171.       h = TRIF(h,0,RGB_Maximum1,0,h,RGB_Maximum1);
  172.       T_L[i]=RGB_corr[h];
  173.    }
  174.   
  175.  }
  176.  
  177.  
  178. static void monocorr(implane *l)
  179.  {dim x,y,w,h;
  180.   uBYTE *ptr;
  181.  
  182.   melde("monocorr\n");
  183.   initmtable();
  184.  
  185.   w=l->iwidth;
  186.   h=l->iheight;
  187.  
  188.   for(y=0;y<h;y++)
  189.    {
  190.     ptr=  l->im + y *  l->mwidth;
  191.     for(x=0;x<w;x++,ptr++)
  192.      { *ptr = T_L[*ptr]; 
  193.      }
  194.    }
  195.  }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. void colconvert(sizeinfo *si,implane *l,implane *c1,implane *c2)
  204.  
  205. #define w (si->rdhlen)
  206. #define h (si->rdvlen)
  207.  
  208.  {
  209.   melde("colconvert\n");
  210.  
  211.     if((!l ) || ( l->iwidth != w ) || ( l->iheight != h) || (! l->im)) error(E_INTERN);
  212.  
  213.   if(!monochrome)
  214.    {
  215.     if((!c1) || (c1->iwidth != w ) || (c1->iheight != h) || (!c1->im)) error(E_INTERN);
  216.     if((!c2) || (c2->iwidth != w ) || (c2->iheight != h) || (!c2->im)) error(E_INTERN);
  217.    }
  218.  
  219.   if (do_crop)  cropit(si,l,c1,c2);
  220.   else          shrink(si,l,c1,c2);
  221.   if (do_sharp) sharpit(l);
  222.  
  223.   switch (outfor)
  224.    {
  225.     /*  RGB-Conversion */
  226.     case O_PPM:
  227.     case O_DCOLOR:
  228.     case O_DOVER_COLOR:
  229.         ycctorgb(l,c1,c2);
  230.                 break;
  231.  
  232.     /* Grayscale Conversion */
  233.     
  234.     case O_PGM:
  235.     case O_DGRAY:
  236.     case O_DOVER_GRAY:
  237.                 monocorr(l);
  238.                 break;
  239.  
  240.     /* No Conversion */
  241.     case O_YCC:
  242.                 break;
  243.  
  244.     default: error(E_INTERN);
  245.    }
  246. #undef w
  247. #undef h
  248.  }
  249.  
  250.  
  251.  
  252.