Example #1
0
//GLuint loadTexture ( char * filename, int width, int height ){
void loadTexture (){

	Pic *textureSky = jpeg_read((char*)"sky6.jpg", NULL);
      
    glGenTextures( 1, &texIndexSky );
    glBindTexture( GL_TEXTURE_2D, texIndexSky );
    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
   
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSky->nx, textureSky->ny, 0, GL_RGB, GL_UNSIGNED_BYTE, textureSky->pix);
    
    
	Pic *textureGround = jpeg_read((char*)"ground1.jpg", NULL);
   
    glGenTextures( 1, &texIndexGround );
    glBindTexture( GL_TEXTURE_2D, texIndexGround );
    
    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
   	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureGround->nx, textureGround->ny, 0, GL_RGB, GL_UNSIGNED_BYTE, textureGround->pix);
 
    pic_free(textureSky);
    pic_free(textureGround);    
   
    
    
}
Example #2
0
static void
cont_dtor(pic_state *pic, void *data)
{
  struct fullcont *cont = data;

  pic_free(pic, cont->stk_ptr);
  pic_free(pic, cont->st_ptr);
  pic_free(pic, cont->ci_ptr);
  pic_free(pic, cont->arena);
  pic_free(pic, cont);
}
Example #3
0
void
pic_heap_close(pic_state *pic, struct heap *heap)
{
  struct heap_page *page;

  while (heap->pages) {
    page = heap->pages;
    heap->pages = heap->pages->next;
    pic_free(pic, page);
  }
  pic_free(pic, heap);
}
Example #4
0
void
pic_rope_decref(pic_state *pic, struct pic_rope *x) {
  if (! --x->refcnt) {
    if (x->chunk) {
      CHUNK_DECREF(x->chunk);
      pic_free(pic, x);
      return;
    }
    pic_rope_decref(pic, x->left);
    pic_rope_decref(pic, x->right);
    pic_free(pic, x);
  }
}
Example #5
0
static pic_value
pic_str_string_map(pic_state *pic)
{
  struct pic_proc *proc;
  pic_value *argv, vals, val;
  int argc, i, len, j;
  pic_str *str;
  char *buf;

  pic_get_args(pic, "l*", &proc, &argc, &argv);

  if (argc == 0) {
    pic_errorf(pic, "string-map: one or more strings expected, but got zero");
  } else {
    pic_assert_type(pic, argv[0], str);
    len = pic_str_len(pic_str_ptr(argv[0]));
  }
  for (i = 1; i < argc; ++i) {
    pic_assert_type(pic, argv[i], str);

    len = len < pic_str_len(pic_str_ptr(argv[i]))
      ? len
      : pic_str_len(pic_str_ptr(argv[i]));
  }
  buf = pic_malloc(pic, len);

  pic_try {
    for (i = 0; i < len; ++i) {
      vals = pic_nil_value();
      for (j = 0; j < argc; ++j) {
        pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals);
      }
      val = pic_apply_list(pic, proc, vals);

      pic_assert_type(pic, val, char);
      buf[i] = pic_char(val);
    }
    str = pic_make_str(pic, buf, len);
  }
  pic_catch {
    pic_free(pic, buf);
    pic_raise(pic, pic->err);
  }

  pic_free(pic, buf);

  return pic_obj_value(str);
}
Example #6
0
/* 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);

}
Example #7
0
static pic_value
pic_vec_vector_to_string(pic_state *pic)
{
  pic_vec *vec;
  char *buf;
  int n, start, end, i;
  pic_str *str;

  n = pic_get_args(pic, "v|ii", &vec, &start, &end);

  switch (n) {
  case 1:
    start = 0;
  case 2:
    end = vec->len;
  }

  if (end < start) {
    pic_errorf(pic, "vector->string: end index must not be less than start index");
  }

  buf = pic_malloc(pic, end - start);

  for (i = start; i < end; ++i) {
    pic_assert_type(pic, vec->data[i], char);

    buf[i - start] = pic_char(vec->data[i]);
  }

  str = pic_make_str(pic, buf, end - start);
  pic_free(pic, buf);

  return pic_obj_value(str);
}
Example #8
0
/*
	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);
}
Example #9
0
/* 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);
}
Example #10
0
File: regexp.c Project: krig/picrin
static void
regexp_dtor(pic_state *pic, void *data)
{
  struct pic_regexp_t *preg;

  preg = data;
  regfree(&preg->reg);
  pic_free(pic, data);
}
Example #11
0
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);      

}
Example #12
0
/*
	loadTexture - Loads a texture from a JPEG file to memory and returns the handle
		Note: pWidth and pHeight are pointers to return imageWidth and imageHeight
*/
GLuint loadTexture (char *filename, int *pWidth = NULL, int *pHeight = NULL)
{
	GLuint texIndex;

	if (filename == NULL)
		return 0;

	Pic *texture = jpeg_read (filename, NULL);
	if (texture == NULL)
		return 0;

	glGenTextures (1, &texIndex);
	glBindTexture (GL_TEXTURE_2D, texIndex);

	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

	glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	if (pWidth != NULL)
		*pWidth = texture->nx;
	if (pHeight != NULL)
		*pHeight = texture->ny;

	switch (texture->bpp)
	{
	case 1:
	default:
		gluBuild2DMipmaps (GL_TEXTURE_2D, 1, texture->nx, texture->ny,
					GL_R3_G3_B2, GL_UNSIGNED_BYTE, texture->pix);
		break;
	case 3:
		gluBuild2DMipmaps (GL_TEXTURE_2D, 3, texture->nx, texture->ny,
					GL_RGB, GL_UNSIGNED_BYTE, texture->pix);
		break;
	case 4:
		gluBuild2DMipmaps (GL_TEXTURE_2D, 4, texture->nx, texture->ny,
					GL_RGBA8, GL_UNSIGNED_BYTE, texture->pix);
		break;
	}

	pic_free(texture);

	return texIndex;
}
Example #13
0
static pic_value
pic_str_make_string(pic_state *pic)
{
  int len;
  char c = ' ';
  char *buf;
  pic_value ret;

  pic_get_args(pic, "i|c", &len, &c);

  buf = pic_malloc(pic, len);
  memset(buf, c, len);

  ret = pic_obj_value(pic_make_str(pic, buf, len));

  pic_free(pic, buf);
  return ret;
}
Example #14
0
static pic_value
pic_str_string(pic_state *pic)
{
  int argc, i;
  pic_value *argv;
  pic_str *str;
  char *buf;

  pic_get_args(pic, "*", &argc, &argv);

  buf = pic_malloc(pic, argc);

  for (i = 0; i < argc; ++i) {
    pic_assert_type(pic, argv[i], char);
    buf[i] = pic_char(argv[i]);
  }

  str = pic_make_str(pic, buf, argc);
  pic_free(pic, buf);

  return pic_obj_value(str);
}
Example #15
0
File: port.c Project: KeenS/benz
static pic_value
pic_port_read_blob_ip(pic_state *pic)
{
  struct pic_port *port;
  struct pic_blob *bv;
  int n;
  char *buf;
  size_t start, end, i, len;

  n = pic_get_args(pic, "b|pkk", &bv, &port, &start, &end);
  switch (n) {
  case 1:
    port = pic_stdin(pic);
  case 2:
    start = 0;
  case 3:
    end = bv->len;
  }

  assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!");

  if (end < start) {
    pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index");
  }

  len = end - start;

  buf = pic_calloc(pic, len, sizeof(char));
  i = xfread(buf, sizeof(char), len, port->file);
  memcpy(bv->data + start, buf, i);
  pic_free(pic, buf);

  if (i == 0) {
    return pic_eof_object();
  }
  else {
    return pic_size_value(i);
  }
}
Example #16
0
// 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);
}
void Image::FreeImage()
{
	//Frees the image
	pic_free(this->pImageData);
}
Example #18
0
static void
gc_finalize_object(pic_state *pic, struct object *obj)
{
  switch (obj_type(obj)) {
  case PIC_TYPE_VECTOR: {
    struct vector *vec = (struct vector *) obj;
    pic_free(pic, vec->data);
    break;
  }
  case PIC_TYPE_BLOB: {
    struct blob *blob = (struct blob *) obj;
    pic_free(pic, blob->data);
    break;
  }
  case PIC_TYPE_DATA: {
    struct data *data = (struct data *) obj;
    if (data->type->dtor) {
      data->type->dtor(pic, data->data);
    }
    break;
  }
  case PIC_TYPE_DICT: {
    struct dict *dict = (struct dict *) obj;
    kh_destroy(dict, &dict->hash);
    break;
  }
  case PIC_TYPE_SYMBOL: {
    /* TODO: remove this symbol's entry from pic->syms immediately */
    break;
  }
  case PIC_TYPE_ATTR: {
    struct attr *attr = (struct attr *) obj;
    kh_destroy(attr, &attr->hash);
    break;
  }
  case PIC_TYPE_IREP: {
    struct irep *irep = (struct irep *) obj;
    if ((irep->flags & IREP_CODE_STATIC) == 0) {
      pic_free(pic, (code_t *) irep->code);
    }
    pic_free(pic, irep->obj);
    pic_free(pic, irep->irep);
    break;
  }
  case PIC_TYPE_FRAME: {
    struct frame *frame = (struct frame *) obj;
    pic_free(pic, frame->regs);
    break;
  }
  case PIC_TYPE_ROPE_LEAF: {
    struct rope_leaf *leaf = (struct rope_leaf *) obj;
    pic_free(pic, (char *) leaf->str);
    break;
  }

  case PIC_TYPE_STRING:
  case PIC_TYPE_ROPE_NODE:
  case PIC_TYPE_PAIR:
  case PIC_TYPE_RECORD:
  case PIC_TYPE_PROC_FUNC:
  case PIC_TYPE_PROC_IREP:
    break;

  default:
    PIC_UNREACHABLE();
  }
}
Example #19
0
void
pic_gc(pic_state *pic)
{
  struct context *cxt;
  size_t j;
  khash_t(oblist) *s = &pic->oblist;
  struct symbol *sym;
  int it;
  struct object *obj, *prev, *next;

  assert(pic->gc_attrs == NULL);

  if (! pic->gc_enable) {
    return;
  }

  /* scan objects */

  for (cxt = pic->cxt; cxt != NULL; cxt = cxt->prev) {
    if (cxt->fp) gc_mark_object(pic, (struct object *)cxt->fp);
    if (cxt->sp) gc_mark_object(pic, (struct object *)cxt->sp);
    if (cxt->irep) gc_mark_object(pic, (struct object *)cxt->irep);
    gc_mark(pic, cxt->conts);
  }

  for (j = 0; j < pic->ai; ++j) {
    gc_mark_object(pic, (struct object *)pic->arena[j]);
  }

  gc_mark(pic, pic->globals);
  gc_mark(pic, pic->halt);

  /* scan weak references */

  do {
    struct object *key;
    pic_value val;
    int it;
    khash_t(attr) *h;
    struct attr *attr;

    j = 0;
    attr = pic->gc_attrs;

    while (attr != NULL) {
      h = &attr->hash;
      for (it = kh_begin(h); it != kh_end(h); ++it) {
        if (! kh_exist(h, it))
          continue;
        key = kh_key(h, it);
        val = kh_val(h, it);
        if (is_alive(key)) {
          if (pic_obj_p(pic, val) && ! is_alive((struct object *) pic_ptr(pic, val))) {
            gc_mark(pic, val);
            ++j;
          }
        }
      }
      attr = attr->prev;
    }
  } while (j > 0);

  /* reclaim dead weak references */

  while (pic->gc_attrs != NULL) {
    khash_t(attr) *h = &pic->gc_attrs->hash;
    for (it = kh_begin(h); it != kh_end(h); ++it) {
      if (! kh_exist(h, it))
        continue;
      obj = kh_key(h, it);
      if (! is_alive(obj)) {
        kh_del(attr, h, it);
      }
    }
    pic->gc_attrs = pic->gc_attrs->prev;
  }

  for (it = kh_begin(s); it != kh_end(s); ++it) {
    if (! kh_exist(s, it))
      continue;
    sym = kh_val(s, it);
    if (sym && ! is_alive((struct object *)sym)) {
      kh_del(oblist, s, it);
    }
  }

  /* reclaim dead objects */

  for (prev = &pic->gc_head, obj = prev->next; obj != &pic->gc_head; prev = obj, obj = next) {
    next = obj->next;
    if (is_alive(obj)) {
      unmark(obj);
    } else {
      gc_finalize_object(pic, obj);
      pic_free(pic, obj);
      prev->next = next;
      obj = prev;
    }
  }
}
int main()
{
  FILE *jpegin, *jpegout;
  char file_name[50];
  unsigned char **pic, *scan_line, **out_pic;
  int i, j, in_width, in_height, out_width, out_height, row_stride, quality;
  struct jpeg_decompress_struct cinfo;
  struct jpeg_compress_struct cinfo1;
  struct jpeg_error_mgr jerr;
  JSAMPROW row_pointer[1];

  sprintf(file_name, "%s.jpg", "testimg");
  if((jpegin = fopen(file_name, "rb")) == NULL){
    fprintf(stderr, "cannot open %s\n", file_name);
    return 1;
  }

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo, jpegin);
  jpeg_read_header(&cinfo, TRUE);
  jpeg_start_decompress(&cinfo);
  row_stride = cinfo.output_width * cinfo.output_components;

  in_width = cinfo.output_width;
  in_height = cinfo.output_height;

  pic = pic_calloc(pic, in_height, in_width*3);
 
  /* 
     you might have to change the out_width and out_height 
     if the output image size is different from the original.
  */
  out_width = in_width;
  out_height = in_height;
  out_pic = pic_calloc(out_pic, out_height, out_width*3);

  while (cinfo.output_scanline < in_height) {
    jpeg_read_scanlines(&cinfo, &pic[cinfo.output_scanline], 1);
  }

  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(jpegin);

  /* Simple Processing (negate the picture) */
  out_pic = process(pic,out_pic, out_height, out_width);

  sprintf(file_name, "%s.jpg", "output");
  if((jpegout = fopen(file_name, "wb")) == NULL){
    fprintf(stderr, "cannot open %s\n", file_name);
    return 1;
  }

  cinfo1.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo1);
  jpeg_stdio_dest(&cinfo1, jpegout);

  cinfo1.image_width = out_width;
  cinfo1.image_height = out_height;
  cinfo1.input_components = 3;
  cinfo1.in_color_space = JCS_RGB;
  jpeg_set_defaults(&cinfo1);

  quality = 70; 
  jpeg_set_quality(&cinfo1, quality, TRUE);
  jpeg_start_compress(&cinfo1, TRUE);
  row_stride = out_width * 3;

  while(cinfo1.next_scanline < cinfo1.image_height){
    row_pointer[0] = &out_pic[cinfo1.next_scanline][0];
    (void) jpeg_write_scanlines(&cinfo1, row_pointer, 1);
  }
  jpeg_finish_compress(&cinfo1);
  fclose(jpegout);
  jpeg_destroy_compress(&cinfo1);

  pic_free(pic, cinfo.output_height);
  pic_free(out_pic, cinfo.output_height);

  return 0;
}
Example #21
0
static void
gc_finalize_object(pic_state *pic, struct object *obj)
{
  switch (obj_type(pic, obj)) {
  case PIC_TYPE_VECTOR: {
    pic_free(pic, obj->u.vec.data);
    break;
  }
  case PIC_TYPE_BLOB: {
    pic_free(pic, obj->u.blob.data);
    break;
  }
  case PIC_TYPE_STRING: {
    pic_rope_decref(pic, obj->u.str.rope);
    break;
  }
  case PIC_TYPE_DATA: {
    if (obj->u.data.type->dtor) {
      obj->u.data.type->dtor(pic, obj->u.data.data);
    }
    break;
  }
  case PIC_TYPE_DICT: {
    kh_destroy(dict, &obj->u.dict.hash);
    break;
  }
  case PIC_TYPE_SYMBOL: {
    /* TODO: remove this symbol's entry from pic->syms immediately */
    break;
  }
  case PIC_TYPE_WEAK: {
    kh_destroy(weak, &obj->u.weak.hash);
    break;
  }
  case PIC_TYPE_IREP: {
    struct irep *irep = &obj->u.irep;
    if ((irep->flags & IREP_CODE_STATIC) == 0) {
      pic_free(pic, (code_t *) irep->code);
    }
    pic_free(pic, irep->obj);
    pic_free(pic, irep->irep);
    break;
  }
  case PIC_TYPE_PORT: {
    pic_fclose(pic, obj_value(pic, obj)); /* FIXME */
    break;
  }
  case PIC_TYPE_FRAME: {
    pic_free(pic, obj->u.frame.regs);
    break;
  }

  case PIC_TYPE_PAIR:
  case PIC_TYPE_ERROR:
  case PIC_TYPE_RECORD:
  case PIC_TYPE_PROC_FUNC:
  case PIC_TYPE_PROC_IREP:
    break;

  default:
    PIC_UNREACHABLE();
  }
}