Форматы Хранения Графических данных, страница 4

yout[0] = yin2[0];

yout[1] = yin2[1];

//сохр изображения в файл

void SaveImage(HWND hWnd)

{     char FilePath[1024];

if (SelectOpenFile(hWnd, FilePath, sizeof(FilePath)))

{

int /*x, y, x1, y1, i1, i2,*/i,j, a, c, xR, yT, yB, xL, m;

int polysx[MAXPOINTS*2][2], polysy[MAXPOINTS*2][2], ip[2];

int lnx[2], lny[2], lcx[2], lcy[2];

//обрезать координаты, не попадающие в выделение

xR = IsSelection ? XRight-XLeft: 0xffff;

if(xR < 0) xR = -xR;

xL = IsSelection ? (XLeft > XRight ? XRight: XLeft ): 0;

yT = IsSelection ? (YTop > YBottom ? YBottom: YTop ) : 0;

yB = IsSelection ? YBottom-YTop : 0xffff;

if(yB < 0) yB = -yB;

fp = fopen(FilePath, "wb");

if(!fp){MessageBox(NULL, "Failed to open file", "", MB_OK);return;}

PICHeader();

a = (FillColor & 0xff)     > 0xbf     ? 1 : 0;

a|= (FillColor & 0xff00)   > 0xbf00   ? 1 : 0;

a|= (FillColor & 0xff0000) > 0xbf0000 ? 1 : 0;

c = (FillColor & 0xff)     > 0x7f     ? 1 : 0;

c|= (FillColor & 0xff00)   > 0x7f00   ? 2 : 0;

c|= (FillColor & 0xff0000) > 0x7f0000 ? 4 : 0;

a = a >= 0 ? 0x8 : 0;

PICColor(a | c);

for(j=0; j<2; j++)

m = 1;

{     for(i=1;i<PointsCnt[j];i++)

{     if(m){

lnx[0] = (PolysX[i-1][j] - xL);

lny[0] = (PolysY[i-1][j] - yT);

}else

{

lnx[0] = lnx[1];

lny[0] = lny[1];

}

lnx[1] = (PolysX[i][j] - xL);

lny[1] = (PolysY[i][j] - yT);

if( ClipLine(lnx, lny, lcx, lcy, xR, yB) )

{     if( m ){PICMove(lcx[0], lcy[0]);}

if( lcx[1] != lnx[1] || lcy[1] != lny[1] ){m=1;}

else m=0;

PICDraw(lcx[1], lcy[1]);

}

}//for(i)

if(IsComplete[j])

{     lnx[0] = (PolysX[i][j] - xL);

lny[0] = (PolysY[i][j] - yT);

lnx[1] = (PolysX[0][j] - xL);

lny[1] = (PolysY[0][j] - yT);

if( ClipLine(lnx, lny, lcx, lcy, xR, yB) )

{

if( lcx[0] != lnx[0] || lcy[0] != lny[0] )PICMove(lcx[0], lcy[0]);

PICDraw(lcx[1], lcy[1]);

}

}

}

PICEnd();

fclose(fp);

}

}

void OpenImage(HWND hWnd)

{

char FilePath[1024];

if (SelectLoadFile(hWnd, FilePath, sizeof(FilePath)))

{

struct Rect r;

fp = fopen(FilePath, "rb");

if(!fp){MessageBox(NULL, "Failed to open file", "", MB_OK);return;}

/* Считываем размеры изображения.*/

PICGetSize(FilePath,&r);

/* Отрисовываем все изображение */

/* на эхране полностью.*/

PICWrite (FilePath, &r);

InvalidateRect(hWnd, NULL, NULL);

}

}

int sign(int n)

{     if (n > 0)

return 1;

else  if (n < 0)

return -1;

else

return 0;

}

void PutPixel(HDC, int, int, bool);

void Line(HDC hDC, int x0, int y0, int x1, int y1)

{

int res_x = CONSTA / 2, res_y = CONSTA / 2;

int x = x0, y = y0;

int dx = x1 - x0, dy = y1 - y0;

int ex = sign(dx), ey = sign(dy);

PutPixel(hDC, x, y, true);

while (!(x == x1 && y == y1))

{

res_x += dx;

if (res_x >= CONSTA || res_x < 0)

{

x += ex;

PutPixel(hDC, x, y, true);

res_x -= ex * CONSTA;

}

res_y += dy;

if (res_y >= CONSTA || res_y < 0)

{

y += ey;

PutPixel(hDC, x, y, true);

res_y -= ey * CONSTA;

}

}

}

void PutPixel(HDC hDC, int x, int y, bool IsFilled)

{

if(PixelSize==1){SetPixel(hDC, x, y, FillColor);return;}

RECT r;

HBRUSH hbr = CreateSolidBrush(FillColor);

HRGN rgn;

r.left = PixelSize * x + 1;

r.top = PixelSize * y + 1;

r.right = r.left + PixelSize - 1;

r.bottom = r.top + PixelSize - 1;

if (IsPixelBox)

rgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);

else

rgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);

PaintRgn(hDC, rgn);

FrameRgn(hDC, rgn, hbr, 1, 1);