Новые уровни
От: xmlx  
Дата: 11.03.09 14:39
Оценка: :))
компилируйте в visual studio и пользуйтесь!
#pragma comment(linker, "/SUBSYSTEM:windows")
#include <windows.h>
#include <math.h>

//класс для создания главного окна
const char szClassName[]="MosaicAndFont";
HWND Main_hwnd,hwndEdit1;
WNDPROC MainProc;
WNDCLASSEX wcex;
HDC myDC;

long x,y,m_x,m_y,i,j;
long flip_cnt;
long crx[8];
long pdata[256][256],cpdata[256][256],orgimg[256][256],cpyimg[256][256];

long my_stackPointer;
long my_stack[65536];
char editdata[16];

float hsv_h,hsv_s,hsv_v,r,g,b;

long fmin(long a, long b);
void Flip();
void Skeletonization();
LRESULT CALLBACK Edit1WndProc(HWND, UINT, WPARAM,LPARAM); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM,LPARAM); 

long st_pop(long &st_x,long &st_y)
{ 
    if(my_stackPointer > 0) 
    { 
        long p = my_stack[my_stackPointer]; 
        st_x = (long)(p/160); 
        st_y = (long)(p%160); 
        my_stackPointer--; 
        return 1;
    }     
    else 
    { 
        return 0; 
    }    
}    

long st_push(long st_x,long st_y) 
{ 
    if(my_stackPointer < 65536 - 1) 
    { 
        my_stackPointer++; 
        my_stack[my_stackPointer] = (long)(st_x*160 + st_y); 
        return 1; 
    }     
    else 
    { 
        return 0; 
    }    
}     

void my_emptyStack()
{ 
 long st_x, st_y;
 while(st_pop(st_x, st_y)); 
}

void my_floodFill4Stack(long orgimg[256][256],long x,long y,long newColor,long oldColor)
{
    if(newColor == oldColor) return; //avoid infinite loop
    my_emptyStack();
    if(!st_push(x, y)) return; 
    while(st_pop(x, y))
    {
        orgimg[x][y] = newColor;
        if(((x + 1) < 160) && (orgimg[x + 1][y] == oldColor))
        {          
            if(!st_push(x + 1, y)) return;           
        }    
        if(((x - 1) > 0) && (orgimg[x - 1][y] == oldColor))
        {
            if(!st_push(x - 1, y)) return;           
        }    
        if(((y + 1) < 160) && (orgimg[x][y + 1] == oldColor))
        {
            if(!st_push(x, y + 1)) return;           
        }    
        if(((y - 1) > 0) && (orgimg[x][y - 1] == oldColor))
        {
            if(!st_push(x, y - 1)) return;           
        }    
    }     
}


void my_HSV2RGB()
{
    int i;
    float f, p, q, t, hTemp;

    if( hsv_s == 0.0 || hsv_h == -1.0) // s==0? Totally unsaturated = grey so R,G and B all equal value
    {
      r = g = b = hsv_v;
      return;
    }
    hTemp = hsv_h/60.0f;
    i = (int)floor( hTemp );                // which sector
    f = hTemp - i;                      // how far through sector
    p = hsv_v * ( 1 - hsv_s );
    q = hsv_v * ( 1 - hsv_s * f );
    t = hsv_v * ( 1 - hsv_s * ( 1 - f ) );

    switch( i )
    {
    case 0:{r = hsv_v;g = t;b = p;break;}
    case 1:{r = q;g = hsv_v;b = p;break;}
    case 2:{r = p;g = hsv_v;b = t;break;}
    case 3:{r = p;g = q;b = hsv_v;break;}
    case 4:{r = t;g = p;b = hsv_v;break;}
    case 5:{r = hsv_v;g = p;b = q;break;}
    }
}

long fmin(long a, long b)
{
 if (a<b) { return a; } else { return b; };
};

void Flip()
{
 long xi,xj,f,p;
 flip_cnt=0;
 for (xi=1;xi<=m_x-2;xi++) {
 for (xj=1;xj<=m_y-2;xj++) {
  if (cpdata[xi][xj]==1) {
   f=0;
   p=0;
   pdata[xi][xj]=1;
   crx[0]=cpdata[xi-1][xj-1];
   crx[1]=cpdata[xi][xj-1];
   crx[2]=cpdata[xi+1][xj-1];
   crx[3]=cpdata[xi+1][xj];
   crx[4]=cpdata[xi+1][xj+1];
   crx[5]=cpdata[xi][xj+1];
   crx[6]=cpdata[xi-1][xj+1];
   crx[7]=cpdata[xi-1][xj];
   for (j=0;j<=7;j++) { if (crx[j]==1) { f+=1; } else if (crx[j]==0) { p+=1;};};
   if ((f==2) && (p!=6)) {pdata[xi][xj]=0;};
  };

  if (cpdata[xi][xj]>1) {
   pdata[xi][xj]=cpdata[xi][xj]-1;
   flip_cnt=flip_cnt+1;
  };
 };};
 for (xi=0;xi<=m_x-1;xi++) {
 for (xj=0;xj<=m_y-1;xj++) {
  cpdata[xi][xj]=pdata[xi][xj];
 };};
};

void Skeletonization()
{
 long xi,xj,pxlmk,xt,k,q,p;
 m_x=160;
 m_y=160;
 for (xi=0;xi<=m_x-1;xi++) {
 for (xj=0;xj<=m_y-1;xj++) {
  cpdata[xi][xj]=pdata[xi][xj];
     };};
//=========== step 1 (Distance Transform)
 for (i=0;i<=1;i++) {
  while (true) {
   pxlmk=0;
   for (xi=1;xi<=m_x-2;xi++) {
   for (xj=1;xj<=m_y-2;xj++) {
    xt=cpdata[xi][xj];
    if (cpdata[xi][xj]!=0) { cpdata[xi][xj]=fmin(fmin(cpdata[xi][xj-1],cpdata[xi][xj+1]),fmin(cpdata[xi-1][xj],cpdata[xi+1][xj]))+1;};
    if (xt!=cpdata[xi][xj]) { pxlmk+=1;};
   };};
   if (pxlmk==0) break;
  };
  for (xi=0;xi<=m_x-1;xi++) {
  for (xj=0;xj<=m_y-1;xj++) {
   pdata[xi][xj]=0;
  };};
  while (true) {
   Flip();
   if (flip_cnt==0) break;
  };
 };
//=============
//============= step 2
 for (k=0;k<=1;k++) {
  for (xi=1;xi<=m_x-2;xi++) {
  for (xj=1;xj<=m_y-2;xj++) {
   if (cpdata[xi][xj]==1) {
    crx[0]=cpdata[xi-1][xj-1];
    crx[1]=cpdata[xi][xj-1];
    crx[2]=cpdata[xi+1][xj-1];
    crx[3]=cpdata[xi+1][xj];
    crx[4]=cpdata[xi+1][xj+1];
    crx[5]=cpdata[xi][xj+1];
    crx[6]=cpdata[xi-1][xj+1];
    crx[7]=cpdata[xi-1][xj];
    q=0;
    for (i=0;i<=7;i++) { if (crx[i]==1) q+=1; };
    p=0;
    for (i=0;i<=7;i++) {
     if ((crx[(i) % 8]==1) && (crx[(i+1) % 8]==0)) { p+=1;};
    };
    if (q==8) { p=1;};
    if ((q>2) && (p==1)) { cpdata[xi][xj]=0;};
   };
  };};
 };
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, int nCmdShow) 
{ 
 wcex.cbSize         = sizeof(WNDCLASSEX);
 wcex.style         = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc     = WndProc;
 wcex.cbClsExtra     = 0;
 wcex.cbWndExtra     = 0;
 wcex.hInstance     = hInstance;
 wcex.hIcon         = NULL;
 wcex.hCursor         = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
 wcex.lpszMenuName     = NULL;
 wcex.lpszClassName = szClassName;
 wcex.hIconSm         = NULL;
 RegisterClassEx(&wcex); 

 Main_hwnd = CreateWindow(szClassName, "MosaicAndFont", WS_OVERLAPPEDWINDOW, //WS_POPUP
   280, 220, 320, 300, NULL, NULL, hInstance, NULL);
 hwndEdit1=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","QWEY",
     WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
     5,200,300,25,Main_hwnd,0,hInstance,NULL);

HFONT hFont = CreateFont( 
                 15,                          /* lfHeight         */ 
                 10,                           /* lfWidth          */ 
                 0,                           /* lfEscapement     */ 
                 0,                           /* lfOrientation    */ 
                 10,                          /* lfWeight         */ 
                 FALSE,                       /* lfItalic         */ 
                 FALSE,                       /* lfUnderline      */ 
                 FALSE,                       /* lfStrikeOut      */ 
                 DEFAULT_CHARSET,                /* lfCharSet        */ 
                 OUT_DEFAULT_PRECIS,          /* lfOutPrecision   */ 
                 CLIP_DEFAULT_PRECIS,         /* lfClipPrecision  */ 
                 DEFAULT_QUALITY,             /* lfQuality        */ 
                 FIXED_PITCH | FF_MODERN,     /* lfPitchAndFamily */ 
                 "Lucida Console"                      /* lfFaceName       */ 
                 );
 SendMessage(hwndEdit1,WM_SETFONT,(WPARAM)hFont,0);
 MainProc = (WNDPROC)SetWindowLong(hwndEdit1, GWL_WNDPROC, (DWORD)Edit1WndProc);
 ShowWindow(Main_hwnd, nCmdShow); 
 UpdateWindow(Main_hwnd); 

 MSG msg; 
 while(GetMessage(&msg,NULL,0,0)) 
 { 
  TranslateMessage(&msg); 
  DispatchMessage(&msg); 
 } 
 return (int)msg.wParam;
} 

LRESULT CALLBACK Edit1WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam)
{ 
if (Message == WM_KEYDOWN) {
 if (wparam==VK_RETURN) {
  GetWindowText(hwndEdit1,editdata,5);
  myDC=GetDC(Main_hwnd);
  for (i=10;i<150;i++) { for (j=10;j<150;j++) {pdata[i][j]=1;};};
  HFONT textfont;
  textfont=CreateFont(50, 30, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET,0,0,0,0,"Lucida Console");
  SetBkColor(myDC,RGB(0,0,0));
  SetTextColor(myDC, RGB(0xff, 0xff, 0xff));
  HGDIOBJ hold;
  hold=SelectObject(myDC,textfont);
  TextOut(myDC,15,50,editdata,4);
  for (i=0;i<160;i++) { for (j=0;j<160;j++) {if (GetPixel(myDC,i,j)==RGB(0xff,0xff,0xff)) {pdata[i][j]=0;};};};
  SelectObject(myDC,hold);
  DeleteObject(textfont);
  for (i=0;i<1000;i++) {pdata[10+rand()*140/32768][10+rand()*140/32768]=0;};
  for (x=0;x<160;x++) {
  for (y=0;y<160;y++) {
   if (pdata[x][y]==0) {cpyimg[x][y]=0xffffff;} else {cpyimg[x][y]=0x0;};
  };};
  Skeletonization();

  long wp_x,wp_y,f,xi,xj,fnt_color,ib,rgb,my_r,my_g,my_b;
  fnt_color=0xff0000;
  float hue_color,sat_color;
  for (x=0;x<160;x++) {
  for (y=0;y<160;y++) {
   if (cpdata[x][y]==1) {orgimg[x][y]=0xffffff;} else {orgimg[x][y]=0x0;};
  };};

  while (true) {
  wp_x=0;
  wp_y=0;
  f=0;
  for (xi=0;xi<160;xi++) {
   for (xj=0;xj<160;xj++) {
    if ((cpyimg[xi][xj]==0xffffff) && (cpyimg[xi+1][xj]==0xffffff) && (cpyimg[xi+2][xj]==0xffffff) && (cpyimg[xi][xj+1]==0xffffff) && (cpyimg[xi+1][xj+1]==0xffffff) && (cpyimg[xi+2][xj+1]==0xffffff) && (cpyimg[xi][xj+2]==0xffffff) && (cpyimg[xi+1][xj+2]==0xffffff) && (cpyimg[xi+2][xj+2]==0xffffff)) {
     wp_x=xi;
     wp_y=xj;
     f=1;
     break;
    };
   };
   if (f==1) { break;};
  };
  
  if (f==1) {
   if ((wp_x==0) && (wp_y==0)) {
    my_stackPointer=0;
    my_floodFill4Stack(cpyimg,wp_x,wp_y,126,0xffffff);
    my_stackPointer=0;
    my_floodFill4Stack(orgimg,wp_x,wp_y,126,0);
   } else {
    my_stackPointer=0;
    my_floodFill4Stack(cpyimg,wp_x,wp_y,127,0xffffff);
    my_stackPointer=0;
    my_floodFill4Stack(orgimg,wp_x,wp_y,127,0);
   };
  } else break;
 };
 
 for (i=0;i<140;i++) {
 for (j=0;j<140;j++) {
  if (cpyimg[i+10][j+10]==0xffffff) { 
   my_stackPointer=0;
   my_floodFill4Stack(orgimg,i+10,j+10,rand()%127+128,0);
  };
 };};
 long my_x;
 hue_color=220.0;
 sat_color=50;
 for (i=0;i<140;i++) {
 for (j=0;j<140;j++) {
  my_x=(orgimg[i+10][j+10])&0xff;
  if (my_x>=128) {
   hsv_h=hue_color;
   hsv_s=sat_color/100;
   hsv_v=(float)2.0-(float)my_x/128;
   r=0;
   g=0;
   b=0;
   my_HSV2RGB();
   ib=((long)(b*255))&0xff;
   if (ib==127) { ib=128;};
   orgimg[i+10][j+10]=((((long)(r*255))&0xff)<<16)+((((long)(g*255))&0xff)<<8)+(ib);
  } else if (x<126) {
   orgimg[i+10][j+10]=0;
  };
 };};

 for (i=0;i<140;i++) {
 for (j=0;j<140;j++) {
  rgb = orgimg[i+10][j+10];
  my_r = (rgb >> 16) & 0xff;
  my_g = (rgb >> 8) & 0xff;
  my_b = (rgb) & 0xff;
  if (my_b==127) {
   SetPixel(myDC,i,j,RGB((fnt_color>>16)&0xff, (fnt_color>>8)&0xff, fnt_color&0xff));
  } else if (my_b==126) {
   SetPixel(myDC,i,j,0x0);
  } else {
   SetPixel(myDC,i,j,RGB(my_r,my_g,my_b));
  };
 };};
  ReleaseDC(Main_hwnd,myDC);
  return 0;
 };
};
 return CallWindowProc(MainProc, hwndEdit1, Message, wparam, lparam);
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam)
{
if (Message == WM_DESTROY ) 
{ 
  PostQuitMessage(0);
  return 0;
} else if (Message == WM_SETFOCUS){
 SetFocus(hwndEdit1);
};
 return DefWindowProc(hwnd,Message,wparam,lparam); 
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.