PIXELATE.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 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 /* PIXELATE.C  */
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <float.h>
00029 #include <math.h>
00030 #include <windows.h>
00031 #include "struct.h"           /* general structures    */
00032 #include "..\common\postprocess\ximage.h"
00033 #include "local.h"
00034 
00035 #include "pixelate.h"
00036 
00037 #if __X__MIPS__
00038 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00039 #endif
00040 
00041 static HINSTANCE hDLLinstance=NULL; /* use to pick up resources from DLL  */
00042 
00043 #include "utils.h"
00044 #include "paint.c"
00045 
00046 #if __WATCOMC__
00047 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00048 #elif __BC__
00049 BOOL WINAPI DllEntryPoint(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00050 #else
00051 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00052 #endif
00053   switch (dwReason) {
00054     case DLL_PROCESS_ATTACH:
00055       hDLLinstance = hDLL;  /* handle to DLL file */
00056       break;
00057     case DLL_PROCESS_DETACH:
00058       break;
00059   }
00060   return (int)TRUE;
00061 }
00062 
00063 static void GetConeR(long x, long y, long r,
00064                      long Xmax, long Ymax,
00065                      unsigned char *R,
00066                      unsigned char *G,
00067                      unsigned char *B,
00068                      fullscreenbuffer *S){
00069  float d,dc=75.0;
00070  d=dc*((float)rand()/32767.0 - 0.5);
00071  d += (float)((S+y*Xmax+x)->R);
00072  d=min(255.0,d);
00073  d=max(0,d);
00074  *R=(unsigned char)d;
00075  d=dc*((float)rand()/32767.0 - 0.5);
00076  d += (float)((S+y*Xmax+x)->G);
00077  d=min(255.0,d);
00078  d=max(0,d);
00079  *G=(unsigned char)d;
00080  d=dc*((float)rand()/32767.0 - 0.5);
00081  d += (float)((S+y*Xmax+x)->B);
00082  d=min(255.0,d);
00083  d=max(0,d);
00084  *B=(unsigned char)d;
00085  return;
00086 }
00087 
00088 static void GetConeC(long x, long y, long r,
00089                      long Xmax, long Ymax,
00090                      unsigned char *R,
00091                      unsigned char *G,
00092                      unsigned char *B,
00093                      fullscreenbuffer *S){
00094  *R=(S+y*Xmax+x)->R;
00095  *G=(S+y*Xmax+x)->G;
00096  *B=(S+y*Xmax+x)->B;
00097  return;
00098 }
00099 
00100 static void GetConeA(long x, long y, long r,
00101                      long Xmax, long Ymax,
00102                      unsigned char *R,
00103                      unsigned char *G,
00104                      unsigned char *B,
00105                      fullscreenbuffer *S){
00106  float d,c=0.0,dr=0.0,dg=0.0,db=0.0;
00107  long  i,j;
00108  fullscreenbuffer *s;
00109  for(i=max(x-r,0);i<min(x+r,Xmax-1);i++)
00110  for(j=max(y-r,0);j<min(y+r,Ymax-1);j++){
00111    d=sqrt((x-i)*(x-i)+(y-j)*(y-j));
00112    if(d < (float)r){
00113      s=(S+j*Xmax+i);
00114      c  += 1.0;
00115      dr += (float)s->R;
00116      dg += (float)s->G;
00117      db += (float)s->B;
00118    }
00119  }
00120  *R = (unsigned char)(dr/c);
00121  *G = (unsigned char)(dg/c);
00122  *B = (unsigned char)(db/c);
00123  return;
00124 }
00125 
00126 static void DrawCone(long x, long y, long r,
00127                      long Xmax, long Ymax,
00128                      unsigned char R,
00129                      unsigned char G,
00130                      unsigned char B,
00131                      fullscreenbuffer *S,
00132                      float            *Z,
00133                      int type){
00134  long  i,j;
00135  float d,z;
00136  fullscreenbuffer *s;
00137  for(i=max(x-r,0);i<min(x+r,Xmax-1);i++)
00138  for(j=max(y-r,0);j<min(y+r,Ymax-1);j++){
00139    d=sqrt((float)(x-i)*(float)(x-i)+
00140           (float)(y-j)*(float)(y-j));
00141    if(d < (float)r){
00142      z=(r-d);
00143      if(z > *(Z+j*Xmax+i)){
00144        s=(S+j*Xmax+i);
00145 //#if 0
00146        if(type == 1 && r < 4) {
00147          if((d=fabs(z - *(Z+j*Xmax+i))) < 2.0){
00148            s->R=(2.0-d)/2.0*255+d/2.0*R;
00149            s->G=(2.0-d)/2.0*255+d/2.0*G;
00150            s->B=(2.0-d)/2.0*255+d/2.0*B;
00151          }
00152          else{
00153            s->R=R;
00154            s->G=G;
00155            s->B=B;
00156          }
00157        }
00158        else{
00159 //#endif
00160          s->R=R;
00161          s->G=G;
00162          s->B=B;
00163        }
00164        *(Z+j*Xmax+i) = z;
00165      }
00166    }
00167  }
00168  return;
00169 }
00170 
00171 long _RenderImageProcess(char *PrmList, XIMAGE *lpXimage){
00172  unsigned char R,G,B;
00173  long v,x,y,i,j,iRad,Xres,Yres,nXcones,nYcones,type;
00174  char name_path[255];
00175  float rad=0.125,*Zb;
00176  fullscreenbuffer *S,*So;
00177  sscanf(PrmList,"%s %ld %ld %f",name_path,&v,&type,&rad);
00178  Xres=lpXimage->Xmax; Yres=lpXimage->Ymax;
00179  iRad=(long)(rad*(float)Xres);
00180 // iRad=10;
00181 // iRad=40;
00182  nXcones=Xres/iRad+1;
00183  nYcones=Yres/iRad+1;
00184 
00185  if(type == 0){
00186    iRad *= 2.0;  // for crystalize
00187    nXcones *= 1.5; nYcones *= 1.5;
00188  }
00189  else{
00190    iRad *= 0.5;  // for pointillize
00191    nXcones *= 1.5; nYcones *= 1.5;
00192  }
00193 
00194 //sprintf(name_path,"Irad=%ld",iRad);
00195 //MessageBox(NULL,name_path,NULL,MB_OK);
00196 
00197  So=(fullscreenbuffer *)X__Malloc(Xres*Yres*(long)sizeof(fullscreenbuffer));
00198  if(So == NULL){
00199    MessageBox(GetFocus(),"Memory Out",NULL,MB_OK);
00200    return 0;
00201  }
00202  Zb=(float *)X__Malloc(Xres*Yres*(long)sizeof(float));
00203  if(So == NULL){
00204    X__Free(So);
00205    MessageBox(GetFocus(),"Memory Out",NULL,MB_OK);
00206    return 0;
00207  }
00208  S=lpXimage->Screen;
00209  memset(So,0xFF,sizeof(fullscreenbuffer)*Xres*Yres);
00210  for(i=0;i<Xres;i++)for(j=0;j<Yres;j++)*(Zb+j*Xres+i)=0.0;
00211  for(i=0;i<nXcones;i++)for(j=0;j<nYcones;j++){
00212    x=(long)(Xres*(float)rand()/32767.0);
00213    y=(long)(Yres*(float)rand()/32767.0);
00214    GetConeC(x,y,iRad,Xres,Yres,&R,&G,&B,S);    // At centre
00215 //   GetConeR(x,y,iRad,Xres,Yres,&R,&G,&B,S);  // Random
00217    DrawCone(x,y,iRad,Xres,Yres,R,G,B,So,Zb,type);
00218  }
00219 
00220  memcpy(S,So,sizeof(fullscreenbuffer)*Xres*Yres);
00221  X__Free(So);
00222  X__Free(Zb);
00223  return 1;
00224 }
00225 
00226 /*************** Function that renders any of the OpenGL Functionality ************/
00227 
00228 long _RenderGLexternal(char *PrmList, XIMAGE *lpXimage){
00229 MessageBox(NULL,"OpenGL function called","OK",MB_OK);
00230  return 1;
00231 }
00232 
00233 /*************** +++++++++++++++++++++++++++++++++++++++++++++++++++++ ************/
00234 
00235 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00236 
00237 static int version=1;
00238 static long type=0;  // 0=crystalize 1=pointilize
00239 static double radius=0.125;
00240 
00241 char * _SetExternalParameters(
00242   char *Op,                 /* string for the parameters                  */
00243   HWND hWnd,                /* parent window                              */
00244   long ruler,               /* ruler scale value to facilitate scaling    */
00245   char *name,               /* name of DLL file with the effect           */
00246   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00247                                     ){
00248  char buffer[256];
00249  if(Op != NULL){  /* parameters exist so read them off the list */
00250    sscanf(Op,"%s %ld %ld %f",buffer,&version,&type,&radius);
00251  }
00252  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_PIXELATE),hWnd,
00253               (DLGPROC)DlgProc) == FALSE)return Op;
00254  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00255  sprintf(buffer,"%s %ld %ld %f",name,version,type,radius);
00256  if((Op=(char *)CALL_MALLOC(strlen(buffer)+1)) == NULL){
00257   MessageBox (GetFocus(),"External effect: Out of memory","Error",
00258                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00259    return NULL;
00260  }
00261  strcpy(Op,buffer);
00262  return Op;
00263 }
00264 
00265 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00266  char str[32];
00267  BOOL err;
00268  switch( msg ) {
00269    case WM_INITDIALOG:
00270      radius *= 100.0;
00271      sprintf(str,"%.1f",radius);
00272      SetDlgItemText(hwnd,DLG_PIXELATE_RADIUS,str);
00273      if     (type == 0)
00274        SendDlgItemMessage(hwnd,DLG_PIXELATE_CRYSTAL,BM_SETCHECK,TRUE,0);
00275      if     (type == 1)
00276        SendDlgItemMessage(hwnd,DLG_PIXELATE_POINTS,BM_SETCHECK,TRUE,0);
00277      CentreDialogOnScreen(hwnd);
00278      return TRUE;
00279    case WM_PAINT:
00280      PaintBackground(hwnd);
00281      break;
00282    case WM_COMMAND:
00283      switch(LOWORD(wparam)){
00284         case IDCANCEL:
00285           EndDialog(hwnd,FALSE);
00286           return(TRUE);
00287         case IDOK:
00288           if(GetDlgItemText(hwnd,DLG_PIXELATE_RADIUS,str,12) != 0)
00289             radius=atof(str);
00290           radius *= 0.01;
00291           if(SendDlgItemMessage(hwnd,DLG_PIXELATE_CRYSTAL,BM_GETCHECK,0,0))
00292                type=0;
00293           else type=1;
00294 
00295           EndDialog(hwnd,TRUE);
00296           return(TRUE);
00297         default:
00298           break;
00299       }
00300       break;
00301     default: break;
00302  }
00303  return FALSE;
00304 }
00305 

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