FILTER.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2007 OpenFX Development Team
00004 -- */
00005 
00006 /* FILTER.C  image post-processing external DLL                         */
00007 /*                                                                      */
00008 /* This image processor gives the user the opportunity to alter the     */
00009 /* colour balance in an image once it has been rendered. The proportion */
00010 /* of red, green and blue in each pixel is adjusted by multiplying      */
00011 /* the R,G,B values held in the ScreenBuffer after they have been       */
00012 /* calculated but before they are written to disk or built into an      */
00013 /* animation.                                                           */
00014 /*                                                                      */
00015 /* This example servers to illustrate the design of the Dialog Box      */
00016 /* called by the Animator to set up the process and how pixels in the   */
00017 /* screen buffer are addressed and modified.                            */
00018 /*                                                                      */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <float.h>
00023 #include <math.h>
00024 #include <windows.h>
00025 
00026 #include <gl/gl.h>
00027 #include <gl/glu.h>
00028 
00029 // Need these when using OpenGL extensions
00030 #include <fcntl.h>
00031 #define __gl2_main_
00032 #include "..\gl2.h"
00033 #define PADDR(functype,funcname) (funcname = (functype) wglGetProcAddress( #funcname ))
00035 
00036 #include "struct.h"           /* general structures    */
00037 #include "..\common\postprocess\ximage.h"
00038 #include "local.h"
00039 
00040 #include "filter.h"
00041 
00042 #if __X__MIPS__
00043 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00044 #endif
00045 
00046 static HINSTANCE hDLLinstance=NULL; /* use to pick up resources from DLL   */
00047 
00048 /************************** Local Utility Functions ***********************/
00049 
00050 #include "utils.h"
00051 
00052 #include "paint.c"
00053 
00054 /************************** DLL entry point ********************************/
00055 
00056 #if __WATCOMC__
00057 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00058 #elif __BC__
00059 BOOL WINAPI DllEntryPoint(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00060 #else
00061 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00062 #endif
00063   switch (dwReason) {
00064     case DLL_PROCESS_ATTACH:
00065 #if __X__MIPS__
00066       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00067 #endif
00068 //MessageBox(NULL,"Attaching",NULL,MB_OK);
00069       hDLLinstance = hDLL;  /* handle to DLL file */
00070       break;
00071     case DLL_PROCESS_DETACH:
00072 #if __X__MIPS__
00073       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00074 #endif
00075 //MessageBox(NULL,"Detaching",NULL,MB_OK);
00076       break;
00077   }
00078 return (int)TRUE;
00079 }
00080 
00081 #if __SC__
00082 #pragma startaddress(DllMain)
00083 #endif
00084 
00085 /*******************************************************************\
00086 |                Code that executes the process                     |
00087 \*******************************************************************/
00088 
00089 long _RenderImageProcess(char *PrmList, XIMAGE *lpXimage){
00090  int i,j;
00091  double rs,gs,bs;
00092  char dummy[255];
00093  fullscreenbuffer *S;
00094  /* read the parameters from the parameter list */
00095  sscanf(PrmList,"%s %f %f %f",dummy,&rs,&gs,&bs);
00096  /* set local pointer to start of screen buffer */
00097  S=lpXimage->Screen;
00098  for(j=0;j<lpXimage->Ymax;j++)   /* do rows in screen buffer */
00099  for(i=0;i<lpXimage->Xmax;i++){  /* do all pixles in row j   */
00100    /* scale the R,G,B values for pixel i,j */
00101    S->R = min(255,(unsigned char)((double)S->R * rs));
00102    S->G = min(255,(unsigned char)((double)S->G * gs));
00103    S->B = min(255,(unsigned char)((double)S->B * bs));
00104    S++;             /* point to next pixels in screen buffer */
00105  }
00106  return 1;          /* all done OK                           */
00107 }
00108 
00109 /*************** Function that renders any of the OpenGL Functionality ************/
00110 
00111 long _RenderGLexternal(char *PrmList, XIMAGE *lpXimage){
00112  GLfloat rs,gs,bs;
00113  char dummy[255];
00114  gl2Initialize(); // needed for some functions 
00115  sscanf(PrmList,"%s %f %f %f",dummy,&rs,&gs,&bs);
00116  //glEnable(GL_CONVOLUTION_2D);
00117  //glDisable(GL_CONVOLUTION_2D);
00118  glMatrixMode(GL_PROJECTION);
00119  glLoadIdentity();
00120  glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
00121  glMatrixMode( GL_MODELVIEW );
00122  glLoadIdentity();
00123  glViewport( 0, 0, lpXimage->Xmax,lpXimage->Ymax);
00124  glEnable(GL_BLEND);
00125  glDisable(GL_DEPTH_TEST);
00126  glDepthMask(GL_FALSE);
00127  glBlendColor(rs,gs,bs,1.0);
00128  // glBlendColor(1.0,0.0,0.0,1.0); // for a red filter
00129  glBlendFunc(GL_ONE_MINUS_CONSTANT_COLOR,GL_CONSTANT_COLOR);
00130  glColor4f(0.0,0.0,0.0,1.0);
00131  glBegin(GL_QUADS);
00132  glVertex3f(0.0,0.0,-1.0); 
00133  glVertex3f(1.0,0.0,-1.0); 
00134  glVertex3f(1.0,1.0,-1.0);
00135  glVertex3f(0.0,1.0,-1.0);
00136  glEnd();
00137  glEnable(GL_DEPTH_TEST);
00138  glDepthMask(GL_TRUE);
00139  glDisable(GL_BLEND);
00140  return 1;
00141 }
00142 
00143 /*************** Functions used for set up  ***************/
00144 
00145 
00146 /* local variable for communication between dialog box and Setup function */
00147 double red_scale=1.0,green_scale=1.0,blue_scale=1.0;
00148 
00149 /* Dialog box callback prototype */
00150 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00151 
00152 char * _SetExternalParameters(
00153   char *Op,                 /* string for the parameters                  */
00154   HWND hWnd,                /* parent window                              */
00155   long ruler,               /* ruler scale value to facilitate scaling    */
00156   char *name,               /* name of DLL file with the effect           */
00157   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00158                                     ){
00159  /* output buffer should be as long as necessary to hold full string        */
00160  char buffer[256];
00161  if(Op != NULL){  /* parameters exist so read them off the list */
00162    sscanf(Op,"%s %f %f %f",buffer,&red_scale,&green_scale,&blue_scale);
00163  }
00164  /*   Do the user interface as required to set up the effect, may use a     */
00165  /*   dialog box etc. Return old string if effect is cancelled (no change)  */
00166  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_FILTER),hWnd,
00167               (DLGPROC)DlgProc) == FALSE)return Op;
00168  /* Free space occupied by old parameter string */
00169  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00170  /* print the parameters into the buffer */
00171  sprintf(buffer,"%s %f %f %f",name,red_scale,green_scale,blue_scale);
00172  /* Prepare the output buffer to take copy of parameter list */
00173  if((Op=(char *)CALL_MALLOC(strlen(buffer)+1)) == NULL){
00174   MessageBox (GetFocus(),"External effect: Out of memory","Error",
00175                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00176    return NULL;
00177  }
00178  /* Copy the parameter string to the output buffer */
00179  strcpy(Op,buffer);
00180  return Op;
00181 }
00182 
00183 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00184  char str[32];
00185  switch( msg ) {
00186    case WM_INITDIALOG:
00187      /* prime dialog box controls          */
00188      sprintf(str,"%.2f",red_scale);
00189      SetDlgItemText(hwnd,DLG_FILTER_RED,str);
00190      sprintf(str,"%.2f",green_scale);
00191      SetDlgItemText(hwnd,DLG_FILTER_GREEN,str);
00192      sprintf(str,"%.2f",blue_scale);
00193      SetDlgItemText(hwnd,DLG_FILTER_BLUE,str);
00194      CentreDialogOnScreen(hwnd);
00195      return TRUE;
00196    case WM_PAINT:
00197      PaintBackground(hwnd);
00198      break;
00199    case WM_COMMAND:
00200      switch(LOWORD(wparam)){
00201         case IDCANCEL:    /* user clicked cancel                            */
00202           EndDialog(hwnd,FALSE);
00203           return(TRUE);
00204         case IDOK:        /* OK so get numbers from the Dailog Box controls */
00205           if(GetDlgItemText(hwnd,DLG_FILTER_RED,str,12) != 0)
00206             sscanf(str,"%f",&red_scale);
00207           if(GetDlgItemText(hwnd,DLG_FILTER_GREEN,str,12) != 0)
00208             sscanf(str,"%f",&green_scale);
00209           if(GetDlgItemText(hwnd,DLG_FILTER_BLUE,str,12) != 0)
00210             sscanf(str,"%f",&blue_scale);
00211           EndDialog(hwnd,TRUE);
00212           return(TRUE);
00213         default:
00214           break;
00215       }
00216       break;
00217     default: break;
00218  }
00219  return FALSE;
00220 }
00221 

Generated on Sun Apr 27 14:20:12 2014 for OpenFX by  doxygen 1.5.6