static GImage *ColorWheel(int width,int height) { struct hslrgb col; GImage *wheel; struct _GImage *base; int i,j; double x,y, hh, hw; uint32 *row; memset(&col,0,sizeof(col)); col.v = 1.0; if ( width<10 ) width = 10; if ( height<10 ) height = 10; hh = height/2.0; hw = width/2.0; wheel = GImageCreate(it_true,width,height); base = wheel->u.image; for ( i=0; i<height; ++i ) { row = (uint32 *) (base->data + i*base->bytes_per_line); y = (i-hh)/(hh-1); for ( j=0; j<width; ++j ) { x = (j-hw)/(hw-1); col.s = sqrt(x*x + y*y); if ( col.s>1.0 ) { *row++ = 0xffffff; } else { col.h = atan2(y,x)*180/3.1415926535897932; if ( col.h < 0 ) col.h += 360; gHSV2RGB(&col); *row++ = COLOR_CREATE( (int) rint(255*col.r), (int) rint(255*col.g), (int) rint(255*col.b)); } } } return( wheel ); }
int BCExportXBM(char *filename,BDFChar *bdfc, int format) { struct _GImage base; GImage gi; GClut clut; int ret; int tot; int scale, i; uint8 *pt, *end; memset(&gi,'\0', sizeof(gi)); memset(&base,'\0', sizeof(base)); gi.u.image = &base; if ( !bdfc->byte_data ) { BCRegularizeBitmap(bdfc); /* Sigh. Bitmaps use a different defn of set than images do. make it consistant */ tot = bdfc->bytes_per_line*(bdfc->ymax-bdfc->ymin+1); for ( pt = bdfc->bitmap, end = pt+tot; pt<end; *pt++ ^= 0xff ); base.image_type = it_mono; base.data = bdfc->bitmap; base.bytes_per_line = bdfc->bytes_per_line; base.width = bdfc->xmax-bdfc->xmin+1; base.height = bdfc->ymax-bdfc->ymin+1; base.trans = -1; if ( format==0 ) ret = !GImageWriteXbm(&gi,filename); #ifndef _NO_LIBPNG else if ( format==2 ) ret = GImageWritePng(&gi,filename,false); #endif else ret = GImageWriteBmp(&gi,filename); /* And back to normal */ for ( pt = bdfc->bitmap, end = pt+tot; pt<end; *pt++ ^= 0xff ); } else { BCRegularizeGreymap(bdfc); base.image_type = it_index; base.data = bdfc->bitmap; base.bytes_per_line = bdfc->bytes_per_line; base.width = bdfc->xmax-bdfc->xmin+1; base.height = bdfc->ymax-bdfc->ymin+1; base.clut = &clut; clut.clut_len = 1<<bdfc->depth; clut.is_grey = true; clut.trans_index = base.trans = -1; scale = 255/((1<<bdfc->depth)-1); scale = COLOR_CREATE(scale,scale,scale); for ( i=0; i< 1<<bdfc->depth; ++i ) clut.clut[(1<<bdfc->depth)-1 - i] = i*scale; #ifndef _NO_LIBPNG if ( format==2 ) ret = GImageWritePng(&gi,filename,false); else #endif ret = GImageWriteBmp(&gi,filename); } return( ret ); }
/* jpeg routines use 24 bit pixels, xvt routines pad out to 32 */ static void transferBufferToImage(struct jpegState *js,int ypos) { struct jpeg_decompress_struct *cinfo = js->cinfo; JSAMPLE *pt, *end; Color *ppt; ppt = (Color *) (js->base->data+ypos*js->base->bytes_per_line); for ( pt = js->buffer, end = pt+3*cinfo->image_width; pt<end; ) { register int r,g,b; r = *(pt++); g= *(pt++); b= *(pt++); *(ppt++) = COLOR_CREATE(r,g,b); } }
GImage * GImageReadTiff (char *filename) { TIFF *tif; uint32_t w, h, i, j; uint32_t *ipt, *fpt; size_t npixels; uint32 *raster; GImage *ret = NULL; struct _GImage *base; tif = TIFFOpen (filename, "r"); if (tif == NULL) return (ret); TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &h); npixels = w * h; raster = (uint32_t *) xmalloc (szmax (1, npixels * sizeof (uint32_t))); if (raster != NULL) { if (TIFFReadRGBAImage (tif, w, h, raster, 0)) { ret = GImageCreate (it_true, w, h); if (ret != NULL) { base = ret->u.image; for (i = 0; i < h; ++i) { ipt = (uint32_t *) (base->data + i * base->bytes_per_line); fpt = raster + (h - 1 - i) * w; for (j = 0; j < w; ++j) *ipt++ = COLOR_CREATE (TIFFGetR (fpt[j]), TIFFGetG (fpt[j]), TIFFGetB (fpt[j])); } } } free (raster); } TIFFClose (tif); return (ret); }
static GImage *Gradient(int height) { GImage *grad; struct _GImage *base; int i,j,c; uint32 *row; if ( height<10 ) height = 10; grad = GImageCreate(it_true,GRAD_WIDTH,height); base = grad->u.image; for ( i=0; i<height; ++i ) { row = (uint32 *) (base->data + i*base->bytes_per_line); c = 255*(height-1-i)/(height-1); for ( j=0; j<GRAD_WIDTH; ++j ) { *row++ = COLOR_CREATE(c,c,c); } } return( grad ); }
GImage * GImageReadRgb (char *filename) { FILE *fp = fopen (filename, "rb"); struct sgiheader header; int j, i; unsigned char *pt, *end; unsigned long *ipt, *iend; GImage *ret; struct _GImage *base; if (fp == NULL) return (NULL); getsgiheader (&header, fp); if (header.magic != SGI_MAGIC || (header.format != VERBATIM && header.format != RLE) || (header.bpc != 1 && header.bpc != 2) || header.dim < 1 || header.dim > 3 || header.pixmax > 65535 || (header.pixmax > 255 && header.bpc == 1) || (header.chans != 1 && header.chans != 3 && header.chans != 4) || header.pixmax < 0 || header.pixmin < 0 || header.pixmin > header.pixmax || header.colormap != 0) { fclose (fp); return (NULL); } ret = GImageCreate (header.dim == 3 ? it_true : it_index, header.width, header.height); base = ret->u.image; if (header.format == RLE) { unsigned long *starttab /*, *lengthtab */ ; unsigned char **ptrtab; long tablen; tablen = header.height * header.chans; starttab = (unsigned long *) xmalloc (szmax (1, tablen * sizeof (long))); /*lengthtab = (unsigned long *)xmalloc(szmax(1, tablen*sizeof(long))); */ ptrtab = (unsigned char **) xmalloc (szmax (1, tablen * sizeof (unsigned char *))); readlongtab (fp, starttab, tablen); /*readlongtab(fp,lengthtab,tablen); */ for (i = 0; i < tablen; ++i) find_scanline (fp, &header, i, starttab, ptrtab); if (header.chans == 1) { for (i = 0; i < header.height; ++i) memcpy (base->data + (header.height - 1 - i) * base->bytes_per_line, ptrtab[i], header.width); } else { unsigned long *ipt; for (i = 0; i < header.height; ++i) { ipt = (unsigned long *) (base->data + (header.height - 1 - i) * base->bytes_per_line); for (j = 0; j < header.width; ++j) *ipt++ = COLOR_CREATE (ptrtab[i][j], ptrtab[i + header.height][j], ptrtab[i + 2 * header.height][j]); } } freeptrtab (ptrtab, tablen); free (ptrtab); free (starttab); /*free(lengthtab); */ } else { if (header.chans == 1 && header.bpc == 1) { for (i = 0; i < header.height; ++i) { fread (base->data + (header.height - 1 - i) * base->bytes_per_line, header.width, 1, fp); if (header.pixmax != 255) { pt = (unsigned char *) (base->data + (header.height - 1 - i) * base->bytes_per_line); for (end = pt + header.width; pt < end; ++pt) *pt = (*pt * 255) / header.pixmax; } } } else if (header.chans == 1) { for (i = 0; i < header.height; ++i) { pt = (unsigned char *) (base->data + (header.height - 1 - i) * base->bytes_per_line); for (end = pt + header.width; pt < end;) *pt++ = (getshort (fp) * 255L) / header.pixmax; } } else if (header.bpc == 1) { unsigned char *r, *g, *b, *a = NULL; unsigned char *rpt, *gpt, *bpt; r = (unsigned char *) xmalloc (header.width); g = (unsigned char *) xmalloc (header.width); b = (unsigned char *) xmalloc (header.width); if (header.chans == 4) a = (unsigned char *) xmalloc (header.width); for (i = 0; i < header.height; ++i) { fread (r, header.width, 1, fp); fread (g, header.width, 1, fp); fread (b, header.width, 1, fp); if (header.chans == 4) fread (a, header.width, 1, fp); ipt = (unsigned long *) (base->data + (header.height - 1 - i) * base->bytes_per_line); rpt = r; gpt = g; bpt = b; for (iend = ipt + header.width; ipt < iend;) *ipt++ = COLOR_CREATE (*rpt++ * 255L / header.pixmax, *gpt++ * 255L / header.pixmax, *bpt++ * 255L / header.pixmax); } free (r); free (g); free (b); free (a); } else { unsigned char *r, *g, *b, *a = NULL; unsigned char *rpt, *gpt, *bpt; r = (unsigned char *) xmalloc (header.width); g = (unsigned char *) xmalloc (header.width); b = (unsigned char *) xmalloc (header.width); if (header.chans == 4) a = (unsigned char *) xmalloc (header.width); for (i = 0; i < header.height; ++i) { for (j = 0; j < header.width; ++j) r[j] = getshort (fp) * 255L / header.pixmax; for (j = 0; j < header.width; ++j) g[j] = getshort (fp) * 255L / header.pixmax; for (j = 0; j < header.width; ++j) b[j] = getshort (fp) * 255L / header.pixmax; if (header.chans == 4) { fread (a, header.width, 1, fp); fread (a, header.width, 1, fp); } ipt = (unsigned long *) (base->data + (header.height - 1 - i) * base->bytes_per_line); rpt = r; gpt = g; bpt = b; for (iend = ipt + header.width; ipt < iend;) *ipt++ = COLOR_CREATE (*rpt++, *gpt++, *bpt++); } free (r); free (g); free (b); free (a); } } return (ret); }
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <basics.h> #include <stdlib.h> #include "utype.h" #include "gdraw.h" #include "ggadgetP.h" #include "gresource.h" #include "gwidget.h" #include "gkeysym.h" #include "ustring.h" GBox _ggadget_Default_Box = { bt_raised, bs_rect, 2, 2, 0, 0, COLOR_CREATE(0xd8,0xd8,0xd8), /* border left */ /* brightest */ COLOR_CREATE(0xd0,0xd0,0xd0), /* border top */ COLOR_CREATE(0x80,0x80,0x80), /* border right */ COLOR_CREATE(0x66,0x66,0x66), /* border bottom */ /* darkest */ COLOR_DEFAULT, /* normal background */ COLOR_DEFAULT, /* normal foreground */ COLOR_CREATE(0xd8,0xd8,0xd8), /* disabled background */ COLOR_CREATE(0x66,0x66,0x66), /* disabled foreground */ COLOR_CREATE(0xff,0xff,0x00), /* active border */ COLOR_CREATE(0xa0,0xa0,0xa0), /* pressed background */ COLOR_CREATE(0x00,0x00,0x00), /* gradient bg end */ COLOR_CREATE(0x00,0x00,0x00), /* border inner */ COLOR_CREATE(0x00,0x00,0x00), /* border outer */ }; GBox _GListMark_Box = GBOX_EMPTY; /* Don't initialize here */ FontInstance *_ggadget_default_font = NULL;
int ExportImage(char *filename,SplineChar *sc, int layer, int format, int pixelsize, int bitsperpixel) { /* 0=*.xbm, 1=*.bmp, 2=*.png, 3=*.xpm, 4=*.c(fontforge-internal) */ struct _GImage base; GImage gi; GClut clut; BDFChar *bdfc; int ret; int tot, i; uint8 *pt, *end; int scale; void *freetypecontext; double emsize = sc->parent->ascent+sc->parent->descent; if ( autohint_before_generate && sc->changedsincelasthinted && !sc->manualhints ) SplineCharAutoHint(sc,layer,NULL); memset(&gi,'\0', sizeof(gi)); memset(&base,'\0', sizeof(base)); memset(&clut,'\0', sizeof(clut)); gi.u.image = &base; if ( bitsperpixel==1 ) { if ( (freetypecontext = FreeTypeFontContext(sc->parent,sc,NULL,layer))==NULL ) bdfc = SplineCharRasterize(sc,layer,pixelsize); else { bdfc = SplineCharFreeTypeRasterize(freetypecontext,sc->orig_pos,pixelsize,72,1); FreeTypeFreeContext(freetypecontext); } BCRegularizeBitmap(bdfc); /* People don't seem to like having a minimal bounding box for their */ /* images. */ BCExpandBitmapToEmBox(bdfc, 0, (int) rint(sc->parent->ascent*pixelsize/emsize)-pixelsize, (int) rint(sc->width*pixelsize/emsize), (int) rint(sc->parent->ascent*pixelsize/emsize)); /* Sigh. Bitmaps use a different defn of set than images do. make it consistant */ tot = bdfc->bytes_per_line*(bdfc->ymax-bdfc->ymin+1); for ( pt = bdfc->bitmap, end = pt+tot; pt<end; *pt++ ^= 0xff ); base.image_type = it_mono; base.data = bdfc->bitmap; base.bytes_per_line = bdfc->bytes_per_line; base.width = bdfc->xmax-bdfc->xmin+1; base.height = bdfc->ymax-bdfc->ymin+1; base.trans = -1; if ( format==0 ) ret = !GImageWriteXbm(&gi,filename); #ifndef _NO_LIBPNG else if ( format==2 ) ret = GImageWritePng(&gi,filename,false); #endif else if ( format==3 ) ret = !GImageWriteXpm(&gi,filename); else if ( format==4 ) ret = !GImageWriteGImage(&gi,filename); else ret = GImageWriteBmp(&gi,filename); BDFCharFree(bdfc); } else { if ( (freetypecontext = FreeTypeFontContext(sc->parent,sc,NULL,layer))==NULL ) bdfc = SplineCharAntiAlias(sc,pixelsize,layer,(1<<(bitsperpixel/2))); else { bdfc = SplineCharFreeTypeRasterize(freetypecontext,sc->orig_pos,pixelsize,72,bitsperpixel); FreeTypeFreeContext(freetypecontext); } BCRegularizeGreymap(bdfc); BCExpandBitmapToEmBox(bdfc, 0, (int) rint(sc->parent->ascent*pixelsize/emsize) - pixelsize, (int) rint(sc->width*pixelsize/emsize), (int) rint(sc->parent->ascent*pixelsize/emsize)); base.image_type = it_index; base.data = bdfc->bitmap; base.bytes_per_line = bdfc->bytes_per_line; base.width = bdfc->xmax-bdfc->xmin+1; base.height = bdfc->ymax-bdfc->ymin+1; base.clut = &clut; base.trans = -1; clut.clut_len = 1<<bitsperpixel; clut.is_grey = true; clut.trans_index = -1; scale = 255/((1<<bitsperpixel)-1); scale = COLOR_CREATE(scale,scale,scale); for ( i=0; i< 1<<bitsperpixel; ++i ) clut.clut[(1<<bitsperpixel)-1 - i] = i*scale; #ifndef _NO_LIBPNG if ( format==2 ) ret = GImageWritePng(&gi,filename,false); else #endif ret = GImageWriteBmp(&gi,filename); BDFCharFree(bdfc); } return( ret ); }
static int fillbmpheader(FILE *file,struct bmpheader *head) { int i; memset(head,'\0',sizeof(*head)); if ( getc(file)!='B' || getc(file)!='M' ) return 0; /* Bad format */ head->size = getl(file); head->mbz1 = getshort(file); head->mbz2 = getshort(file); head->offset = getl(file); head->headersize = getl(file); if ( head->headersize==12 ) { /* Windows 2.0 format, also OS/2 */ head->width = getshort(file); head->height = getshort(file); head->planes = getshort(file); head->bitsperpixel = getshort(file); head->colorsused = 0; head->compression = 0; } else { head->width = getl(file); head->height = getl(file); head->planes = getshort(file); head->bitsperpixel = getshort(file); head->compression = getl(file); head->imagesize = getl(file); head->ignore1 = getl(file); head->ignore2 = getl(file); head->colorsused = getl(file); head->colorsimportant = getl(file); } if ( head->height<0 ) head->height = -head->height; else head->invert = true; if ( head->bitsperpixel!=1 && head->bitsperpixel!=4 && head->bitsperpixel!=8 && head->bitsperpixel!=16 && head->bitsperpixel!=24 && head->bitsperpixel!=32 ) return( 0 ); if ( head->compression==3 && ( head->bitsperpixel==16 || head->bitsperpixel==32 )) /* Good */; else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 )) /* Good */; else if ( head->compression==1 && head->bitsperpixel==8 ) /* Good */; else if ( head->compression==2 && head->bitsperpixel==4 ) /* Good */; else return( 0 ); if ( head->colorsused==0 ) head->colorsused = 1<<head->bitsperpixel; if ( head->bitsperpixel>=16 ) head->colorsused = 0; if ( head->colorsused>(1<<head->bitsperpixel) ) return( 0 ); for ( i=0; i<head->colorsused; ++i ) { int b = getc(file), g = getc(file), r=getc(file); head->clut[i] = COLOR_CREATE(r,g,b); if ( head->headersize!=12 ) getc(file); } if ( head->compression==3 || head->headersize==108 ) { head->red_mask = getl(file); head->green_mask = getl(file); head->blue_mask = getl(file); head->red_shift = bitshift(head->red_mask); head->green_shift = bitshift(head->green_mask); head->blue_shift = bitshift(head->blue_mask); } if ( head->headersize==108 ) { getl(file); /* alpha_mask */ getl(file); /* color space type */ getl(file); /* redx */ getl(file); /* redy */ getl(file); /* redz */ getl(file); /* greenx */ getl(file); /* greeny */ getl(file); /* greenz */ getl(file); /* bluex */ getl(file); /* bluey */ getl(file); /* bluez */ getl(file); /* gammared */ getl(file); /* gammagreen */ getl(file); /* gammablue */ } return( 1 ); }
static int fillbmpheader(FILE *fp,struct bmpheader *head) { /* Get BMPheader info. Return 0 if read the header okay, -1 if read error */ int i; long temp; if ( fgetc(fp)!='B' || getc(fp)!='M' || /* Bad format */ \ getlong(fp,&head->size) || \ (head->mbz1=getshort(fp))<0 || \ (head->mbz2=getshort(fp))<0 || \ getlong(fp,&head->offset) || \ getlong(fp,&head->headersize) ) return( -1 ); if ( head->headersize==12 ) { /* Windows 2.0 format, also OS/2 */ if ( (head->width=getshort(fp))<0 || \ (head->height=getshort(fp))<0 || \ (head->planes=getshort(fp))<0 || \ (head->bitsperpixel=getshort(fp))<0 ) return( -1 ); //head->colorsused=0; //head->compression=0; } else { if ( getlong(fp,&head->width) || \ getlong(fp,&head->height) || \ (head->planes=getshort(fp))<0 || \ (head->bitsperpixel=getshort(fp))<0 || \ getlong(fp,&head->compression) || \ getlong(fp,&head->imagesize) || \ getlong(fp,&head->ignore1) || \ getlong(fp,&head->ignore2) || \ getlong(fp,&head->colorsused) || \ getlong(fp,&head->colorsimportant) ) return( -1 ); } if ( head->height<0 ) head->height = -head->height; else head->invert = true; if ( head->bitsperpixel!=1 && head->bitsperpixel!=4 && head->bitsperpixel!=8 && head->bitsperpixel!=16 && head->bitsperpixel!=24 && head->bitsperpixel!=32 ) return( -1 ); if ( head->compression==3 && ( head->bitsperpixel==16 || head->bitsperpixel==32 ) ) /* Good */; else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 ) ) /* Good */; else if ( head->compression==1 && head->bitsperpixel==8 ) /* Good */; else if ( head->compression==2 && head->bitsperpixel==4 ) /* Good */; else return( -1 ); if ( head->colorsused==0 ) head->colorsused = 1<<head->bitsperpixel; if ( head->bitsperpixel>=16 ) head->colorsused = 0; if ( head->colorsused>(1<<head->bitsperpixel) ) return( -1 ); for ( i=0; i<head->colorsused; ++i ) { int b,g,r; if ( (b=fgetc(fp))<0 || (g=fgetc(fp))<0 || (r=fgetc(fp))<0 ) return( -1 ); head->clut[i]=COLOR_CREATE(r,g,b); if ( head->headersize!=12 && fgetc(fp)<0 ) return( -1 ); } if ( head->compression==3 || head->headersize==108 ) { if ( getlong(fp,&head->red_mask) || \ getlong(fp,&head->green_mask) || \ getlong(fp,&head->blue_mask) ) return( -1 ); head->red_shift = bitshift(head->red_mask); head->green_shift = bitshift(head->green_mask); head->blue_shift = bitshift(head->blue_mask); } if ( head->headersize==108 && ( getlong(fp,&temp) || /* alpha_mask */ \ getlong(fp,&temp) || /* color space type */ \ getlong(fp,&temp) || /* redx */ \ getlong(fp,&temp) || /* redy */ \ getlong(fp,&temp) || /* redz */ \ getlong(fp,&temp) || /* greenx */ \ getlong(fp,&temp) || /* greeny */ \ getlong(fp,&temp) || /* greenz */ \ getlong(fp,&temp) || /* bluex */ \ getlong(fp,&temp) || /* bluey */ \ getlong(fp,&temp) || /* bluez */ \ getlong(fp,&temp) || /* gammared */ \ getlong(fp,&temp) || /* gammagreen */ \ getlong(fp,&temp) ) /* gammablue */ ) return( -1 ); return( 0 ); }