static Any get_clipboard_data(DisplayObj d, Name type) { HGLOBAL mem; HENHMETAFILE hmf; Any rval = FAIL; OpenClipboard(CLIPBOARDWIN); if ( type != NAME_winMetafile && (mem = GetClipboardData(CF_UNICODETEXT)) ) { wchar_t *data = GlobalLock(mem); wchar_t *copy, *q; q = copy = pceMalloc((wcslen(data)+1)*sizeof(wchar_t)); for(; *data; data++) { if ( *data == '\r' && data[1] == '\n' ) { data++; *q++ = '\n'; } else *q++ = *data; } *q = EOS; rval = WCToString(copy, q-copy); pceFree(copy); GlobalUnlock(mem); } else if ( type != NAME_winMetafile && (mem = GetClipboardData(CF_TEXT)) ) { char far *data = GlobalLock(mem); char *copy, *q; q = copy = pceMalloc(strlen(data)); for(; *data; data++) { if ( *data == '\r' && data[1] == '\n' ) { data++; *q++ = '\n'; } else *q++ = *data; } *q = EOS; rval = CtoString(copy); pceFree(copy); GlobalUnlock(mem); } else if ( type != NAME_text && (hmf = GetClipboardData(CF_ENHMETAFILE)) ) { HENHMETAFILE copy = CopyEnhMetaFile(hmf, NULL); if ( !copy ) { errorPce(d, NAME_winMetafile, CtoName("CopyEnhMetaFile"), APIError()); fail; } rval = CtoWinMetafile(copy); DeleteEnhMetaFile(hmf); } CloseClipboard(); return rval; }
static void ws_open_colourmap(ColourMap cm) { if ( !getExistingPaletteColourMap(cm) && notNil(cm->colours) ) { int size = valInt(cm->colours->size); LOGPALETTE *lp = pceMalloc(offset(LOGPALETTE, palPalEntry[size])); PALETTEENTRY *pe = &lp->palPalEntry[0]; HPALETTE hpal; int n, nc = 0; DisplayObj d = CurrentDisplay(NIL); for(n=0; n<size; n++) { Colour c = cm->colours->elements[n]; if ( isName(c) && (c = checkType(c, TypeColour, NIL)) ) elementVector(cm->colours, toInt(n+1), c); if ( instanceOfObject(c, ClassColour) ) { if ( c->kind == NAME_named ) ws_create_colour(c, d); pe->peRed = valInt(c->red) >> 8; pe->peGreen = valInt(c->green) >> 8; pe->peBlue = valInt(c->blue) >> 8; pe->peFlags = 0; pe++; nc++; } else Cprintf("%s is not a colour\n", pp(c)); }
static HPALETTE CreateCCPalette(int size) { int le = size * size * size; LOGPALETTE *lp = pceMalloc(offset(LOGPALETTE, palPalEntry[le])); int i, r, g, b; PALETTEENTRY *pe = &lp->palPalEntry[0]; BYTE *intensity = alloca(size * sizeof(BYTE)); HPALETTE hpal; lp->palVersion = 0x300; lp->palNumEntries = le; for(i=0; i<size; i++) intensity[i] = (255*i)/(size-1); /* gamma correction? */ for(r=0; r<size; r++) { for(g = 0; g<size; g++) { for(b = 0; b<size; b++) { pe->peRed = intensity[r]; pe->peGreen = intensity[g]; pe->peBlue = intensity[b]; pe->peFlags = 0; pe++; } } } if ( !(hpal = CreatePalette(lp)) ) Cprintf("Failed to create color cube with %d entries\n", le); return hpal; }
static void addTable(Table t, unsigned long name, unsigned long value) { Symbol *l = &t->symbols[hashvalue(t->size, name)]; Symbol s = (Symbol)pceMalloc(sizeof(struct symbol)); s->name = name; s->value = value; s->next = *l; *l = s; }
void * xmalloc(size_t nbytes) { void *rval = pceMalloc(nbytes); if ( !rval ) { Cprintf("[PCE: Not enough memory]\n"); exit(1); } return rval; }
static Table newTable(int size) { Table t = (Table)pceMalloc(sizeof(struct table) + (size-1) * sizeof(Symbol)); int i; Symbol *s; t->size = size; for(i=size, s=t->symbols; --i >= 0; s++) *s = NULL; return t; }
StringObj create_string_from_str(String s, int tmp) { string s2; CharArray c; StringObj str; charA *do_free = NULL; if ( s->s_iswide ) { const charW *txt = s->s_textW; const charW *end = &txt[s->s_size]; charA *p; for( ; txt < end; txt++ ) { if ( *txt > 0xff ) goto canonical; } str_inithdr(&s2, FALSE); s2.s_size = s->s_size; if ( !(s2.s_textA = alloca(s->s_size)) ) { s2.s_textA = pceMalloc(s->s_size); do_free = s2.s_textA; } for(txt = s->s_textW, p = s2.s_textA; txt < end; ) *p++ = *txt++; s = &s2; } canonical: c = StringToScratchCharArray(s); if ( tmp ) str = tempObject(ClassString, name_procent_s, c, EAV); else str = answerObject(ClassString, name_procent_s, c, EAV); doneScratchCharArray(c); if ( do_free ) pceFree(do_free); return str; }
XImage * read_ppm_file(Display *disp, Colormap cmap, int depth, IOSTREAM *fd) { XImage *img; long here = Stell(fd); int c; int fmt, encoding; int width, height, bytes_per_line, scale=0; char *data; int allocdepth; int pad = XBitmapPad(disp); Visual *v = DefaultVisual(disp, DefaultScreen(disp)); ncolours = nmapped = nfailed = 0; /* statistics */ assert(pad%8 == 0); if ( (c=Sgetc(fd)) != 'P' ) { Sungetc(c, fd); return NULL; } if ( !cmap ) cmap = DefaultColormap(disp, DefaultScreen(disp)); c = Sgetc(fd); if ( c < '1' || c > '9' ) goto errout; c -= '0'; fmt = ((c - 1) % 3) + 1; encoding = c - fmt; width = getNum(fd); height = getNum(fd); if ( fmt == PNM_PBM ) { depth = 1; } else { scale = getNum(fd); if ( !depth ) depth = DefaultDepth(disp, DefaultScreen(disp)); } if ( width < 0 || height < 0 || scale < 0 ) goto errout; allocdepth = (depth >= 24 ? 32 : depth); bytes_per_line = roundup((width*allocdepth+7)/8, pad/8); data = (char *)pceMalloc(height * bytes_per_line); img = XCreateImage(disp, v, depth, fmt == PNM_PBM ? XYBitmap : ZPixmap, 0, data, width, height, pad, bytes_per_line); if ( !img ) { perror("XCreateImage"); pceFree(data); goto errout; } img->bits_per_pixel = depth; switch(encoding) { int x, y; case PNM_ASCII: { switch(fmt) { case PNM_PBM: for(y=0; y<height; y++) { for(x=0; x<width; x++) { int value = getNum(fd); if ( value < 0 || value > 1 ) goto errout; XPutPixel(img, x, y, value); } } break; case PNM_PGM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int g = getNum(fd); unsigned long pixel; if ( g < 0 || g > scale ) goto errout; if ( scale != 255 ) g = rescale(g, scale, 255); pixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int r = getNum(fd); int g = getNum(fd); int b = getNum(fd); unsigned long pixel; if ( r < 0 || r > scale || g < 0 || g > scale || b < 0 || b > scale ) goto errout; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } pixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } break; } break; } case PNM_RAWBITS: { switch(fmt) { case PNM_PBM: { int byte = 0; int bit = 0; for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( !bit ) { byte = Sgetc(fd); bit = 8; } bit--; XPutPixel(img, x, y, (byte & (1<<bit)) ? 1 : 0); } bit = 0; /* scanlines are byte-aligned */ } break; } case PNM_PGM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int g; unsigned long pixel; if ( Sfeof(fd) || (g=Sgetc(fd)) > scale ) goto errout; if ( scale != 255 ) g = rescale(g, scale, 255); pixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int r, g, b; unsigned long pixel; if ( Sfeof(fd) || (r=Sgetc(fd)) > scale || (g=Sgetc(fd)) > scale || (b=Sgetc(fd)) > scale ) goto errout; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } pixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } break; } break; } case PNM_RUNLEN: { int rlen = 0; unsigned long cpixel = NOPIXEL; switch(fmt) { case PNM_PGM: { Table t = newTable(64); DEBUG(NAME_pnm, Cprintf("Reading runlength encoded graymap\n")); for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( rlen-- > 0 ) { XPutPixel(img, x, y, cpixel); } else { int g; if ( (g=Sgetc(fd)) > scale || (rlen = Sgetc(fd)) == EOF ) goto errout; rlen &= 0xff; if ( scale != 255 ) g = rescale(g, scale, 255); cpixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, cpixel); rlen--; } } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( rlen-- > 0 ) { XPutPixel(img, x, y, cpixel); } else { int r, g, b; if ( (r=Sgetc(fd)) > scale || (g=Sgetc(fd)) > scale || (b=Sgetc(fd)) > scale || (rlen = Sgetc(fd)) == EOF ) goto errout; rlen &= 0xff; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } cpixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, cpixel); rlen--; } } } freeTable(t); break; } } } } DEBUG(NAME_ppm, Cprintf("PNM: Converted %dx%dx%d image, %d colours (%d mapped, %d failed)\n", width, height, depth, ncolours, nmapped, nfailed)); return img; errout: DEBUG(NAME_ppm, Cprintf("PNM: Format error, index = %d\n", Stell(fd))); Sseek(fd, here, SEEK_SET); return NULL; }
static unsigned char * read_x11_bitmap_file(IOSTREAM *fd, int *w, int *h) { unsigned char *data = NULL; char line[LINESIZE]; int size = 0; char name_and_type[LINESIZE]; /* an input line */ char *type; /* for parsing */ int value; /* from an input line */ int version10p; /* bool, old format */ int padding; /* to handle alignment */ int bytes_per_line; /* per scanline of data */ unsigned int ww = 0; /* width */ unsigned int hh = 0; /* height */ int hx = -1; /* x hotspot */ int hy = -1; /* y hotspot */ if (initialized == FALSE) initHexTable(); #define RETURN_ERROR { if (data) pceFree(data); return NULL; } while (Sfgets(line, LINESIZE, fd)) { if ( sscanf(line,"#define %s %d",name_and_type,&value) == 2) { if (!(type = strrchr(name_and_type, '_'))) type = name_and_type; else type++; if (!strcmp("width", type)) ww = (unsigned int) value; if (!strcmp("height", type)) hh = (unsigned int) value; if (!strcmp("hot", type)) { if (type-- == name_and_type || type-- == name_and_type) continue; if (!strcmp("x_hot", type)) hx = value; if (!strcmp("y_hot", type)) hy = value; } continue; } if (sscanf(line, "static short %s = {", name_and_type) == 1) version10p = 1; else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1) version10p = 0; else if (sscanf(line, "static char %s = {", name_and_type) == 1) version10p = 0; else continue; if (!(type = strrchr(name_and_type, '_'))) type = name_and_type; else type++; if (strcmp("bits[]", type)) continue; if (!ww || !hh) RETURN_ERROR; if ((ww % 16) && ((ww % 16) < 9) && version10p) padding = 1; else padding = 0; bytes_per_line = (ww+7)/8 + padding; size = bytes_per_line * hh; data = (unsigned char *) pceMalloc(size); if (version10p) { unsigned char *ptr; int bytes; for (bytes=0, ptr=data; bytes<size; (bytes += 2)) { if ((value = NextInt(fd)) < 0) RETURN_ERROR; *(ptr++) = value; if (!padding || ((bytes+2) % bytes_per_line)) *(ptr++) = value >> 8; } } else { unsigned char *ptr; int bytes; for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) { if ((value = NextInt(fd)) < 0) RETURN_ERROR; *ptr=value; } } }