home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Graphics / PPT / pluginsrc / gray / grayscale.c next >
C/C++ Source or Header  |  1999-12-08  |  5KB  |  196 lines

  1. /*----------------------------------------------------------------------*/
  2. /*
  3.     PROJECT: ppt filters
  4.     MODULE : grayscale
  5.  
  6.     PPT and this file are (C) Janne Jalkanen 1998.
  7.  
  8.     $Id: grayscale.c,v 3.0 1999/12/08 23:08:33 jj Exp jj $
  9. */
  10. /*----------------------------------------------------------------------*/
  11.  
  12. /*----------------------------------------------------------------------*/
  13. /* Includes */
  14.  
  15. #include <pptplugin.h>
  16.  
  17. /*----------------------------------------------------------------------*/
  18. /* Defines */
  19.  
  20. #define MYNAME      "Greyscale"
  21.  
  22. /*----------------------------------------------------------------------*/
  23. /* Internal prototypes */
  24.  
  25. /*----------------------------------------------------------------------*/
  26. /* Global variables */
  27.  
  28. #pragma msg 186 ignore
  29.  
  30. const char blurb[] =
  31.     "This converts a color picture to\n"
  32.     "a greyscaled one by calculating\n"
  33.     "a weighted average of main components\n";
  34.  
  35. const struct TagItem MyTagArray[] = {
  36.     PPTX_NoNewFrame, TRUE,
  37.     PPTX_Name, MYNAME,
  38.     /* Other tags go here */
  39.     PPTX_Author, "Janne Jalkanen, 1995-1999",
  40.     PPTX_InfoTxt, blurb,
  41.     PPTX_ColorSpaces, CSF_RGB|CSF_ARGB,
  42.     PPTX_RexxTemplate, "",
  43.     PPTX_SupportsGetArgs, TRUE,
  44.     TAG_END, 0L
  45. };
  46.  
  47. #if defined( __GNUC__ )
  48. const BYTE LibName[]="grayscale.effect";
  49. const BYTE LibIdString[]="grayscale.effect 1.3";
  50. const UWORD LibVersion=1;
  51. const UWORD LibRevision=3;
  52. ADD2LIST(LIBEffectInquire,__FuncTable__,22);
  53. ADD2LIST(LIBEffectExec,__FuncTable__,22);
  54. /* The following two definitions are only required
  55.    if you link with libinitr */
  56. ADD2LIST(LIBEffectInquire,__LibTable__,22);
  57. ADD2LIST(LIBEffectExec,__LibTable__,22);
  58. ADD2LIST(-1,__LibTable__,20); /* End marker for MakeLibrary() */
  59.  
  60. /* Make GNU C specific declarations. __UtilityBase is
  61.    required for the libnix integer multiplication */
  62. struct ExecBase *SysBase = NULL;
  63. struct Library *__UtilityBase = NULL;
  64. #endif
  65.  
  66.  
  67. /*----------------------------------------------------------------------*/
  68. /* Code */
  69.  
  70. LIBINIT
  71. {
  72. #if defined(__GNUC__)
  73.     SysBase = SYSBASE();
  74.  
  75.     if( NULL == (__UtilityBase = OpenLibrary("utility.library",37L))) {
  76.         return 1L;
  77.     }
  78.  
  79. #endif
  80.     return 0;
  81. }
  82.  
  83. LIBCLEANUP
  84. {
  85. #if defined(__GNUC__)
  86.     if( __UtilityBase ) CloseLibrary(__UtilityBase);
  87. #endif
  88. }
  89.  
  90. EFFECTINQUIRE(attr,PPTBase,EffectBase)
  91. {
  92.     return TagData( attr, MyTagArray );
  93. }
  94.  
  95. EFFECTEXEC(frame,tags,PPTBase,EffectBase)
  96. {
  97.     UWORD beginx, endx;
  98.     UWORD row;
  99.     UBYTE comps;
  100.     FRAME *newframe;
  101.  
  102.     beginx = frame->selbox.MinX;
  103.     endx   = frame->selbox.MaxX;
  104.  
  105.     InitProgress( frame, "Grayscaling...", frame->selbox.MinY, frame->selbox.MaxY );
  106.  
  107.     if(frame->selbox.MinY == 0 && beginx == 0 && endx == frame->pix->width &&
  108.        frame->selbox.MaxY == frame->pix->height)
  109.         {
  110.             PIXINFO *p;
  111.  
  112.             newframe = MakeFrame( frame );
  113.             if(!newframe) {
  114.                 return NULL;
  115.             }
  116.  
  117.             p = newframe->pix;
  118.  
  119.             p->components = 1;
  120.             p->colorspace = CS_GRAYLEVEL;
  121.             p->origdepth  = 8;
  122.             if(InitFrame( newframe ) != PERR_OK) {
  123.                 RemFrame(newframe);
  124.                 return NULL;
  125.             }
  126.             comps = 1;
  127.         } else {
  128.             newframe = DupFrame( frame, DFF_COPYDATA );
  129.             if(!newframe)
  130.                 return NULL;
  131.  
  132.             comps = frame->pix->components;
  133.         }
  134.  
  135.     for( row = frame->selbox.MinY; row < frame->selbox.MaxY; row++) {
  136.         WORD col;
  137.         ROWPTR scp, dcp, cp;
  138.  
  139.         scp = GetPixelRow( frame, row );
  140.         dcp = GetPixelRow( newframe, row );
  141.         cp  = dcp + MULU16(beginx, comps);
  142.         scp += MULU16(beginx,comps);
  143.  
  144.         if(Progress(frame, row)) {
  145.             RemFrame(newframe);
  146.             newframe = NULL;
  147.             goto quit;
  148.         }
  149.  
  150.         /*
  151.          *  Inner loop. This should be fairly quick anyways...
  152.          */
  153.  
  154.         for( col = beginx; col < endx; col++ ) {
  155.             UWORD val;
  156.             UBYTE r,g,b;
  157.  
  158.             if( frame->pix->colorspace == CS_ARGB ) scp++; /* Skip alpha */
  159.  
  160.             r = *scp++; g = *scp++; b = *scp++;
  161.             val = ( (r * (UWORD)30) + (g * (UWORD)59) + (b * (UWORD)11) ) / (UWORD) 100;
  162.  
  163.             switch( newframe->pix->colorspace ) {
  164.                 case CS_RGB:
  165.                     cp[0] = cp[1] = cp[2] = (UBYTE)val;
  166.                     break;
  167.                 case CS_ARGB:
  168.                     cp[1] = cp[2] = cp[3] = (UBYTE)val;
  169.                     break;
  170.                 case CS_GRAYLEVEL:
  171.                     *cp = (UBYTE)val;
  172.                     break;
  173.             }
  174.  
  175.             cp += comps;
  176.  
  177.         }
  178.         PutPixelRow( newframe, row, dcp );
  179.     }
  180.  
  181. quit:
  182.     ClearProgress(frame);
  183.  
  184.     return newframe;
  185. }
  186.  
  187. EFFECTGETARGS(frame,tags,PPTBase,EffectBase)
  188. {
  189.     return PERR_OK;
  190. }
  191.  
  192. /*----------------------------------------------------------------------*/
  193. /*                            END OF CODE                               */
  194. /*----------------------------------------------------------------------*/
  195.  
  196.