VDV-1469 (664230), страница 7
Текст из файла (страница 7)
}
PutGWL(ch, (short*)po, nx, i);
}
h_mfree(po);
if (chol) DisposeGW(chol);
ud = h_data(w);
ud->ch = ch;
ud->iw = nx; ud->ih = ny;
ud->x0 = (ud->iw-ud->w)/2; // Центровка
ud->y0 = (ud->ih-ud->h)/2; // изображения
redraw_window(w);
}
// зеркальное отображение
void Do_Mirrow(WinHandle w)
{ int i,j,vol;
int nx,ny;
int *po;
GWHandle ch;
Win2d *ud = h_data(w);
if ((ch=ud->ch) == 0) return;
nx = ud->iw; ny = ud->ih;
po = (int*)h_malloc(nx*sizeof(int));
for( i = 0; i < ny; i++)
{ GetGWL(ch, (short*)po, nx, i);
for(j=0;j { vol = po[j]; po[j] = po[nx-j-1]; po[nx-j-1] = vol; } PutGWL(ch, (short*)po, nx, i); } h_mfree(po); ud = h_data(w); ud->flef^=1; redraw_cw(w); } static void Do_ResZo(WinHandle w, int nxi, int nyi, int reszo, int type) { int i,k; int nx,ny,ix,iy,new_nx,new_ny,x0,y0; int *po; int q1,q2,q3,q4; double x,y,dx,dy; long cx,cy,cx1,cy1,s1,s2; double sc_x,sc_y,cox = 65536.0; long lx,ly,ldx,ldy,lix,liy; unsigned char *pc,s,p; IJ ij; GWHandle ch,chol; Rect r; Rect wr = WorkRect(w); Win2d *ud = h_data(w); if ((chol=ud->ch) == 0) return; if(reszo) { nx=nxi; ny=nyi; new_nx = ud->iw; new_ny = ud->ih; if(new_nx==nx && new_ny==ny) return; } else { r = type ? Set_Rect(w) : Set_Sqr(w); if(r.x0 == -1) return; new_nx = r.x1-r.x0+1; new_ny = r.y1-r.y0+1; ud = h_data(w); sc_x = (double)ud->iw/new_nx; sc_y = (double)ud->ih/new_ny; if( sc_x < sc_y) { nx = ud->iw; ny = new_ny*sc_x; if(ny%2) ny--; ud->scale = sc_x; } else { ny = ud->ih; nx = new_nx*sc_y; if(nx%2) nx--; ud->scale = sc_y; } } OnScale(wr.x0,wr.y0); dx = (double)new_nx/(double)nx; dy = (double)new_ny/(double)ny; ldx= cox*dx; ldy = cox*dy; po = (int*)h_malloc(nx*sizeof(int)); if(reszo) x0 = y0 = 0; else { ud = h_data(w); x0 = r.x0-wr.x0+ud->x0; y0 = r.y0-wr.y0+ud->y0; } ch = NewGW(f_create("rout.tmp",F_CREATE_NORMAL),0); ly = cox*y0; for( i = 0; i < ny; i++) { lx = cox*x0; ly+= ldy; iy = ly>>16; liy = iy; liy = liy << 16; cy = ly - liy; cy1= 65537L - cy; for(k = 0; k < nx; k++) { lx+= ldx; ix = lx>>16; ij.i = ix; ij.j = iy; GetGWA(chol, (short*)&q1, 1, &ij); ij.i = ix+1; ij.j = iy; GetGWA(chol, (short*)&q2, 1, &ij); ij.i = ix; ij.j = iy+1; GetGWA(chol, (short*)&q3, 1, &ij); ij.i = ix+1; ij.j = iy+1; GetGWA(chol, (short*)&q4, 1, &ij); lix = ix; lix = lix << 16; cx = lx - lix; cx1= 65537L - cx; s1 = ((long)q1*cx1+(long)q2*cx) >> 16; s1 = (s1*cy1)>>16; s2 = ((long)q3*cx1 + (long)q4*cx) >> 16; s2 = (s2*cy)>>16; po[k] = s1+s2; } PutGWL(ch, (short*)po, nx, i); DoneVisSave((int)((i+1)*100.0/ny+0.5)); } h_mfree(po); if (chol) DisposeGW(chol); ud = h_data(w); ud->ch = ch; ud->r0 = (double)ud->r0*(double)new_nx/ud->iw; ud->iw = nx; ud->ih = ny; ud->x0 = (ud->iw-ud->w)/2; // Центровка ud->y0 = (ud->ih-ud->h)/2; // изображения OffScale(); if(reszo) redraw_window(w); else { if (ud->size_big) SetRectShape(w,&CommonRect); else SetRectShape(w,&ud->own_size); redraw_cw(w); } } void Do_Zoom(WinHandle w, int type) { Do_ResZo(w,0,0,0,type); } void Do_Resemple(WinHandle w, int nx, int ny, int type) { Do_ResZo(w,nx,ny,1,type); } /***********************************************************/ typedef unsigned char BYTE; typedef unsigned int WORD; typedef unsigned long DWORD; typedef struct tagBITMAPFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { DWORD biSize; long biWidth; long biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; long biXPelsPerMeter; long biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER; void Fr_Save(WinHandle w, int select) { int i,k,x,y,x1,y1,nx,ny,format,ii,fin; BYTE rr,gg,bb; Rect r; BYTE *filp,*pallet,*pal; GCHandle ch; Win2d *ud = h_data(w); USGC *s; BITMAPFILEHEADER *hfile; BITMAPINFOHEADER *hinfo; long hi; Rect wr = WorkRect(w); int L = wr.x1 - wr.x0 + 1; int H = wr.y1 - wr.y0 + 1; if(ud->r0r0>10) hi = 100.0/(0.2*(double)ud->r0/(double)(ud->iw-1)); else hi = 0l; if(!ud->img_name || !ud->ch) return; // BringToFront(w); VisSetWin(w); // ????? format = 0; if(select == 0) {r = Set_Rect(w); if(r.x0 < 0) return; redraw_window(w); } else if(select == 1) {r.x0 = wr.x0; r.y0 = wr.y0; r.x1 = wr.x1; r.y1 = wr.y1;} else if(select == 2) { if((ud->iw > L) || (ud->ih > H)) {r.x0 = wr.x0; r.y0 = wr.y0; r.x1 = wr.x1; r.y1 = wr.y1;} else { r.x0 = wr.x0-ud->x0; r.y0 = wr.y0-ud->y0; r.x1 = r.x0 + ud->iw-1; r.y1 = r.y0 + ud->ih-1; } } else return; nx = r.x1-r.x0+1; ny = r.y1-r.y0+1; if(nx%4) { nx-=nx%4; r.x0++; } if(ny%2) { ny--; r.y0++; } i = InputForm(mesman,&format); if (i != 1) return; s = h_malloc(4096); if(!s) return; if(format == 2) // BMP save { hfile =h_malloc(sizeof(BITMAPFILEHEADER)); hinfo =h_malloc(sizeof(BITMAPINFOHEADER)); // Select name and open BMP file filp = GetFileName(GetCfgItem("bmp"),H_CREATE); if (*filp == 0) {h_mfree(s); h_mfree(hfile); h_mfree(hinfo); return;} if ((fin = f_create(filp, F_CREATE_NORMAL)) == -1) {h_mfree(s); h_mfree(hfile); h_mfree(hinfo); return;} // Save BITMAPFILEHEADER hfile->bfType = 0x4D42; // BM - символы идентифицирующие формат hfile->bfSize = 0l; // размер файла в long-ах hfile->bfReserved1 = 0; hfile->bfReserved2 = 0; hfile->bfOffBits = sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+1024L; // смещение начала собственно битового массива в байтах f_write(fin, hfile, sizeof(BITMAPFILEHEADER)); // Save BITMAPIMFOHEADER hinfo->biSize = (long)sizeof(BITMAPINFOHEADER); hinfo->biWidth = (long)nx; // кратно long-ам (если надо, добавить нули) hinfo->biHeight = (long)ny; hinfo->biPlanes = 1; hinfo->biBitCount = 8; hinfo->biCompression = 0L; hinfo->biSizeImage = 0L; hinfo->biXPelsPerMeter = hi; hinfo->biYPelsPerMeter = hi; hinfo->biClrUsed = 0L; // к-во цветов в таблице цветности hinfo->biClrImportant = 0L; // обязательное к-во цветов f_write(fin, hinfo, sizeof(BITMAPINFOHEADER)); // Save PALLETE pallet = h_malloc(1024); pal = pallet; for(ii=0; ii<256; ii++) { g_get_pal((USGC)ii,&rr,&gg,&bb); *pal++ = rr*4; *pal++ = gg*4; *pal++ = bb*4; *pal++ = 0; } f_write(fin, pallet, 1024); h_mfree(pallet); // Save BITMAP for(y=r.y1; y>=r.y0; y--) { for(x=r.x0,k=0; x<=r.x1; x++,k++) { s[k] = g_point(x, y, 0); g_point(x, y, s[k]);} f_write(fin, s, nx); } f_close(fin); h_mfree(hfile); h_mfree(hinfo); } else if(format == 0) // IMG save { filp = GetFileName(GetCfgItem("frag"),H_CREATE); if (*filp == 0) { h_mfree(s); return;} if ((fin = f_create(filp, F_CREATE_NORMAL)) == -1) {h_mfree(s); return;} f_write(fin, &nx, sizeof(int)); f_write(fin, &ny, sizeof(int)); for(y=r.y0; y<=r.y1; y++) { for(x=r.x0,k=0; x<=r.x1; x++,k++) { s[k] = g_point(x, y, 0); g_point(x, y, s[k]); } f_write(fin, s, nx); } f_close(fin); ShowCursor(x,y,CURT_HAND); } else if(format == 1) // PCX save { if ((fin = f_create("frag.tmp", F_CREATE_NORMAL)) == -1) {h_mfree(s); return;} ch = NewGC(fin,0); pallet = h_malloc(768); for(y=r.y0,i = 0; y<=r.y1; y++,i++) { for(x=r.x0,k=0; x<=r.x1; x++,k++) { s[k] = g_point(x, y, 0); g_point(x, y, s[k]);} PutGCL(ch, s, nx, i); } pal = pallet; for(ii=0; ii<256; ii++) { g_get_pal((USGC)ii,&rr,&gg,&bb); *pal++ = rr*4; *pal++ = gg*4; *pal++ = bb*4; } filp = GetFileName(GetCfgItem("pcx"),H_CREATE); if (*filp) cell_to_pcx(filp, ch, nx, ny, pallet); h_mfree(pallet); DisposeGC(ch); f_delete("frag.tmp"); } h_mfree(s); } void PCX_Save(WinHandle w) { int i,k,x,y,x1,y1,nx,ny,ii,fin; USGC rr,gg,bb; Rect r; USGC *filp,*pallet,*pal; GCHandle ch; Win2d *ud; USGC *s,*s1,name[13],ss[81]; Rect wr = WorkRect(w); ud = h_data(w); if(!ud->img_name || !ud->ch) return; r.x0 = wr.x0; r.y0 = wr.y0; r.x1 = wr.x1; r.y1 = wr.y1; nx = r.x1-r.x0+1; ny = r.y1-r.y0+1; if(nx%2) { nx--; r.x0++; } if(ny%2) { ny--; r.y0++; } s = h_malloc(1024); if(!s) return; // PCX save if ((fin = f_create("frag.tmp", F_CREATE_NORMAL)) == -1) {h_mfree(s); return;} ch = NewGC(fin,0); pallet = h_malloc(768); for(y=r.y0,i = 0; y<=r.y1; y++,i++) { for(x=r.x0,k=0; x<=r.x1; x++,k++) { s[k] = g_point(x, y, 0); g_point(x, y, s[k]);} PutGCL(ch, s, nx, i); } pal = pallet; for(ii=0; ii<256; ii++) { g_get_pal((USGC)ii,&rr,&gg,&bb); *pal++ = rr*4; *pal++ = gg*4; *pal++ = bb*4; } strcpy(ss,GetCfgItem("pcx")); if(*ss != 0) { s1=strrchr(ss,'\\'); if(!s1) return ; else { s1++; *s1=0; } } ud = h_data(w); sprintf(name,"%ld-%d.pcx",ud->no,ud->ns); strcat(ss,name); if (*ss) cell_to_pcx(ss, ch, nx, ny, pallet); h_mfree(pallet); DisposeGC(ch); f_delete("frag.tmp"); h_mfree(s); } void Fr_Load(WinHandle w) { GWHandle chl; int i,k,x,y,x1,y1,nx,ny; Rect r; EventRecord e; char name[81],s[81],*s1; int *pi; unsigned char *c,*pc,*p,*pp; int fin; char *filp; Win2d *ud = h_data(w); filp = GetFileName(GetCfgItem("frag"),H_READ); if (*filp == 0) return; if ((fin = f_open(filp, F_OPEN_READ)) == -1) return; f_read(fin, &nx, sizeof(int)); f_read(fin, &ny, sizeof(int)); get_ct_handle(w, ct); get_ctsys(); ct->l[2] = nx; ct->l[3] = ny; ct->b[249] = 1; ud = h_data(w); ud->iw = nx; ud->ih = ny; ud->ps = 1; ud->frag = 1; ud->objtype = 0; // GW file name definition strcpy(name,GetCfgItem("image")); pc = name+strlen(name); while (pc > name && pc[-1] != '\\') pc--; sprintf(pc,"w%d.tmp",ud->temp_name); if (ud->ch) { DisposeGW(ud->ch); f_delete(name); } ud->ch = NewGW(f_create(name,F_CREATE_NORMAL),0); chl = ud->ch; p = h_malloc(nx); pi = (int*)h_malloc(nx*2); for( k = 0; k < ny; k++) {f_read(fin, p, nx); for (i = 0; i < nx; i++) pi[i] = (int)p[i]+2048; PutGWL(chl,(short*)pi,nx,k); // Write to GW file } h_mfree(p); h_mfree(pi); VisSetWin(w); strcpy(s,filp); ud = h_data(w); if(!ud->img_old && ud->img_name) { int len = strlen(h_data(ud->img_name)); ud->img_old = h_alloc(len+1); ud = h_data(w); strcpy(h_data(ud->img_old),h_data(ud->img_name)); } if(ud->img_name) { if (ud->tempimg) f_delete(h_data(ud->img_name)); h_free(ud->img_name); } else h_free(ud->win.title); ud = h_data(w); ud->img_name = h_alloc(strlen(s)+1); ud = h_data(w); strcpy(h_data(ud->img_name),s); ud->tempimg = 0; ud->win.title = ud->img_name; PaintTitle(w); ud->x0 = (ud->iw-ud->w)/2; // Центровка ud->y0 = (ud->ih-ud->h)/2; // изображения redraw_window(w); } 94Приложение 3
Описание программы обработки изображений Ctsoft