void VGLBitmapPutChar(VGLBitmap *Object, int x, int y, byte ch, u_long fgcol, u_long bgcol, int fill, int dir) { int b, Bpc, Bpl, lin, bit, topbit; Bpl = (VGLTextFont->Width + 7) / 8; Bpc = Bpl * VGLTextFont->Height; topbit = VGLTextFont->Width - 1; for(lin = 0; lin < VGLTextFont->Height; lin++) { for(bit = 0; bit < VGLTextFont->Width; bit++) { b = bit + (-VGLTextFont->Width & 7); if (VGLTextFont->BitmapArray[(ch*Bpc)+(lin*Bpl)+(b/8)]&(1<<(b%8))) switch (dir) { case 0: VGLSetXY(Object, (x+topbit-bit), (y+lin), fgcol); break; case 1: VGLSetXY(Object, (x+lin), (y-topbit+bit), fgcol); break; case 2: VGLSetXY(Object, (x-topbit+bit), (y-lin), fgcol); break; case 3: VGLSetXY(Object, (x-lin), (y+topbit-bit), fgcol); break; case 4: VGLSetXY(Object, (x+lin+topbit-bit), (y+lin+bit), fgcol); break; } else if (fill) switch (dir) { case 0: VGLSetXY(Object, (x+topbit-bit), (y+lin), bgcol); break; case 1: VGLSetXY(Object, (x+lin), (y-topbit+bit), bgcol); break; case 2: VGLSetXY(Object, (x-topbit+bit), (y-lin), bgcol); break; case 3: VGLSetXY(Object, (x-lin), (y+topbit-bit), bgcol); break; case 4: VGLSetXY(Object, (x+lin+topbit-bit), (y+lin+bit), bgcol); break; } } } }
void VGLBitmapPutChar(VGLBitmap *Object, int x, int y, byte ch, byte fgcol, byte bgcol, int fill, int dir) { int lin, bit; for(lin = 0; lin < VGLTextFont->Height; lin++) { for(bit = 0; bit < VGLTextFont->Width; bit++) { if (VGLTextFont->BitmapArray[((ch*VGLTextFont->Height)+lin)]&(1<<bit)) switch (dir) { case 0: VGLSetXY(Object, (x+7-bit), (y+lin), fgcol); break; case 1: VGLSetXY(Object, (x+lin), (y-7+bit), fgcol); break; case 2: VGLSetXY(Object, (x-7+bit), (y-lin), fgcol); break; case 3: VGLSetXY(Object, (x-lin), (y+7-bit), fgcol); break; case 4: VGLSetXY(Object, (x+lin+7-bit), (y+lin+bit), fgcol); break; } else if (fill) switch (dir) { case 0: VGLSetXY(Object, (x+7-bit), (y+lin), bgcol); break; case 1: VGLSetXY(Object, (x+lin), (y-7+bit), bgcol); break; case 2: VGLSetXY(Object, (x-7+bit), (y-lin), bgcol); break; case 3: VGLSetXY(Object, (x-lin), (y+7-bit), bgcol); break; case 4: VGLSetXY(Object, (x+lin+7-bit), (y+lin+bit), bgcol); break; } } } }
int png_load(char *filename) { int i,j,k; FILE *fd; u_char header[NUMBER]; png_structp png_ptr; png_infop info_ptr,end_info; png_uint_32 width,height; int bit_depth,color_type,interlace_type; int compression_type,filter_type; int channels,rowbytes; double gamma; png_colorp palette; int num_palette; png_bytep *row_pointers; char c; int res=0; fd=fopen(filename,"rb"); if(fd==NULL) { VGLEnd(); perror("fopen"); exit(1); } fread(header,1,NUMBER,fd); if(!png_check_sig(header,NUMBER)) { fprintf(stderr,"Not a PNG file.\n"); return(-1); } png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,(void *)NULL, NULL,NULL); info_ptr=png_create_info_struct(png_ptr); end_info=png_create_info_struct(png_ptr); if(!png_ptr || !info_ptr || !end_info) { VGLEnd(); fprintf(stderr,"failed to allocate needed structs!\n"); png_destroy_read_struct(&png_ptr,&info_ptr,&end_info); return(-1); } png_set_sig_bytes(png_ptr,NUMBER); png_init_io(png_ptr,fd); png_read_info(png_ptr,info_ptr); png_get_IHDR(png_ptr,info_ptr,&width,&height,&bit_depth, &color_type,&interlace_type,&compression_type,&filter_type); png_get_PLTE(png_ptr,info_ptr,&palette,&num_palette); channels=png_get_channels(png_ptr,info_ptr); rowbytes=png_get_rowbytes(png_ptr,info_ptr); if(bit_depth==16) png_set_strip_16(png_ptr); if(color_type & PNG_COLOR_MASK_ALPHA) png_set_strip_alpha(png_ptr); if(png_get_gAMA(png_ptr,info_ptr,&gamma)) png_set_gamma(png_ptr,screen_gamma,gamma); else png_set_gamma(png_ptr,screen_gamma,0.45); if(res==0) { /* Dither */ if(color_type & PNG_COLOR_MASK_COLOR) { if(png_get_valid(png_ptr,info_ptr,PNG_INFO_PLTE)) { png_uint_16p histogram; png_get_hIST(png_ptr,info_ptr,&histogram); png_set_dither(png_ptr,palette,num_palette,max_screen_colors,histogram,0); } else { png_color std_color_cube[16]={ {0x00,0x00,0x00}, {0x02,0x02,0x02}, {0x04,0x04,0x04}, {0x06,0x06,0x06}, {0x08,0x08,0x08}, {0x0a,0x0a,0x0a}, {0x0c,0x0c,0x0c}, {0x0e,0x0e,0x0e}, {0x10,0x10,0x10}, {0x12,0x12,0x12}, {0x14,0x14,0x14}, {0x16,0x16,0x16}, {0x18,0x18,0x18}, {0x1a,0x1a,0x1a}, {0x1d,0x1d,0x1d}, {0xff,0xff,0xff}, }; png_set_dither(png_ptr,std_color_cube,max_screen_colors,max_screen_colors,NULL,0); } } } png_set_packing(png_ptr); if(png_get_valid(png_ptr,info_ptr,PNG_INFO_sBIT)) { png_color_8p sig_bit; png_get_sBIT(png_ptr,info_ptr,&sig_bit); png_set_shift(png_ptr,sig_bit); } png_read_update_info(png_ptr,info_ptr); png_get_IHDR(png_ptr,info_ptr,&width,&height,&bit_depth, &color_type,&interlace_type,&compression_type,&filter_type); png_get_PLTE(png_ptr,info_ptr,&palette,&num_palette); channels=png_get_channels(png_ptr,info_ptr); rowbytes=png_get_rowbytes(png_ptr,info_ptr); row_pointers=malloc(height*sizeof(png_bytep)); for(i=0;i<height;i++) { row_pointers[i]=malloc(rowbytes); } png_read_image(png_ptr,row_pointers); png_read_end(png_ptr,end_info); png_destroy_read_struct(&png_ptr,&info_ptr,&end_info); fclose(fd); /* Set palette */ if(res) k=2; else k=2; for(i=0;i<256;i++) { pal_red[i]=255; pal_green[i]=255; pal_blue[i]=255; } for(i=0;i<num_palette;i++) { pal_red[i]=(palette+i)->red>>k; pal_green[i]=(palette+i)->green>>k; pal_blue[i]=(palette+i)->blue>>k; } pal_colors=num_palette; if(pic.Bitmap!=NULL) free(pic.Bitmap); pic.Bitmap=(byte *)calloc(rowbytes*height,sizeof(byte)); pic.Type=MEMBUF; pic.Xsize=rowbytes; pic.Ysize=height; for(i=0;i<rowbytes;i++) { for(j=0;j<height;j++) { VGLSetXY(&pic, i,j,row_pointers[j][i]); } } a.zoom=1; a.Xshift=(VGLDisplay->Xsize-pic.Xsize)/2; a.Yshift=(VGLDisplay->Ysize-pic.Ysize)/2; a.rotate=0; return(0); }
void display( VGLBitmap *pic, byte *red, byte *green, byte *blue, struct action *e) { VGLBitmap target; int x,y,i=0,j=0; VGLMouseMode(VGL_MOUSEHIDE); VGLRestorePalette(); /* XXX Broken in r!=2. Libvgl bug. */ //VGLClear(VGLDisplay,0); VGLBitmapCopy(&bkg,0,0,VGLDisplay,0,0,bkg.Xsize,bkg.Ysize); if(e!=NULL) { if(e->zoom!=1 || e->rotate) { target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize*e->zoom*e->zoom,1); if(e->rotate) { target.Xsize=pic->Ysize*e->zoom; target.Ysize=pic->Xsize*e->zoom; } else { target.Xsize=pic->Xsize*e->zoom; target.Ysize=pic->Ysize*e->zoom; } target.Type=pic->Type; for(x=0;x<pic->Xsize;x++) { for(y=0;y<pic->Ysize;y++) { for(i=0;i<e->zoom;i++) { for(j=0;j<e->zoom;j++) { if(e->rotate) { VGLSetXY(&target,target.Xsize-(e->zoom*y+i),e->zoom*x+j,VGLGetXY(pic,x,y)); } else { VGLSetXY(&target,e->zoom*x+i,e->zoom*y+j,VGLGetXY(pic,x,y)); } } } } } } else { target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize,sizeof(byte)); target.Xsize=pic->Xsize; target.Ysize=pic->Ysize; target.Type=pic->Type; VGLBitmapCopy(pic,0,0,&target,0,0,pic->Xsize,pic->Ysize); } } else { target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize,sizeof(byte)); target.Xsize=pic->Xsize; target.Ysize=pic->Ysize; target.Type=pic->Type; VGLBitmapCopy(pic,0,0,&target,0,0,pic->Xsize,pic->Ysize); } VGLSetPalette(red, green, blue); if(e!=NULL) { VGLBitmapCopy(&target,0,0,VGLDisplay,e->Xshift,e->Yshift,target.Xsize,target.Ysize); } else { VGLBitmapCopy(&target,0,0,VGLDisplay,0,0,target.Xsize,target.Ysize); } VGLMouseMode(VGL_MOUSESHOW); free(target.Bitmap); }
void VGLSetXY(VGLBitmap *object, int x, int y, u_long color) { int offset, soffset, undermouse; VGLCheckSwitch(); if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) { if (object == VGLDisplay) { undermouse = VGLMouseFreezeXY(x, y); VGLSetXY(&VGLVDisplay, x, y, color); } else if (object->Type != MEMBUF) return; /* invalid */ else undermouse = 0; if (!undermouse) { offset = (y * object->VXsize + x) * object->PixelBytes; switch (object->Type) { case VIDBUF8S: case VIDBUF16S: case VIDBUF32S: offset = VGLSetSegment(offset); /* FALLTHROUGH */ case MEMBUF: case VIDBUF8: case VIDBUF16: case VIDBUF24: case VIDBUF32: color = htole32(color); switch (object->PixelBytes) { case 1: memcpy(&object->Bitmap[offset], &color, 1); break; case 2: memcpy(&object->Bitmap[offset], &color, 2); break; case 3: memcpy(&object->Bitmap[offset], &color, 3); break; case 4: memcpy(&object->Bitmap[offset], &color, 4); break; } break; case VIDBUF24S: soffset = VGLSetSegment(offset); color = htole32(color); switch (VGLAdpInfo.va_window_size - soffset) { case 1: memcpy(&object->Bitmap[soffset], &color, 1); soffset = VGLSetSegment(offset + 1); memcpy(&object->Bitmap[soffset], (byte *)&color + 1, 2); break; case 2: memcpy(&object->Bitmap[soffset], &color, 2); soffset = VGLSetSegment(offset + 2); memcpy(&object->Bitmap[soffset], (byte *)&color + 2, 1); break; default: memcpy(&object->Bitmap[soffset], &color, 3); break; } break; case VIDBUF8X: outb(0x3c4, 0x02); outb(0x3c5, 0x01 << (x&0x3)); object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color); break; case VIDBUF4S: offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8); goto set_planar; case VIDBUF4: offset = y*VGLAdpInfo.va_line_width + x/8; set_planar: outb(0x3c4, 0x02); outb(0x3c5, 0x0f); outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */ outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */ outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */ object->Bitmap[offset] |= (byte)color; } } if (object == VGLDisplay) VGLMouseUnFreeze(); }