Esempio n. 1
0
int inspect(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err)
{
  int num_to_display = 4;
  char selected_attribute[100] = "";
  int c;
  while ((c = getopt(argc, argv, "n:a:")) != -1)
  switch (c)
  {
    case 'n':
      num_to_display = clamp_int(atoi(optarg), 3, 100);
      break;
    case 'a':
      strncpy(selected_attribute, optarg, sizeof(selected_attribute));
      break;
    default:
      abort();
  }
  
  int num_shapes_without_selected_attribute = 0;
  int num_values = 0;
  char ** values = NULL;
  int * value_counts = NULL;
  
  int num_shapes = 0;
  int num_shapes_with_no_attributes = 0;
  int num_shapes_with_no_vertexs = 0;
  int num_vertexs = 0;
  int num_each_gl_type[7] = {0,0,0,0,0,0,0};
  
  struct Shape * shape = NULL;
  while ((shape = read_shape(pipe_in)))
  {
    if (shape->gl_type < 7) num_each_gl_type[shape->gl_type]++;
    if (shape->num_vertexs == 0) num_shapes_with_no_vertexs++;
    
    num_vertexs += shape->num_vertexs;
    num_shapes++;
    
    long i;
    
    if (selected_attribute[0] != 0) // -a [selected_attribute] provided
    {
      int has_attribute = 0;
      for (i = 0 ; i < shape->num_attributes ; i++)
      {
        struct Attribute * attribute = &shape->attributes[i];
        
        // only process the selected attribute
        if (strcmp(shape->attributes[i].name, &selected_attribute[0]) != 0) continue;
        
        has_attribute = 1;
        
        // has this value been found previously?
        int j, found = 0;
        for (j = 0 ; j < num_values ; j++)
          if (strcmp(shape->attributes[i].value, values[j])==0)
            { found = 1; value_counts[j]++; break; }
        if (found) continue;
        
        // value hasn't been found, add it to the list
        values = realloc(values, sizeof(char*)*(num_values+1));
        value_counts = realloc(value_counts, sizeof(int)*(num_values+1));
        values[num_values] = malloc(shape->attributes[i].value_length+1);
        value_counts[num_values] = 1;
        strncpy(values[num_values], shape->attributes[i].value, shape->attributes[i].value_length);
        num_values++;
      }
      if (has_attribute == 0) num_shapes_without_selected_attribute++;
    }
    else if (num_shapes <= 2)
    {
      long count_zero = 0;
      
      fprintf(stderr, "shape:\n");
      fprintf(stderr, "  version: %d\n", shape->version);
      fprintf(stderr, "  unique_set_id: %d\n", shape->unique_set_id);
      fprintf(stderr, "  gl_type: %s\n", get_gl_type_name(shape->gl_type));
      fprintf(stderr, "  num_attributes: %d\n", shape->num_attributes);
      if (shape->num_attributes) fprintf(stderr, "  attributes:\n");
      for (i = 0 ; i < shape->num_attributes ; i++)
      {
        struct Attribute * attribute = &shape->attributes[i];
        fprintf(stderr, "    %s(%d): '%s'\n", attribute->name, attribute->value_length, attribute->value);
      }
      fprintf(stderr, "  num_vertexs: %d\n", shape->num_vertexs);
      fprintf(stderr, "  num_vertex_arrays: %d\n", shape->num_vertex_arrays);
      if (shape->num_vertex_arrays) fprintf(stderr, "  vertex_arrays:\n");
      for (i = 0 ; i < shape->num_vertex_arrays ; i++)
      {
        fprintf(stderr, "    array_type: %s\n", get_array_type_name(shape->vertex_arrays[i].array_type));
        fprintf(stderr, "    num_dimensions: %d\n", shape->vertex_arrays[i].num_dimensions);
        fprintf(stderr, "    vertexs:\n");
        if (shape->num_vertexs > 0 && shape->vertex_arrays[i].num_dimensions > 0)
        {
          long j,k;
          for (k = 0 ; k < shape->num_vertexs ; k++)
          {
            if (k < num_to_display) fprintf(stderr, "      ");
            int is_zero = 1;
            for (j = 0 ; j < shape->vertex_arrays[i].num_dimensions ; j++)
            {
              if (shape->vertex_arrays[i].vertexs[k*shape->vertex_arrays[i].num_dimensions + j] != 0.0) is_zero = 0;
              if (k < num_to_display) fprintf(stderr, "%f ", shape->vertex_arrays[i].vertexs[k*shape->vertex_arrays[i].num_dimensions + j]);
            }
            if (is_zero) count_zero ++;
            if (k < num_to_display) fprintf(stderr, "\n");
            else if (k == num_to_display) fprintf(stderr, "      ...\n");
          }
        }
        if (i == num_to_display) fprintf(stderr, "    [...]\n");
      }
      if (count_zero > 0) fprintf(stderr, "  count_zero: %ld\n", count_zero);
    }
  }
  
  fprintf(pipe_err, "{\n");
  
  if (selected_attribute[0] != 0) // -a [attribute_name] provided
  {
    fprintf(pipe_err, "  \"num_unique_values\": %d,\n", num_values);
    fprintf(pipe_err, "  \"selected_attribute\": \"%s\",\n", selected_attribute);
    
    if (num_shapes_without_selected_attribute > 0)
      fprintf(pipe_err, "  \"num_shapes_without_selected_attribute\": %d,\n", num_shapes_without_selected_attribute);
    
    if (num_values > 0) fprintf(pipe_err, "  \"first_%d_unique_values\": [\n", (num_to_display < num_values) ? num_to_display : num_values);
    int i;
    for (i = 0 ; i < num_values ; i++)
    {
      if (i < num_to_display) fprintf(pipe_err, "    \"%s (%d)\"%s\n", values[i], value_counts[i], (i==num_values-1)?"":",");
      free(values[i]);
    }
    if (num_values > 0) fprintf(pipe_err, "  ]\n");
    free(values);
    free(value_counts);
  }
  
  if (num_shapes_with_no_vertexs > 0) printf("  \"num_shapes_with_no_vertexs\": %d,\n", num_shapes_with_no_vertexs);
  fprintf(pipe_err, "  \"num_shapes\": %d,\n", num_shapes);
  fprintf(pipe_err, "  \"num_vertexs\": %d,\n", num_vertexs);
  fprintf(pipe_err, "  \"num_each_gl_type\": [%d,%d,%d,%d,%d,%d,%d]\n", num_each_gl_type[0], num_each_gl_type[1], num_each_gl_type[2], num_each_gl_type[3], num_each_gl_type[4], num_each_gl_type[5], num_each_gl_type[6]);
  fprintf(pipe_err, "}\n");
}
Esempio n. 2
0
void MoleculeGraphics::create_cmdlist(void) {
  reset_disp_list();
  // set the default values
  DispCmdTriangle triangle;
  DispCmdCylinder cylinder;
  DispCmdPoint point;
  DispCmdLine line;
  DispCmdCone cone;
  DispCmdColorIndex color;
  DispCmdLineType linetype;
  DispCmdLineWidth linewidth;
  DispCmdSphere sphere;
  DispCmdSphereRes sph_res;
  //DispCmdComment comment;
  DispCmdPickPoint cmdPickPoint;

  append(DMATERIALON);
  color.putdata(0, cmdList);     // use the first color by default (blue)
  int last_res = -1;             // default sphere resolution

  int last_line = ::SOLIDLINE;       // default for lines
  linetype.putdata(last_line, cmdList); // solid and
  int last_width = 1;
  linewidth.putdata(last_width, cmdList);  //  of width 1

  // go down the list and draw things
  int num = num_elements();
  ShapeClass *shape;
  for (int i=0; i<num; i++) {
    shape = &(shapes[i]);
    switch (shape->shape) {
    case NONE: {
      break;
    }
    case POINT: {
      append(DMATERIALOFF);
      point.putdata(shape->data+0, cmdList);
      append(DMATERIALON);
      break;
    }
    case PICKPOINT: 
      cmdPickPoint.putdata(shape->data, (int)shape->data[3], cmdList);
      break;
    case LINE: {
      append(DMATERIALOFF);
      int style = int(shape->data[6]);
      int width = int(shape->data[7]);
      if (style != last_line) {
        linetype.putdata(style, cmdList);
        last_line = style;
      }
      if (width != last_width) {
        linewidth.putdata(width, cmdList);
        last_width = width;
      }
      line.putdata(shape->data+0, shape->data+3, cmdList);
      append(DMATERIALON);
      break;
    }
    case TRIANGLE: {
      triangle.putdata(shape->data+0, shape->data+3, shape->data+6, cmdList);
      break;
    }
    case TRINORM: {
      triangle.putdata(shape->data+0, shape->data+3 , shape->data+6, 
                       shape->data+9, shape->data+12, shape->data+15, cmdList);
      break;
    }
    case TRICOLOR: 
      {
        // compute rgb colors from indices
        float colors[9];
        for (int i=0; i<3; i++) {
          int c = (int)shape->data[18+i];
          c = clamp_int(c, 0, MAXCOLORS-1);
          vec_copy(colors+3*i, scene->color_value(c));
        }
        const float *verts = shape->data+0;
        const float *norms = shape->data+9;
        int facets[3] = { 0,1,2 };
        DispCmdTriMesh::putdata(verts, norms, colors, 3, facets, 1, 0, cmdList);
      }
      break;
    case CYLINDER: {
      cylinder.putdata(shape->data+0, shape->data+3, shape->data[6], 
                       int(shape->data[7]), 
                       (int (shape->data[8])) ? 
                         CYLINDER_TRAILINGCAP | CYLINDER_LEADINGCAP : 0, 
                       cmdList);
      break;
    }
    case CONE: {
      cone.putdata(shape->data+0, shape->data+3, shape->data[6], 
                   shape->data[8], int(shape->data[7]), cmdList);
      break;
    }
    case TEXT: {
      append(DMATERIALOFF);
      DispCmdText text;
      DispCmdTextSize textSize;
      textSize.putdata(shape->data[3], cmdList);
      text.putdata(shape->data, shapetext[(int)shape->data[5]], shape->data[4], cmdList);
      append(DMATERIALON);
      break;
    }
#if 0
    // these aren't supported yet
    case DBEGINREPGEOMGROUP: {
      char *s = (char *) (shape);
      beginrepgeomgroup.putdata(s,cmdList);
      break;
    }
    case DCOMMENT: {
      char *s = (char *) (shape);
      comment.putdata(s,cmdList);
      break;
    }
#endif
    case SPHERE: {
      int res = int (shape->data[4]);
      if (res != last_res) {
        sph_res.putdata(res, cmdList);
        last_res = res;
      }
      sphere.putdata(shape->data+0, shape->data[3], cmdList);
      break;
    }
    case MATERIALS: {
      if (shape->data[0] == 0) append(DMATERIALOFF);
      else append(DMATERIALON);
      break;
    }
    case MATERIAL: {
      const float *data = shape->data;
      cmdList->ambient = data[0];
      cmdList->specular = data[1];
      cmdList->diffuse = data[2];
      cmdList->shininess = data[3];
      cmdList->mirror = data[4];
      cmdList->opacity = data[5];
      cmdList->outline = data[6];
      cmdList->outlinewidth = data[7];
      cmdList->transmode = data[8];
      cmdList->materialtag = (int)data[9];
      break;
    }
    case COLOR: {
      color.putdata(int(shape->data[0]), cmdList);
      break;
    }
    default:
      msgErr << "Sorry, can't draw that" << sendmsg;
    }
  }
  needRegenerate = 0;
}
Esempio n. 3
0
void center_image(DispContext *dc)
{
    dc->pos_x = clamp_int((dc->screen->w - dc->img_w) / 2, -32767, 32768);
    dc->pos_y = clamp_int((dc->screen->h - dc->img_h) / 2, -32767, 32768);
}
Esempio n. 4
0
int main(int argc, char **argv)
{
    int c, image_index, image_count, incr, i;
    SDL_Event event;
    DispContext dc_s, *dc = &dc_s;
    const SDL_VideoInfo *vi;

    for(;;) {
        c = getopt(argc, argv, "h");
        if (c == -1)
            break;
        switch(c) {
        case 'h':
        show_help:
            help();
            break;
        default:
            exit(1);
        }
    }

    if (optind >= argc)
        goto show_help;

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
        fprintf(stderr, "Could not init SDL\n");
        exit(1);
    }
    memset(dc, 0, sizeof(*dc));

    vi = SDL_GetVideoInfo();
    dc->screen_w = vi->current_w;
    dc->screen_h = vi->current_h;
    dc->is_full_screen = 0;

    image_count = argc - optind;
    image_index = 0;
    if (load_image(dc, argv[optind + image_index]) < 0)
        exit(1);
    dc->background_type = BG_TILED;

    {
        int w, h;

        if (image_count > 1 || (dc->img_w < 256 || dc->img_h < 256)) {
            w = DEFAULT_W;
            h = DEFAULT_H;
        } else {
            w = clamp_int(dc->img_w, 32, dc->screen_w);
            h = clamp_int(dc->img_h, 32, dc->screen_h);
        }
        open_window(dc, w, h, 0);
        set_caption(dc, argv + optind, image_index, image_count);
    }

    center_image(dc);
    draw_image(dc);

    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

    for(;;) {
        if (!SDL_WaitEvent(&event))
            continue;
        switch(event.type) {
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym) {
            case SDLK_ESCAPE:
            case SDLK_q:
                goto done;
            case SDLK_SPACE: /* next image */
            case SDLK_n:
                incr = 1;
                goto prev_next;
            case SDLK_p: /* previous image */
                incr = -1;
            prev_next:
                if (image_count > 1) {
                    for(i = 0; i < image_count; i++) {
                        image_index += incr;
                        if (image_index < 0)
                            image_index = image_count - 1;
                        else if (image_index >= image_count)
                            image_index = 0;
                        if (load_image(dc, argv[optind + image_index]) == 0)
                            break;
                    }
                    if (i == image_count)
                        exit(1);
                    set_caption(dc, argv + optind, image_index, image_count);
                    center_image(dc);
                    draw_image(dc);
                }
                break;
            case SDLK_LEFT:
                pan_image(dc, 32, 0);
                break;
            case SDLK_RIGHT:
                pan_image(dc, -32, 0);
                break;
            case SDLK_UP:
                pan_image(dc, 0, 32);
                break;
            case SDLK_DOWN:
                pan_image(dc, 0, -32);
                break;
            case SDLK_c:
                center_image(dc);
                draw_image(dc);
                break;
            case SDLK_b:
                dc->background_type ^= 1;
                draw_image(dc);
                break;
            case SDLK_f:
                dc->is_full_screen ^= 1;
                if (dc->is_full_screen) {
                    /* save old windows size */
                    dc->win_w = dc->screen->w;
                    dc->win_h = dc->screen->h;
                    open_window(dc, dc->screen_w, dc->screen_h, 1);
                } else {
                    open_window(dc, dc->win_w, dc->win_h, 0);
                }
                center_image(dc);
                draw_image(dc);
                break;
            default:
                break;
            }
            break;
        case SDL_VIDEORESIZE:
            {
                open_window(dc, event.resize.w, event.resize.h, 0);
                center_image(dc);
                draw_image(dc);
            }
            break;
        case SDL_QUIT:
            goto done;
        case SDL_MOUSEMOTION:
            if (event.motion.state) {
                pan_image(dc, event.motion.xrel, event.motion.yrel);
            }
            break;
        case SDL_USEREVENT:
            if (dc->frame_count > 1) {
                /* show next frame */
                if (dc->frame_index == (dc->frame_count - 1)) {
                    if (dc->loop_count == 0 ||
                        dc->loop_counter < (dc->loop_count - 1)) {
                        dc->frame_index = 0;
                        dc->loop_counter++;
                    } else {
                        break;
                    }
                } else {
                    dc->frame_index++;
                }
                draw_image(dc);
                restart_frame_timer(dc);
            }
            break;
        default:
            break;
        }
    }
 done:

    SDL_FreeSurface(dc->screen);
    return 0;
}
Esempio n. 5
0
static void cache_render_rgb_float_zo(struct map_cache *cache, struct image_channel *channel,
			  int zoom, int fx, int fy, int fw, int fh)
{
	struct ccd_frame *fr = channel->fr;
	int fjump = fr->w - fw;
	int x, y;
	float *fdat_r, *fdat_g, *fdat_b;
	float *fd0_r, *fd0_g, *fd0_b;
	int lndx;
	float gain_r = LUT_SIZE / (channel->hcut - channel->lcut);
	float flr_r = channel->lcut;
	float gain_g = LUT_SIZE / (channel->hcut - channel->lcut);
	float flr_g = channel->lcut;
	float gain_b = LUT_SIZE / (channel->hcut - channel->lcut);
	float flr_b = channel->lcut;

	unsigned char *cdat = cache->dat;
	unsigned char pix;

	cache->x = fx / zoom;
	cache->y = fy / zoom;

	cache->w = fw / zoom;
	cache->h = fh / zoom;

	float avgf = 1.0 / (zoom * zoom);
	float pixf_r, pixf_g, pixf_b;

	int fj2, xx, yy;

	fdat_r = (float *) fr->rdat + fx + fy * fr->w;
	fdat_g = (float *) fr->gdat + fx + fy * fr->w;
	fdat_b = (float *) fr->bdat + fx + fy * fr->w;

	fjump = fr->w - fw + (zoom - 1) * fr->w;
	fj2 = fr->w - zoom; /* jump from last pixel in zoom row to first one in the next row */

	for (y = 0; y < cache->h; y++) {
		for (x = 0; x < cache->w; x++) {
			pixf_r = pixf_g = pixf_b = 0.0;

			fd0_r = fdat_r;
			fd0_g = fdat_g;
			fd0_b = fdat_b;

			for (yy = 0; yy < zoom; yy++) {
				for (xx = 0; xx < zoom; xx++) {
					pixf_r += *fdat_r++;
					pixf_g += *fdat_g++;
					pixf_b += *fdat_b++;
				}

				fdat_r += fj2;
				fdat_g += fj2;
				fdat_b += fj2;
			}

			fdat_r = fd0_r + zoom;
			fdat_g = fd0_g + zoom;
			fdat_b = fd0_b + zoom;

			lndx = floor(gain_r * (pixf_r * avgf - flr_r));
			clamp_int(&lndx, 0, LUT_SIZE - 1);
			pix = channel->lut[lndx] >> 8;
			*cdat++ = pix;

			lndx = floor(gain_g * (pixf_g * avgf - flr_g));
			clamp_int(&lndx, 0, LUT_SIZE - 1);
			pix = channel->lut[lndx] >> 8;
			*cdat++ = pix;

			lndx = floor(gain_b * (pixf_b * avgf - flr_b));
			clamp_int(&lndx, 0, LUT_SIZE - 1);
			pix = channel->lut[lndx] >> 8;
			*cdat++ = pix;

		}
		fdat_r += fjump;
		fdat_g += fjump;
		fdat_b += fjump;
	}

	cache->cache_valid = 1;
}