/* Write a screenshot to the specified filename */ void saveScreenshot (char *filename){ int i, j; Pic *in = NULL; Pic *out = NULL; if (filename == NULL) return; in = pic_alloc(640, 480, 3, NULL); out = pic_alloc(640, 480, 3, NULL); printf("File to save to: %s\n", filename); glReadPixels(0, 0, 640, 480, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[0]); for ( int j=0; j<480; j++ ) { for ( int i=0; i<640; i++ ) { PIC_PIXEL(out, i, j, 0) = PIC_PIXEL(in, i, 480-1-j, 0); PIC_PIXEL(out, i, j, 1) = PIC_PIXEL(in, i, 480-1-j, 1); PIC_PIXEL(out, i, j, 2) = PIC_PIXEL(in, i, 480-1-j, 2); } } if (jpeg_write(filename, out)) printf("File saved Successfully\n"); else printf("Error in Saving\n"); pic_free(in); pic_free(out); }
/* Write a screenshot to the specified filename */ void saveScreenshot (char *filename) { int i, j; Pic *in = NULL; if (filename == NULL) return; /* Allocate a picture buffer */ in = pic_alloc(640, 480, 3, NULL); printf("File to save to: %s\n", filename); for (i=479; i>=0; i--) { glReadPixels(0, 479-i, 640, 1, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[i*in->nx*in->bpp]); } if (jpeg_write(filename, in)) printf("File saved Successfully\n"); else printf("Error in Saving\n"); pic_free(in); }
/* saveScreenshot - Writes a screenshot to the specified filename in JPEG */ void saveScreenshot (char *filename) { Pic *in = NULL; if (filename == NULL) return; /* Allocate a picture buffer */ in = pic_alloc (Scene::WINDOW_WIDTH, Scene::WINDOW_HEIGHT, 3, NULL); printf("File to save to: %s\n", filename); /* Loop over each row of the image and copy into the image */ for (int i = Scene::WINDOW_HEIGHT - 1; i >= 0; i--) { glReadPixels(0, Scene::WINDOW_HEIGHT - 1 - i, Scene::WINDOW_WIDTH, 1, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[i*in->nx*in->bpp]); } /* Output the file */ if (jpeg_write(filename, in)) { printf("File saved Successfully\n"); } else { printf("Error in Saving\n"); } /* Free memory used by image */ pic_free(in); }
struct pic_blob * pic_make_blob(pic_state *pic, size_t len) { struct pic_blob *bv; bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TT_BLOB); bv->data = pic_alloc(pic, len); bv->len = len; return bv; }
struct pic_vector * pic_vec_new(pic_state *pic, size_t len) { struct pic_vector *vec; vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TT_VECTOR); vec->len = len; vec->data = (pic_value *)pic_alloc(pic, sizeof(pic_value) * len); return vec; }
struct pic_vector * pic_vec_new(pic_state *pic, size_t len) { struct pic_vector *vec; size_t i; vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TT_VECTOR); vec->len = len; vec->data = (pic_value *)pic_alloc(pic, sizeof(pic_value) * len); for (i = 0; i < len; ++i) { vec->data[i] = pic_none_value(); } return vec; }
void save_jpg() { Pic *in = NULL; in = pic_alloc(640, 480, 3, NULL); printf("Saving JPEG file: %s\n", filename); memcpy(in->pix,buffer,3*WIDTH*HEIGHT); if (jpeg_write(filename, in)) printf("File saved Successfully\n"); else printf("Error in Saving\n"); pic_free(in); }
pic_value pic_str_new(pic_state *pic, const char *cstr, size_t len) { struct pic_string *str; char *new_str; new_str = (char *)pic_alloc(pic, len + 1); strncpy(new_str, cstr, len); new_str[len] = '\0'; str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TT_STRING); str->len = len; str->str = new_str; return pic_obj_value(str); }
pic_sym pic_gensym(pic_state *pic, pic_sym base) { int uid = pic->uniq_sym_cnt++, len; char *str; pic_sym uniq; len = snprintf(NULL, 0, "%s@%d", pic_symbol_name(pic, base), uid); str = pic_alloc(pic, len + 1); sprintf(str, "%s@%d", pic_symbol_name(pic, base), uid); /* don't put the symbol to pic->syms to keep it uninterned */ uniq = pic->sym_cnt++; xh_put(&pic->sym_names, uniq, &str); return uniq; }
/* * ppm_read: read a PPM file into memory. * If opic!=0, then picture is read into opic->pix (after checking that * size is sufficient), else a new Pic is allocated. * Returns Pic pointer on success, 0 on failure. */ Pic *ppm_read(char *file, Pic *opic) { FILE *fp; char tok[20]; int nx, ny, pvmax; Pic *p; /* open PPM file */ if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "can't read PPM file %s\n", file); return 0; } /* read PPM header */ if (strcmp(ppm_get_token(fp, tok, sizeof tok), "P6")) { fprintf(stderr, "%s not a valid binary PPM file, bad magic#\n", file); fclose(fp); return 0; } if (sscanf(ppm_get_token(fp, tok, sizeof tok), "%d", &nx) != 1 || sscanf(ppm_get_token(fp, tok, sizeof tok), "%d", &ny) != 1 || sscanf(ppm_get_token(fp, tok, sizeof tok), "%d", &pvmax) != 1) { fprintf(stderr, "%s is not a valid PPM file: bad size\n", file); fclose(fp); return 0; } if (pvmax!=255) { fprintf(stderr, "%s does not have 8-bit components: pvmax=%d\n", file, pvmax); fclose(fp); return 0; } p = pic_alloc(nx, ny, 3, opic); printf("reading PPM file %s: %dx%d pixels\n", file, p->nx, p->ny); if (fread(p->pix, p->nx*3, p->ny, fp) != p->ny) { /* read pixels */ fprintf(stderr, "premature EOF on file %s\n", file); free(p); fclose(fp); return 0; } fclose(fp); return p; }
struct pic_string * pic_get_output_string(pic_state *pic, struct pic_port *port) { size_t size; char *buf; /* get endpos */ xfflush(port->file); size = (size_t)xftell(port->file); xrewind(port->file); /* copy to buf */ buf = (char *)pic_alloc(pic, size + 1); buf[size] = 0; xfread(buf, size, 1, port->file); return pic_make_str(pic, buf, size); }
static pic_value pic_regexp_regexp(pic_state *pic) { const char *ptrn, *flags = ""; int cflags, err; struct pic_regexp_t *reg; pic_get_args(pic, "z|z", &ptrn, &flags); cflags = REG_EXTENDED; while (*flags) { switch (*flags++) { case 'g': case 'G': /* pass */ break; case 'i': case 'I': cflags |= REG_ICASE; break; case 'm': case 'M': cflags |= REG_NEWLINE; break; } } reg = pic_alloc(pic, sizeof(struct pic_regexp_t)); reg->flags = flags; if ((err = regcomp(®->reg, ptrn, cflags)) != 0) { char errbuf[regerror(err, ®->reg, NULL, 0)]; regerror(err, ®->reg, errbuf, sizeof errbuf); regexp_dtor(pic, ®->reg); pic_errorf(pic, "regexp compilation error: %s", errbuf); } return pic_obj_value(pic_data_alloc(pic, ®exp_type, reg)); }
Pic *tiff_read(char *file, Pic *opic) { TIFF *tif; unsigned int npixels; uint32 *raster; int result; uint32 w, h; Pic *pic; tif = TIFFOpen(file, "r"); if( !tif ) return NULL; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); npixels = w*h; raster = (uint32 *)_TIFFmalloc(npixels * sizeof(uint32)); if( !raster ) return NULL; result = TIFFReadRGBAImage(tif, w, h, raster, TRUE); if( opic && npixels == (opic->nx * opic->ny)) pic = opic; else pic = pic_alloc(w, h, 3, opic); unpack_tiff_raster(pic, raster); _TIFFfree(raster); TIFFClose(tif); return pic; }
Pic *tiff_read(char *file, Pic *opic) { TIFF *tif; unsigned int npixels; short samples; uint32 *raster; int result; uint32 w, h; Pic *pic; tif = TIFFOpen(file, "r"); if( !tif ) return NULL; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples); npixels = w*h; raster = (uint32 *)_TIFFmalloc(npixels * sizeof(uint32)); if( !raster ) return NULL; printf("reading TIFF file %s: %dx%d (%d-bit) pixels\n", file, w, h, 8*samples); result = TIFFReadRGBAImage(tif, w, h, raster, TRUE); pic = pic_alloc(w, h, samples, opic); unpack_tiff_raster(pic, raster); _TIFFfree(raster); TIFFClose(tif); return pic; }
// Write a screen-shot, in the PPM format, to the specified filename, in PPM format void saveScreenshot(int windowWidth, int windowHeight, char * filename) { if (filename == NULL) return; // Allocate a picture buffer Pic * in = pic_alloc(windowWidth, windowHeight, 3, NULL); printf("File to save to: %s\n", filename); for (int i=windowHeight-1; i>=0; i--) { glReadPixels(0, windowHeight-i-1, windowWidth, 1, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[i*in->nx*in->bpp]); } if (ppm_write(filename, in)) printf("File saved Successfully\n"); else printf("Error in Saving\n"); pic_free(in); }
pic_state * pic_open(int argc, char *argv[], char **envp) { struct pic_port *pic_make_standard_port(pic_state *, xFILE *, short); char t; pic_state *pic; size_t ai; pic = malloc(sizeof(pic_state)); /* turn off GC */ pic->gc_enable = false; /* root block */ pic->wind = NULL; /* command line */ pic->argc = argc; pic->argv = argv; pic->envp = envp; /* prepare VM stack */ pic->stbase = pic->sp = calloc(PIC_STACK_SIZE, sizeof(pic_value)); pic->stend = pic->stbase + PIC_STACK_SIZE; /* callinfo */ pic->cibase = pic->ci = calloc(PIC_STACK_SIZE, sizeof(pic_callinfo)); pic->ciend = pic->cibase + PIC_STACK_SIZE; /* exception handler */ pic->xpbase = pic->xp = calloc(PIC_RESCUE_SIZE, sizeof(struct pic_proc *)); pic->xpend = pic->xpbase + PIC_RESCUE_SIZE; /* memory heap */ pic->heap = pic_heap_open(); /* symbol table */ xh_init_str(&pic->syms, sizeof(pic_sym *)); /* global variables */ pic->globals = NULL; /* macros */ pic->macros = NULL; /* attributes */ xh_init_ptr(&pic->attrs, sizeof(struct pic_dict *)); /* features */ pic->features = pic_nil_value(); /* libraries */ pic->libs = pic_nil_value(); pic->lib = NULL; /* GC arena */ pic->arena = calloc(PIC_ARENA_SIZE, sizeof(struct pic_object **)); pic->arena_size = PIC_ARENA_SIZE; pic->arena_idx = 0; /* raised error object */ pic->err = pic_undef_value(); /* standard ports */ pic->xSTDIN = NULL; pic->xSTDOUT = NULL; pic->xSTDERR = NULL; /* native stack marker */ pic->native_stack_start = &t; ai = pic_gc_arena_preserve(pic); #define S(slot,name) pic->slot = pic_intern_cstr(pic, name); S(sDEFINE, "define"); S(sLAMBDA, "lambda"); S(sIF, "if"); S(sBEGIN, "begin"); S(sSETBANG, "set!"); S(sQUOTE, "quote"); S(sQUASIQUOTE, "quasiquote"); S(sUNQUOTE, "unquote"); S(sUNQUOTE_SPLICING, "unquote-splicing"); S(sDEFINE_SYNTAX, "define-syntax"); S(sIMPORT, "import"); S(sEXPORT, "export"); S(sDEFINE_LIBRARY, "define-library"); S(sIN_LIBRARY, "in-library"); S(sCOND_EXPAND, "cond-expand"); S(sAND, "and"); S(sOR, "or"); S(sELSE, "else"); S(sLIBRARY, "library"); S(sONLY, "only"); S(sRENAME, "rename"); S(sPREFIX, "prefix"); S(sEXCEPT, "except"); S(sCONS, "cons"); S(sCAR, "car"); S(sCDR, "cdr"); S(sNILP, "null?"); S(sSYMBOLP, "symbol?"); S(sPAIRP, "pair?"); S(sADD, "+"); S(sSUB, "-"); S(sMUL, "*"); S(sDIV, "/"); S(sMINUS, "minus"); S(sEQ, "="); S(sLT, "<"); S(sLE, "<="); S(sGT, ">"); S(sGE, ">="); S(sNOT, "not"); S(sREAD, "read"); S(sFILE, "file"); S(sCALL, "call"); S(sTAILCALL, "tail-call"); S(sGREF, "gref"); S(sLREF, "lref"); S(sCREF, "cref"); S(sRETURN, "return"); S(sCALL_WITH_VALUES, "call-with-values"); S(sTAILCALL_WITH_VALUES, "tailcall-with-values"); pic_gc_arena_restore(pic, ai); #define R(slot,name) pic->slot = pic_gensym(pic, pic_intern_cstr(pic, name)); R(rDEFINE, "define"); R(rLAMBDA, "lambda"); R(rIF, "if"); R(rBEGIN, "begin"); R(rSETBANG, "set!"); R(rQUOTE, "quote"); R(rDEFINE_SYNTAX, "define-syntax"); R(rIMPORT, "import"); R(rEXPORT, "export"); R(rDEFINE_LIBRARY, "define-library"); R(rIN_LIBRARY, "in-library"); R(rCOND_EXPAND, "cond-expand"); pic_gc_arena_restore(pic, ai); /* root tables */ pic->globals = pic_make_dict(pic); pic->macros = pic_make_dict(pic); /* root block */ pic->wind = pic_alloc(pic, sizeof(struct pic_winder)); pic->wind->prev = NULL; pic->wind->depth = 0; pic->wind->in = pic->wind->out = NULL; /* reader */ pic->reader = malloc(sizeof(struct pic_reader)); pic->reader->typecase = PIC_CASE_DEFAULT; pic->reader->trie = pic_make_trie(pic); xh_init_int(&pic->reader->labels, sizeof(pic_value)); /* init readers */ pic_init_reader(pic); /* standard libraries */ pic->PICRIN_BASE = pic_open_library(pic, pic_read_cstr(pic, "(picrin base)")); pic->PICRIN_USER = pic_open_library(pic, pic_read_cstr(pic, "(picrin user)")); pic->lib = pic->PICRIN_USER; /* standard I/O */ pic->xSTDIN = pic_make_standard_port(pic, xstdin, PIC_PORT_IN); pic->xSTDOUT = pic_make_standard_port(pic, xstdout, PIC_PORT_OUT); pic->xSTDERR = pic_make_standard_port(pic, xstderr, PIC_PORT_OUT); pic_gc_arena_restore(pic, ai); /* turn on GC */ pic->gc_enable = true; pic_init_core(pic); return pic; }