Ejemplo n.º 1
0
/*
 * and input1 input2 output: logical-and pixels of two images
 */
static int
cmd_and(int argc, char *argv[])
{
	img_t *image, *mask;
	int rv;

	if (argc < 3)
		return (EXIT_USAGE);

	image = img_read(argv[0]);
	mask = img_read(argv[1]);

	if (mask == NULL || image == NULL) {
		img_free(image);
		return (EXIT_FAILURE);
	}

	if (image->img_width != mask->img_width ||
	    image->img_height != mask->img_height) {
		warnx("image dimensions do not match");
		img_free(image);
		img_free(mask);
		return (EXIT_FAILURE);
	}

	img_and(image, mask);
	rv = img_write(image, argv[2]);
	img_free(image);
	img_free(mask);
	return (rv == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Ejemplo n.º 2
0
/*
 * compare image mask: compute a difference score for the given image and mask.
 */
static int
cmd_compare(int argc, char *argv[])
{
	img_t *image, *mask, *dbgmask;
	char *dbgfile = NULL;
	int rv;
	char c;

	while ((c = getopt(argc, argv, "s:")) != -1) {
		switch (c) {
		case 's':
			dbgfile = optarg;
			break;
		default:
			return (EXIT_USAGE);
		}
	}

	if (optind + 2 > argc)
		return (EXIT_USAGE);

	image = img_read(argv[optind++]);
	mask = img_read(argv[optind++]);

	if (mask == NULL || image == NULL) {
		img_free(image);
		return (EXIT_FAILURE);
	}

	if (image->img_width != mask->img_width ||
	    image->img_height != mask->img_height) {
		warnx("image dimensions do not match");
		rv = EXIT_FAILURE;
		goto done;
	}

	(void) printf("%f\n",
	    img_compare(image, mask, dbgfile ? &dbgmask : NULL));

	if (dbgfile != NULL && dbgmask != NULL) {
		(void) img_write(dbgmask, dbgfile);
		img_free(dbgmask);
	}

	rv = EXIT_SUCCESS;

done:
	img_free(image);
	img_free(mask);
	return (rv);
}
Ejemplo n.º 3
0
/*
 * ident input: identify the game state described in a given image
 */
static int
cmd_ident(int argc, char *argv[])
{
	img_t *image;
	kv_screen_t info;

	if (argc < 1)
		return (EXIT_USAGE);

	if (kv_init(dirname((char *)kv_arg0)) != 0) {
		warnx("failed to initialize masks");
		return (EXIT_FAILURE);
	}

	image = img_read(argv[0]);
	if (image == NULL) {
		warnx("failed to read %s", argv[0]);
		return (EXIT_FAILURE);
	}

	kv_ident(image, &info, KV_IDENT_ALL);
	kv_screen_json(argv[0], 0, 0, &info, NULL, stdout);

	return (EXIT_SUCCESS);
}
Ejemplo n.º 4
0
/*
 * translatexy input output xoffset yoffset: shift an image by the given offsets
 */
static int
cmd_translatexy(int argc, char *argv[])
{
	img_t *image, *newimage;
	char *q;
	int rv;
	long dx, dy;

	if (argc < 4)
		return (EXIT_USAGE);

	image = img_read(argv[0]);
	if (image == NULL)
		return (EXIT_FAILURE);

	dx = strtol(argv[2], &q, 0);
	if (*q != '\0')
		warnx("x offset value truncated to %d", dx);

	dy = strtol(argv[3], &q, 0);
	if (*q != '\0')
		warnx("y offset value truncated to %d", dy);

	newimage = img_translatexy(image, dx, dy);
	if (newimage == NULL) {
		warn("failed to translate image");
		img_free(image);
		return (EXIT_FAILURE);
	}

	rv = img_write(newimage, argv[1]);
	img_free(newimage);
	return (rv);
}
Ejemplo n.º 5
0
int dmk_get_track(struct img_s *img, dmk_t *dmk, uint32_t cyl, uint32_t head)
{
	uint32_t drive, cyllen, heads;
	off_t offs;

#if	DMK_CACHE_TRACK
	/* is it the buffered track number? */
	if (cyl == dmk->rsvd[DMK_TRACK_BUFF] &&
		head == dmk->rsvd[DMK_SIDE_BUFF])
		return 0;
#endif

	drive = img_minor(img);
	cyllen = dmk->cyllen[0] + 256 * dmk->cyllen[1];
	heads = (dmk->flags & DMK_FLAG_SSIDE) ? 1 : 2;

	LOG((3,"DMK","#%x load track %u, head %u into buffer\n",
		drive, cyl, head));

	offs = DMK_HEADER_SIZE + (off_t)cyllen * (cyl * heads + head);
	memset(dmk->track, 0xff, cyllen);
	memset(dmk->track, 0x00, DMK_IDAM_SIZE);
	if (cyllen != img_read(img, offs, dmk->track, cyllen))
		return -1;

#if	DMK_CACHE_TRACK
	dmk->rsvd[DMK_TRACK_BUFF] = cyl;
	dmk->rsvd[DMK_SIDE_BUFF] = head;
#endif
	return 0;
}
Ejemplo n.º 6
0
void main(int argc, char *argv[]) {
  char outname[512], inname[512], *boh;
  int len, i, j, total;
  IMAGE *img = 0, *newimg = 0;
  struct cmap_color *cmap;
  struct gl_color *gl_buffer;

  toonz_init(DUMMY_KEY_SLOT, (int *)&argc, argv);
  InibisciDongle();
  unprotect_lib();

  if (argc < 3) {
    printf(" %s error: missing argument\n", argv[0]);
    printf(" usage: %s infile outfile\n", argv[0]);
    exit(0);
  }

  if (*argv[1] == '-') {
    printf("bad filename <%s> \n", argv[1]);
    exit(0);
  }
  strcpy(inname, argv[1]);

  if (argc > 2)
    strcpy(outname, argv[2]);
  else
    strcpy(outname, argv[1]);

  printf(">> Loading %s\n", inname);

  img = img_read(inname);
  if (!img) {
    printf("file %s not found\n", inname);
    exit(0);
  }

  printf(">> Writing %s\n", outname);
  if (!img_write_plt(outname, img)) {
    printf("non sono stato in grado di scrivere: %s\n", outname);
    exit(0);
  }

  newimg = img_read_plt(outname);
  if (!newimg) {
    printf("file %s not found\n", outname);
    exit(0);
  }

  boh = "MAMMA.cmap";
  printf(">> Writing %s\n", boh);

  img_write_ciak(boh, newimg);
  printf(">> Riprovami e non te ne pentirai!! <<\n");
}
Ejemplo n.º 7
0
unsigned int img_gltexture_read(struct img_io *io)
{
	struct img_pixmap img;
	unsigned int tex;

	img_init(&img);
	if(img_read(&img, io) == -1) {
		img_destroy(&img);
		return 0;
	}

	tex = img_gltexture(&img);
	img_destroy(&img);
	return tex;
}
Ejemplo n.º 8
0
/**
 * @brief dmk_read_track - DMK format read track function
 *
 * This function reads an entire track into the track buffer.
 */
static int dmk_read_track(struct img_s *img, uint32_t cyl, uint32_t head,
	void *buff, uint32_t size, uint32_t den)
{
	uint32_t cyllen, heads;
	uint8_t *dst = buff, *src;
	dmk_t *dmk;
	off_t offs;

	if (0 != dmk_setup(img, &dmk))
		return -1;
	heads = (dmk->flags & DMK_FLAG_SSIDE) ? 1 : 2;
	cyllen = dmk->cyllen[0] + 256 * dmk->cyllen[1];

	offs = DMK_HEADER_SIZE + (off_t)cyllen * (cyl * heads + head);

#if	DMK_CACHE_TRACK
	dmk->rsvd[DMK_TRACK_BUFF] = 0xff;
	dmk->rsvd[DMK_SIDE_BUFF] = 0xff;
#endif

	/* zap idam and track before trying to read it from the image */
	memset(dmk->track, 0xff, cyllen);
	memset(dmk->track, 0x00, DMK_IDAM_SIZE);
	if (cyllen != img_read(img, offs, dmk->track, cyllen))
		return -1;
	src = dmk->track + DMK_IDAM_SIZE;
	cyllen -= DMK_IDAM_SIZE;
	while (cyllen > 0 && size > 0) {
		*dst++ = *src++;
		cyllen--;
		if (den == DEN_FM_LO && 0 == (dmk->flags & DMK_FLAG_SD1BYTE)) {
			src++;
			cyllen--;
		}
		size--;
	}
	while (size > 0) {
		*dst++ = 0xff;
		size--;
	}
#if	DMK_CACHE_TRACK
	dmk->rsvd[DMK_TRACK_BUFF] = cyl;
	dmk->rsvd[DMK_SIDE_BUFF] = head;
#endif
	return 0;
}
Ejemplo n.º 9
0
/**
 * @brief def_read_sector - default read sector function
 *
 * This assumes the default image type with disk geometry
 * defined by the system driver.
 */
static int def_read_sector(struct img_s *img, uint32_t head, uint32_t sec,
	void *buff, size_t size, fdd_chrn_id_t *id, uint32_t den)
{
	uint32_t heads, cyl, spt, sn0, len, lba;
	off_t offs;

	if (NULL == img)
		return -1;
	cyl = img_get_flag(img, DRV_CURRENT_CYLINDER);
	heads = img_get_flag(img, DRV_TOTAL_HEADS);
	spt = img_get_flag(img, DRV_SECTORS_PER_TRACK);
	sn0 = img_get_flag(img, DRV_FIRST_SECTOR_ID);
	len = img_get_flag(img, DRV_SECTOR_LENGTH);

	if (head >= heads)
		return -1;

	if (cyl >= img_get_flag(img, DRV_TOTAL_CYLINDERS))
		return -1;

	if (sec > spt)
		return -1;

	lba = (heads * cyl + head) * spt + sec - sn0;
	LOG((1,"DEF","read C:%02x H:%x R:%02x SPT:%02x (LBA:%x) size:0x%x\n",
		cyl, head, sec, spt, lba, size));
	id->C = cyl;
	id->H = head;
	id->R = sec;
	id->N = len >> 8;
	id->data_id = sec;
	id->flags = 0;

	if (0 != fdd_get_ddam(img, lba))
		id->flags |= ID_FLAG_DELETED_DATA;

	offs = (off_t)len * lba;

	if (size == img_read(img, offs, buff, size))
		return 0;
	return -1;
}
Ejemplo n.º 10
0
texture_t *texture_new_image(const char *path)
{
    texture_t *tex;
    uint8_t *img;
    int w, h, bpp = 0;
    img = img_read(path, &w, &h, &bpp);
    tex = calloc(1, sizeof(*tex));
    tex->path = strdup(path);
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = TF_HAS_TEX;
    tex->format = (int[]){0, 0, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}[bpp];
    texture_create_empty(tex);
    texture_set_data(tex, img, w, h, bpp);
    free(img);
    tex->ref = 1;
    LL_APPEND(g_textures, tex);
    return tex;
}
Ejemplo n.º 11
0
texture_t *texture_new_image(const char *path, int flags)
{
    texture_t *tex;
    uint8_t *img;
    int w, h, bpp = 0;
    img = img_read(path, &w, &h, &bpp);
    if (!img) {
        LOG_W("Cannot open image '%s'", path);
        return NULL;
    }
    tex = calloc(1, sizeof(*tex));
    tex->path = strdup(path);
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = TF_HAS_TEX | flags;
    tex->format = (int[]){0, 0, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}[bpp];
    texture_create_empty(tex);
    texture_set_data(tex, img, w, h, bpp);
    free(img);
    tex->ref = 1;
    return tex;
}
Ejemplo n.º 12
0
static int jv3_setup(struct img_s *img, jv3_t **pjv3)
{
	jv3_t *jv3;
	uint32_t drive;
	uint32_t cyl, sec, head, flg;
	uint32_t cyl_min, cyl_max;
	uint32_t sec_min, sec_max;
	size_t len_min, len_max;
	off_t offs;
	size_t size;
	int i, valid = 0;

	/* see if we already have a dmk struct */
	img_get_data(img, FDD_DATA_JV3, (void *)&jv3);
	if (NULL != jv3) {
		*pjv3 = jv3;
		return 0;
	}

	drive = img_minor(img);

	/* allocate a new dmk struct */
	jv3 = malloc(sizeof(jv3_t));
	if (NULL == jv3)
		return -1;

	memset(jv3, 0, sizeof(*jv3));

	/* read existing header, if any */
	if (JV3_HEADER_SIZE != img_read(img, 0, jv3->hdr, JV3_HEADER_SIZE)) {
		valid = 0;
	} else {
		/* got existing JV3 header */
		cyl_min = 0xff; cyl_max = 0x00;
		sec_min = 0xff; sec_max = 0x00;
		len_min = 0xffff; len_max = 0x0000;
		jv3->heads = 1;
		offs = JV3_HEADER_SIZE;
		for (i = 0; i < JV3_SECTORS; i++) {
			jv3_track_t *tp;
			jv3_dam_t *dp;

			cyl = jv3->hdr[0].table[3*i+0];
			sec = jv3->hdr[0].table[3*i+1];
			flg = jv3->hdr[0].table[3*i+2];
			if (cyl == 0xff)
				break;

			switch (flg & JV3_SIZE_MASK) {
			case JV3_SIZE_256:
				size = 256;
				break;
			case JV3_SIZE_128:
				size = 128;
				break;
			case JV3_SIZE_1024:
				size = 1024;
				break;
			default:
				size = 512;
				break;
			}

			if (cyl < cyl_min)
				cyl_min = cyl;
			if (cyl > cyl_max)
				cyl_max = cyl;
			if (sec < sec_min)
				sec_min = sec;
			if (sec > sec_max)
				sec_max = sec;
			if (size < len_min)
				len_min = size;
			if (size > len_max)
				len_max = size;
			if (flg & JV3_SIDE) {
				head = 1;
				jv3->heads = 2;
			} else {
				head = 0;
			}

			/* track info pointer */
			tp = &jv3->track[cyl][head];

			if (cyl + 1 > jv3->cylinders)
				jv3->cylinders = cyl + 1;

			/* sector info pointer */
			dp = &tp->sector[tp->sectors];

			/* increment sectors in this track */
			tp->sectors += 1;

			dp->size = size;
			dp->offs = offs;
			dp->oflg = 3 * i + 2;
			/* set data address mark status */
			dp->c = cyl;
			dp->h = (flg & JV3_SIDE) ? 1 : 0;
			dp->r = sec;
			dp->n = flg ^ 1;	/* toggle bit 0 */

			LOG((1,"JV3","#%x setup C:%02x H:%x R:%02x N:%02x @ 0x%x\n",
				drive, dp->c, dp->h, dp->r, dp->n, (uint32_t)offs));
			offs += size;
		}
	}

	if (0 == valid) {
		/* create a new JV3 header with no sectors */
		memset(jv3->hdr, 0xff, sizeof(jv3->hdr));
		jv3->wrprot = 0;
	}

	/* set the image data to our jv3 struct */
	if (0 != img_set_data(img, FDD_DATA_JV3, jv3)) {
		free(jv3);
		return -1;
	}
	*pjv3 = jv3;

	img_set_flag(img, DRV_TOTAL_HEADS, jv3->heads);
	img_set_flag(img, DRV_TOTAL_CYLINDERS, jv3->cylinders);
	img_set_flag(img, IMG_FORMAT, FDD_FORMAT_JV3);

	return 0;
}
Ejemplo n.º 13
0
int getrow( IMAGE *image, IMushort *buffer, int y, int z )
{
    register int i;
    register unsigned char *cptr;
    register IMushort *sptr;
    register int cnt;

    if( !(image->flags & (_IORW|_IOREAD)) )
	return -1;
    if(image->dim<3)
	z = 0;
    if(image->dim<2)
	y = 0;
    img_seek(image, y, z);
    if(ISVERBATIM(image->type)) {
	switch(BPP(image->type)) {
	    case 1:
		if (img_read(image,(char *)image->tmpbuf,image->xsize)
							    != image->xsize)
		    return -1;
		else {
		    cptr = (unsigned char *)image->tmpbuf;
		    sptr = buffer;
		    for(i=image->xsize; i--;)
			*sptr++ = *cptr++;
		}
		return image->xsize;
	    case 2:
		cnt = image->xsize<<1;
		if (img_read(image,(char *)buffer,cnt) != cnt)
		    return -1;
		else {
		    if(image->dorev)
			cvtshorts(buffer,cnt);
		    return image->xsize;
		}
	    default:
		i_errhdlr("getrow: wierd bpp\n");
		break;
	}
    } else if(ISRLE(image->type)) {
	switch(BPP(image->type)) {
	    case 1:
		if( (cnt = img_getrowsize(image)) == -1 )
		    return -1;
		if( img_read(image,(char *)image->tmpbuf,cnt) != cnt )
		    return -1;
		else {
		    img_rle_expand(image->tmpbuf,1,buffer,2);
		    return image->xsize;
		}
	    case 2:
		if( (cnt = img_getrowsize(image)) == -1 )
		    return -1;
		if( cnt != img_read(image,(char *)image->tmpbuf,cnt) )
		    return -1;
		else {
		    if(image->dorev)
			cvtshorts(image->tmpbuf,cnt);
		    img_rle_expand(image->tmpbuf,2,buffer,2);
		    return image->xsize;
		}
	    default:
		i_errhdlr("getrow: wierd bpp\n");
		break;
	}
    } else
	i_errhdlr("getrow: wierd image type\n");
    return 0;
}
Ejemplo n.º 14
0
static int dmk_setup(struct img_s *img, dmk_t **pdmk)
{
	dmk_t *dmk;
	uint32_t cyllen, heads;
	int valid = 1;

	/* see if we already have a dmk struct */
	img_get_data(img, FDD_DATA_DMK, (void *)&dmk);
	if (NULL != dmk) {
		*pdmk = dmk;
		return 0;
	}

	/* allocate a new dmk struct */
	dmk = malloc(sizeof(dmk_t));
	if (NULL == dmk)
		return -1;

	/* read existing header, if any */
	if (DMK_HEADER_SIZE != img_read(img, 0, dmk, DMK_HEADER_SIZE)) {
		valid = 0;
	} else {
		/* got existing DMK header */
		cyllen = dmk->cyllen[0] + 256 * dmk->cyllen[1];

		/* what is a sane minimum track length? */
		if (cyllen < 1024)
			valid = 0;
		/* this is certainly a wrong track length */
		if (cyllen > 0x7fff)
			valid = 0;

		/* reallocate the dmk struct to the track length */
		dmk = realloc(dmk, DMK_HEADER_SIZE + cyllen);
		if (NULL == dmk)
			return -1;
		memset(dmk->rsvd, 0xff, sizeof(dmk->rsvd));
		LOG((3,"FDD","DMK setup %u cylinders, %u heads, cyllen %#x\n",
			dmk->cylinders, (dmk->flags & DMK_FLAG_SSIDE) ? 1 : 2,
				cyllen));
	}

	if (0 == valid) {
		/* create a new DMK header */
		cyllen = DMK_TRACK_SIZE;
		dmk->wprot = 0;
		dmk->cylinders = 0;
		dmk->flags = 0;
		memset(dmk->rsvd, 0xff, sizeof(dmk->rsvd));
		dmk->cyllen[0] = cyllen % 256;
		dmk->cyllen[1] = cyllen / 256;
		memset(dmk->realdsk, 0, sizeof(dmk->realdsk));
		/* fill the idam and track buffers */
		memset(dmk->track, 0xff, cyllen);
		memset(dmk->track, 0x00, DMK_IDAM_SIZE);
	}

	/* set the image data to our dmk struct */
	if (0 != img_set_data(img, FDD_DATA_DMK, dmk)) {
		free(dmk);
		return -1;
	}
	*pdmk = dmk;

	heads = (dmk->flags & DMK_FLAG_SSIDE) ? 1 : 2;
	img_set_flag(img, DRV_TOTAL_HEADS, heads);
	img_set_flag(img, DRV_TOTAL_CYLINDERS, dmk->cylinders);
	img_set_flag(img, IMG_FORMAT, FDD_FORMAT_DMK);

	return 0;
}
Ejemplo n.º 15
0
int main (int argc, char *argv [])
{
  FILE *in, *out;

  clock_t a,b;

  img_handle img, cbf_img;

  cbf_handle cbf;

  int id, index;

  unsigned int column, row;

  size_t nelem_read;

  double pixel_size, gain, wavelength, distance;

  int overload, dimension [2], precedence [2];

  const char *detector;

  char *detector_char;

  char detector_id [64];

  const char *direction [2], *array_id;


     /* Usage */ 

  if (argc < 3)
  {
    fprintf (stderr, "\n Usage: %s imagefile cbffile\n", argv [0]);

    exit (2);
  }


    /* Read the image */

  img = img_make_handle ();

  a = clock ();

  cbf_failnez (img_read (img, argv [1]))

  b = clock ();

  fprintf (stderr, " Time to read the image: %.3fs\n", ((b - a) * 1.0) / CLOCKS_PER_SEC);


    /* Get some detector parameters */

    /* Detector identifier */

  detector = img_get_field (img, "DETECTOR");

  if (!detector)

    detector = "unknown";

  strncpy (detector_id, detector, 63);

  detector_id [63] = 0;

  detector_char = detector_id;

  while (*detector_char)

    if (isspace (*detector_char))

      memmove (detector_char, detector_char + 1, strlen (detector_char));

    else
    {
      *detector_char = tolower (*detector_char);

      detector_char++;
    }


    /* Pixel size */
    
  pixel_size = img_get_number (img, "PIXEL SIZE") * 0.001;


    /* Wavelength */

  wavelength = img_get_number (img, "WAVELENGTH");
  

    /* Distance */

  distance = img_get_number (img, "DISTANCE") * 0.001;
  

    /* Image size and orientation & gain and overload */

  if (strcmp (detector_id, "mar180") == 0 ||
      strcmp (detector_id, "mar300") == 0)
  {
    gain = 1.08;

    overload = 120000;

    dimension [0] = img_rows (img);
    dimension [1] = img_columns (img);

    precedence [0] = 1;
    precedence [1] = 2;

    direction [0] = "decreasing";
    direction [1] = "increasing";
  }
  else

    if (strcmp (detector_id, "mar345") == 0)
    {
      gain = 1.55;

      overload = 240000;

      dimension [0] = img_columns (img);
      dimension [1] = img_rows (img);

      precedence [0] = 2;
      precedence [1] = 1;

      direction [0] = "increasing";
      direction [1] = "increasing";
    }
    else

      if (strncmp (detector_id, "adscquantum", 11) == 0)
      {
        gain = 0.20;

        overload = 65000;

        dimension [0] = img_columns (img);
        dimension [1] = img_rows (img);

        precedence [0] = 2;
        precedence [1] = 1;

        direction [0] = "increasing";
        direction [1] = "increasing";
      }
      else
      {
        gain = 0.0;

        overload = 0;

        dimension [0] = img_rows (img);
        dimension [1] = img_columns (img);

        precedence [0] = 1;
        precedence [1] = 2;

        direction [0] = NULL;
        direction [1] = NULL;
      }


    /* Make a cbf version of the image */

  a = clock ();
                                                

    /* Create the cbf */

  cbf_failnez (cbf_make_handle (&cbf))


    /* Make a new data block */

  cbf_failnez (cbf_new_datablock (cbf, "image_1"))


    /* Make the _diffrn category */

  cbf_failnez (cbf_new_category (cbf, "diffrn"))
  cbf_failnez (cbf_new_column   (cbf, "id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))


    /* Make the _diffrn_source category */

  cbf_failnez (cbf_new_category (cbf, "diffrn_source"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "source"))
  cbf_failnez (cbf_set_value    (cbf, "synchrotron"))
  cbf_failnez (cbf_new_column   (cbf, "type"))
  cbf_failnez (cbf_set_value    (cbf, "ssrl crystallography"))


    /* Make the _diffrn_radiation category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_radiation"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "wavelength_id"))
  cbf_failnez (cbf_set_value    (cbf, "L1"))


    /* Make the _diffrn_radiation_wavelength category */

  cbf_failnez (cbf_new_category    (cbf, "diffrn_radiation_wavelength"))
  cbf_failnez (cbf_new_column      (cbf, "id"))
  cbf_failnez (cbf_set_value       (cbf, "L1"))
  cbf_failnez (cbf_new_column      (cbf, "wavelength"))

  if (wavelength)
  
    cbf_failnez (cbf_set_doublevalue (cbf, "%.4f", wavelength))

  cbf_failnez (cbf_new_column      (cbf, "wt"))
  cbf_failnez (cbf_set_value       (cbf, "1.0"))


    /* Make the _diffrn_measurement category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_measurement"))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "method"))
  cbf_failnez (cbf_set_value    (cbf, "oscillation"))
  cbf_failnez (cbf_new_column   (cbf, "sample_detector_distance"))

  if (distance)

    cbf_failnez (cbf_set_doublevalue (cbf, "%.4f", distance))


    /* Make the _diffrn_detector category */  

  cbf_failnez (cbf_new_category (cbf, "diffrn_detector"))
  cbf_failnez (cbf_new_column   (cbf, "id"))
  cbf_failnez (cbf_set_value    (cbf, detector_id))
  cbf_failnez (cbf_new_column   (cbf, "diffrn_id"))
  cbf_failnez (cbf_set_value    (cbf, "DS1"))
  cbf_failnez (cbf_new_column   (cbf, "type"))
  cbf_failnez (cbf_set_value    (cbf, detector))


    /* Make the _diffrn_detector_element category */  

  cbf_failnez (cbf_new_category     (cbf, "diffrn_detector_element"))
  cbf_failnez (cbf_new_column       (cbf, "id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "detector_id"))
  cbf_failnez (cbf_set_value        (cbf, detector_id))


    /* Make the _diffrn_frame_data category */  

  cbf_failnez (cbf_new_category     (cbf, "diffrn_frame_data"))
  cbf_failnez (cbf_new_column       (cbf, "id"))
  cbf_failnez (cbf_set_value        (cbf, "frame_1"))
  cbf_failnez (cbf_new_column       (cbf, "detector_element_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "detector_id"))
  cbf_failnez (cbf_set_value        (cbf, detector_id))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))


    /* Make the _array_structure_list category */  

  cbf_failnez (cbf_new_category     (cbf, "array_structure_list"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_row          (cbf))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "index"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 2))
  cbf_failnez (cbf_new_column       (cbf, "dimension"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, dimension [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, dimension [1]))
  cbf_failnez (cbf_new_column       (cbf, "precedence"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, precedence [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, precedence [1]))
  cbf_failnez (cbf_new_column       (cbf, "direction"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_value        (cbf, direction [0]))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_value        (cbf, direction [1]))


    /* Make the _array_element_size category */

  cbf_failnez (cbf_new_category     (cbf, "array_element_size"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_row          (cbf))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "index"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_next_row         (cbf))
  cbf_failnez (cbf_set_integervalue (cbf, 2))
  cbf_failnez (cbf_new_column       (cbf, "size"))

  if (pixel_size > 0)
  {
    cbf_failnez (cbf_rewind_row       (cbf))
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.1fe-6", pixel_size * 1e6))
    cbf_failnez (cbf_next_row         (cbf))
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.1fe-6", pixel_size * 1e6))
  }


    /* Make the _array_intensities category */

  cbf_failnez (cbf_new_category     (cbf, "array_intensities"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "linearity"))
  cbf_failnez (cbf_set_value        (cbf, "linear"))
  cbf_failnez (cbf_new_column       (cbf, "gain"))

  if (gain)
  
    cbf_failnez (cbf_set_doublevalue  (cbf, "%.3g", gain))
    
  cbf_failnez (cbf_new_column       (cbf, "overload"))

  if (overload)
    
    cbf_failnez (cbf_set_integervalue (cbf, overload))
    
  cbf_failnez (cbf_new_column       (cbf, "undefined"))
  cbf_failnez (cbf_set_integervalue (cbf, 0))


    /* Make the _array_data category */

  cbf_failnez (cbf_new_category     (cbf, "array_data"))
  cbf_failnez (cbf_new_column       (cbf, "array_id"))
  cbf_failnez (cbf_set_value        (cbf, "image_1"))
  cbf_failnez (cbf_new_column       (cbf, "binary_id"))
  cbf_failnez (cbf_set_integervalue (cbf, 1))
  cbf_failnez (cbf_new_column       (cbf, "data"))


    /* Save the binary data */

  cbf_failnez (cbf_set_integerarray_wdims_fs (cbf, CBF_PACKED|CBF_FLAT_IMAGE, 1,
                                 img_pixelptr (img, 0, 0), sizeof (int), 1,
                                 img_rows (img) * img_columns (img),
                                 "little_endian",img_rows (img),img_columns (img),0,0 ))
  

    /* Write the new file */

  out = fopen (argv [2], "w+b");

  if (!out)
  {
    fprintf (stderr, " Couldn't open the CBF file %s\n", argv [2]);

    exit (1);
  }

  cbf_failnez (cbf_write_file (cbf, out, 1, CBF, MSG_DIGEST | MIME_HEADERS  , 0))
  

    /* Free the cbf */

  cbf_failnez (cbf_free_handle (cbf))

  b = clock ();

  fprintf (stderr, " Time to write the CBF image: %.3fs\n", 
                                      ((b - a) * 1.0) / CLOCKS_PER_SEC); 


    /* Read the CBF file and compare the image to the original */
    
  a = clock ();


    /* Create the cbf */

  cbf_failnez (cbf_make_handle (&cbf))


    /* Read the file */

  in = fopen (argv [2], "rb");

  if (!in)
  {
    fprintf (stderr, " Couldn't reopen the CBF file %s\n", argv [2]);

    exit (1);
  }

  cbf_failnez (cbf_read_file (cbf, in, MSG_DIGEST))


    /* Get the image identifier */

  cbf_failnez (cbf_rewind_datablock (cbf))
  cbf_failnez (cbf_find_category    (cbf, "diffrn_frame_data"))
  cbf_failnez (cbf_find_column      (cbf, "array_id"))
  cbf_failnez (cbf_get_value        (cbf, &array_id))
  

    /* Get the image dimensions (second dimension = fast, first = slow) */

  cbf_failnez (cbf_find_category    (cbf, "array_structure_list"))
  cbf_failnez (cbf_rewind_row       (cbf))
  cbf_failnez (cbf_find_column      (cbf, "array_id"))

  dimension [0] = dimension [1] = 0;

  while (cbf_find_nextrow (cbf, array_id) == 0)
  {
    cbf_failnez (cbf_find_column      (cbf, "precedence"))
    cbf_failnez (cbf_get_integervalue (cbf, &index))

    if (index >= 1 && index <= 2)
    {
      cbf_failnez (cbf_find_column (cbf, "dimension"))

      cbf_failnez (cbf_get_integervalue (cbf, &dimension [2 - index]))
    }
    else

      exit (1);

    cbf_failnez (cbf_find_column (cbf, "array_id"))
  }

  if (dimension [0] == 0 || dimension [1] == 0)

    exit (1);


    /* Create the new image */

  cbf_img = img_make_handle ();

  img_set_dimensions (cbf_img, dimension [0], dimension [1]);


    /* Find the binary data */
  
  cbf_failnez (cbf_find_category (cbf, "array_data"))
  cbf_failnez (cbf_find_column   (cbf, "array_id"))
  cbf_failnez (cbf_find_row      (cbf, array_id))
  cbf_failnez (cbf_find_column   (cbf, "data"))


    /* Read the binary data */
  
  cbf_failnez (cbf_get_integerarray (cbf,
                                 &id, img_pixelptr (cbf_img, 0, 0), sizeof (int), 1,
                                 img_rows (cbf_img) * img_columns (cbf_img), &nelem_read))


    /* Free the cbf */

  cbf_failnez (cbf_free_handle (cbf))

  b = clock ();

  fprintf (stderr, " Time to read the CBF image: %.3fs\n", 
                                     ((b - a) * 1.0) / CLOCKS_PER_SEC);


    /* Compare the images */

  if (img_rows (img) != img_rows (cbf_img) || img_columns (img) != img_columns (cbf_img))
  {
    fprintf (stderr, " The dimensions of the CBF image don't match the original\n");

    exit (1);
  }

  for (column = 0; column < (unsigned int) img_columns (cbf_img); column++)

    for (row = 0; row < (unsigned int) img_rows (cbf_img); row++)

      if (img_pixel (cbf_img, column, row) != img_pixel (img, column, row))
      {
        fprintf (stderr, " The CBF image differs from the original at (%d, %d)\n", column, row);

        exit (1);
      }

  fprintf (stderr, " The CBF image matches the original\n");


    /* Free the images */

  img_free_handle (img);

  img_free_handle (cbf_img);


    /* Success */

  return 0;
}
Ejemplo n.º 16
0
/**
 * @brief jv3_read_track - JV3 format read an entire track
 *
 * This assumes the JV3 image type.
 */
static int jv3_read_track(struct img_s *img, uint32_t cyl, uint32_t head,
	void *buff, size_t size, uint32_t den)
{
	jv3_t *jv3;
	jv3_track_t *tp;
	jv3_dam_t *dp;
	uint8_t *dst;
	uint32_t crc = 0xffff;
	size_t left, count;
	uint32_t i;
	int state;

	if (0 != jv3_setup(img, &jv3))
		return -1;

	if (cyl >= jv3->cylinders)
		return -1;
	if (head >= jv3->heads)
		return -1;

	state = 0;
	count = 11;
	i = 0;
	dst = buff;
	left = size;
	tp = &jv3->track[cyl][head];
	dp = tp->sector;
	if (den == DEN_FM_LO) {
		/* fill in a single density track */
		while (left-- > 0) {
			switch (state) {
			case 0:		/* pre AM gap 0xff */
				*dst++ = 0xff;
				if (--count == 0) {
					count = 8;
					state++;
				}
				break;
			case 1:		/* pre AM gap 0x00 */
				*dst++ = 0x00;
				if (--count == 0)
					state++;
				break;
			case 2:		/* AM (address mark) */
				crc = 0xffff;
				crc = calc_crc(crc, 0xfe);
				*dst++ = 0xfe;
				state++;
				break;
			case 3: 	/* cylinder number */
				crc = calc_crc(crc, dp->c);
				*dst++ = dp->c;
				state++;
				break;
			case 4: 	/* head number */
				crc = calc_crc(crc, dp->h);
				*dst++ = dp->h;
				state++;
				break;
			case 5: 	/* record number */
				crc = calc_crc(crc, dp->r);
				*dst++ = dp->r;
				state++;
				break;
			case 6: 	/* sector length */
				crc = calc_crc(crc, dp->n & 0x03);
				*dst++ = dp->n & 0x03;
				state++;
				break;
			case 7: 	/* AM CRC high */
				*dst++ = crc / 256;
				state++;
				break;
			case 8: 	/* AM CRC low */
				*dst++ = crc % 256;
				count = 8;
				state++;
				break;
			case 9:		/* pre DAM gap 0xff */
				*dst++ = 0xff;
				if (--count == 0) {
					count = 8;
					state++;
				}
				break;
			case 10:	/* pre DAM gap 0x00 */
				*dst++ = 0x00;
				if (--count == 0) {
					crc = 0xffff;
					state++;
				}
				break;
			case 11:	/* DAM */
				switch (dp->n & JV3_DAM_MASK) {
				case JV3_DAM_SD_STD:
					crc = calc_crc(crc, 0xfb);
					*dst++ = 0xfb;
					break;
				case JV3_DAM_SD_DEL:
					crc = calc_crc(crc, 0xfa);
					*dst++ = 0xfa;
					break;
				case JV3_DAM_DD_DEL:
					crc = calc_crc(crc, 0xf8);
					*dst++ = 0xf8;
					break;
				}
				state++;
				count = 0;
				if (left < dp->size)
					img_read(img, dp->offs, dst, dp->size);
				else
					img_read(img, dp->offs, dst, left);
				break;
			case 12:	/* sector data */
				crc = calc_crc(crc, *dst++);
				if (++count == dp->size)
					state++;
				break;
			case 13:	/* DAM CRC high */
				*dst++ = crc / 256;
				state++;
				break;
			case 14:	/* DAM CRC low */
				*dst++ = crc % 256;
				i++;
				if (i >= tp->sectors) {
					state++;
					break;
				}
				/* next sector */
				count = 11;
				state = 0;
				dp = &tp->sector[i];
				break;
			case 15:	/* pad track with 0xff */
				*dst++ = 0xff;
				break;
			}
		}
	} else {
		/* fill in a double density track */
		while (left-- > 0) {
			switch (state) {
			case 0:		/* pre AM gap 0x4e */
				*dst++ = 0x4e;
				if (--count == 0) {
					count = 8;
					state++;
				}
				break;
			case 1:		/* pre AM gap 0x00 */
				*dst++ = 0x00;
				if (--count == 0) {
					crc = 0xffff;
					count = 3;
					state++;
				}
				break;
			case 2:		/* reset CRC 0xf5 (read as 0xa1) */
				*dst++ = 0xa1;
				crc = calc_crc(crc, 0xa1);
				if (--count == 0)
					state++;
				break;
			case 3:		/* AM (address mark) */
				crc = calc_crc(crc, 0xfe);
				*dst++ = 0xfe;
				state++;
				break;
			case 4: 	/* cylinder number */
				crc = calc_crc(crc, dp->c);
				*dst++ = dp->c;
				state++;
				break;
			case 5: 	/* head number */
				crc = calc_crc(crc, dp->h);
				*dst++ = dp->h;
				state++;
				break;
			case 6: 	/* record number */
				crc = calc_crc(crc, dp->r);
				*dst++ = dp->r;
				state++;
				break;
			case 7: 	/* sector length */
				crc = calc_crc(crc, dp->n & 0x03);
				*dst++ = dp->n & 0x03;
				state++;
				break;
			case 8: 	/* AM CRC high */
				*dst++ = crc / 256;
				state++;
				break;
			case 9: 	/* AM CRC low */
				*dst++ = crc % 256;
				count = 8;
				state++;
				break;
			case 10:	/* pre DAM gap 0x4e */
				*dst++ = 0x4e;
				if (--count == 0) {
					count = 8;
					state++;
				}
				break;
			case 11:	/* pre DAM gap 0x00 */
				*dst++ = 0x00;
				if (--count == 0) {
					crc = 0xffff;
					count = 3;
					state++;
				}
				break;
			case 12:	/* reset CRC 0xf5 (read as 0xa1) */
				*dst++ = 0xa1;
				crc = calc_crc(crc, 0xa1);
				if (--count == 0)
					state++;
				break;
			case 13:	/* DAM */
				switch (dp->n & JV3_DAM_MASK) {
				case JV3_DAM_DD_STD:
					crc = calc_crc(crc, 0xfb);
					*dst++ = 0xfb;
					break;
				case JV3_DAM_SD_DEL:
					crc = calc_crc(crc, 0xf8);
					*dst++ = 0xf8;
					break;
				case JV3_DAM_DD_DEL:
					crc = calc_crc(crc, 0xf8);
					*dst++ = 0xf8;
					break;
				}
				state++;
				count = 0;
				if (left < dp->size)
					img_read(img, dp->offs, dst, left);
				else
					img_read(img, dp->offs, dst, dp->size);
				break;
			case 14:	/* sector data */
				crc = calc_crc(crc, *dst++);
				if (++count == dp->size)
					state++;
				break;
			case 15:	/* DAM CRC high */
				*dst++ = crc / 256;
				state++;
				break;
			case 16:	/* DAM CRC low */
				*dst++ = crc % 256;
				i++;
				if (i >= tp->sectors) {
					state++;
					break;
				}
				/* next sector */
				count = 11;
				state = 0;
				dp = &tp->sector[i];
				break;
			case 17:	/* pad track with 0x4e */
				*dst++ = 0x4e;
				break;
			}
		}
	}
	return 0;
}
Ejemplo n.º 17
0
/**
 * @brief jv3_read_sector - JV3 format read sector function
 *
 * This assumes the JV3 image type.
 */
static int jv3_read_sector(struct img_s *img, uint32_t head, uint32_t sec,
	void *buff, size_t size, fdd_chrn_id_t *id, uint32_t den)
{
	jv3_t *jv3;
	jv3_track_t *tp;
	jv3_dam_t *dp;
	uint32_t drive, cyl;
	off_t offs;
	uint32_t i;

	if (0 != jv3_setup(img, &jv3))
		return -1;

	drive = img_minor(img);
	cyl = img_get_flag(img, DRV_CURRENT_CYLINDER);

	if (cyl >= jv3->cylinders)
		return -1;
	if (head >= jv3->heads)
		return -1;

	tp = &jv3->track[cyl][head];
	for (i = 0, dp = tp->sector; i < tp->sectors; i++, dp++)
		if (dp->r == sec)
			break;

	/* record not found? */
	if (i >= tp->sectors) {
		dp = &tp->sector[0];
		LOG((1,"JV3","#%x read sector %02x not found SPT:%02x\n",
			drive, sec, tp->sectors));
		id->C = dp->c;
		id->H = dp->h;
		id->R = dp->r;
		id->N = dp->n & 0x03;
		id->data_id = dp->r;
		id->flags = 0;
		return -1;
	}

	id->C = dp->c;
	id->H = dp->h;
	id->R = dp->r;
	id->N = dp->n & 0x03;
	id->data_id = dp->r;
	id->flags = 0;

	LOG((1,"JV3","#%x/%cD read C:%02x H:%x R:%02x N:%02x SPT:%02x\n",
		drive, DEN_FM_LO == den ? 'S' : 'D',
		id->C, id->H, id->R, id->N, tp->sectors));

	switch (dp->n & JV3_DAM_MASK) {
	case JV3_DAM_SD_STD:
	case JV3_DAM_DD_STD:
		id->flags &= ~ID_FLAG_DELETED_DATA;
		break;
	case JV3_DAM_SD_DEL:
	case JV3_DAM_DD_DEL:
		id->flags |= ID_FLAG_DELETED_DATA;
		break;
	}

	offs = dp->offs;

	if (size <= dp->size) {
		if (size == img_read(img, offs, buff, size))
			return 0;
	} else {
		memset(buff, 0xff, size);
		if (size == img_read(img, offs, buff, dp->size))
			return 0;
	}

	return -1;
}
Ejemplo n.º 18
0
int fdd_find_format(struct img_s *img)
{
	uint8_t *buff;
	jv3_header_t *jv3;
	dmk_t *dmk;
	uint32_t drive;
	uint32_t offs;
	uint8_t sec_min, sec_max;
	uint8_t cyl_min, cyl_max;
	uint8_t heads;
	size_t size;

	if (NULL == img)
		return -1;

	if (0 == img_get_flag(img, IMG_EXISTS))
		return -1;

	buff = malloc(0x10000);

	drive = img_minor(img);
	img_set_geometry(img, 1, 1, 1, 256, 0, 0, 0);
	img_read(img, 0, buff, 0x10000);

	/* try JV3 format */
	jv3 = (jv3_header_t *)buff;
	sec_min = 0xff; sec_max = 0x00;
	cyl_min = 0xff; cyl_max = 0x00;
	size = JV3_HEADER_SIZE;
	heads = 1;
	for (offs = 0; offs < JV3_SECTORS; offs++) {
		uint8_t cyl, sec, flg;
		cyl = jv3->table[3*offs + 0];
		sec = jv3->table[3*offs + 1];
		flg = jv3->table[3*offs + 2];
		if (cyl == 0xff)
			break;
		if (cyl < cyl_min)
			cyl_min = cyl;
		if (cyl > cyl_max)
			cyl_max = cyl;
		if (sec < sec_min)
			sec_min = sec;
		if (sec > sec_max)
			sec_max = sec;
		if (flg & JV3_SIDE)
			heads = 2;
		switch (flg & JV3_SIZE_MASK) {
		case JV3_SIZE_256:
			size += 256;
			break;
		case JV3_SIZE_128:
			size += 128;
			break;
		case JV3_SIZE_1024:
			size += 1024;
			break;
		case JV3_SIZE_512:
			size += 512;
			break;
		}
	}
	if (size == img_get_size(img)) {
		free(buff);
		LOG((3,"JV3","#%x found %u heads, cylinders %u to %u, sectors %02x to %02x\n",
			drive, heads, cyl_min, cyl_max, sec_min, sec_max));
		jv3_set_geometry(img);
		return 0;
	}

	/* try DMK format */
	dmk = (dmk_t *)buff;
	if (dmk->cylinders < 0xe0) {
		uint32_t cyllen;
		uint32_t heads;

		cyllen = dmk->cyllen[0] + 256 * dmk->cyllen[1];
		heads = (dmk->flags & DMK_FLAG_SSIDE) ? 1 : 2;
		/* possible track length? */
		if (cyllen > 1024 && cyllen < 32768) {
			size = img_get_size(img) - DMK_HEADER_SIZE;
			/* check if the size matches */
			LOG((3,"DMK","#%x found cyllen %u, heads %u, size %u\n",
				drive, cyllen, heads, (uint32_t)size));
			/* if we have a whole number of cylinders... */
			if (0 == (size % cyllen)) {
				dmk_set_geometry(img);
				free(buff);
				return 0;
			}
		}
	}

	free(buff);
	return -1;
}
Ejemplo n.º 19
0
int
kv_init(const char *dirname)
{
	img_t *mask;
	kv_mask_t *kmp;
	DIR *maskdir;
	struct dirent *entp;
	char *p;
	char maskname[PATH_MAX];
	char maskdirname[PATH_MAX];

	if (kv_nmasks > 0)
		/* already initialized */
		return (0);

	/*
	 * For now, rather than explicitly enumerate the masks and check each
	 * one, we iterate the masks we have, see which ones match this image,
	 * and update the screen info accordingly.
	 */
	(void) snprintf(maskdirname, sizeof (maskdirname),
	    "%s/../assets/masks", dirname);

	if ((maskdir = opendir(maskdirname)) == NULL) {
		warn("failed to opendir %s", maskdirname);
		return (-1);
	}

	while ((entp = readdir(maskdir)) != NULL) {
		if (kv_nmasks == KV_MAX_MASKS) {
			warnx("too many masks (over %d)", KV_MAX_MASKS);
			(void) closedir(maskdir);
			return (-1);
		}

		p = entp->d_name + strlen(entp->d_name) - sizeof (".png") + 1;
		if (strcmp(p, ".png") != 0)
			continue;

		if (strncmp(entp->d_name, "char_", sizeof ("char_") - 1) != 0 &&
		    strncmp(entp->d_name, "pos", sizeof ("pos") - 1) != 0 &&
		    strncmp(entp->d_name, "item_", sizeof ("item_") - 1) != 0 &&
		    strncmp(entp->d_name, "lakitu_start",
		    sizeof ("lakitu_start") - 1) != 0 &&
		    strncmp(entp->d_name, "track_", sizeof ("track_") - 1) != 0)
			continue;

		if (kv_debug > 2)
			(void) printf("reading mask %-20s: ", entp->d_name);

		(void) snprintf(maskname, sizeof (maskname), "%s/%s",
		    maskdirname, entp->d_name);

		if ((mask = img_read(maskname)) == NULL) {
			warnx("failed to read %s", maskname);
			(void) closedir(maskdir);
			return (-1);
		}

		kmp = &kv_masks[kv_nmasks++];
		kmp->km_image = mask;
		(void) strlcpy(kmp->km_name, entp->d_name,
		    sizeof (kmp->km_name));

		if (kv_debug > 2)
			(void) printf("bounded [%d, %d] to [%d, %d]\n",
			    mask->img_minx, mask->img_miny, mask->img_maxx,
			    mask->img_maxy);
	}

	(void) closedir(maskdir);

	/*
	 * It's important that we check position masks before others so that
	 * ks_nplayers is set correctly.
	 */
	qsort(kv_masks, kv_nmasks, sizeof (kv_masks[0]),
	    (int (*)(const void *, const void *))kv_mask_compare);
	return (0);
}
Ejemplo n.º 20
0
/*
 * frames input ...: emit events describing game state changes in video frames
 */
static int
cmd_frames(int argc, char *argv[])
{
	DIR *dirp;
	struct dirent *entp;
	int nframes, rv, i, len;
	kv_emit_f emit;
	char c;
	char *q;
	img_t *image;
	kv_vidctx_t *kvp;
	char *framenames[MAX_FRAMES];

	emit = kv_screen_print;

	while ((c = getopt(argc, argv, "j")) != -1) {
		switch (c) {
		case 'j':
			emit = kv_screen_json;
			break;

		case '?':
		default:
			return (EXIT_USAGE);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1) {
		warnx("missing directory name");
		return (EXIT_USAGE);
	}

	if ((kvp = kv_vidctx_init(dirname((char *)kv_arg0), emit, NULL)) == NULL)
		return (EXIT_FAILURE);

	if ((dirp = opendir(argv[0])) == NULL) {
		kv_vidctx_free(kvp);
		warn("failed to opendir %s", argv[0]);
		return (EXIT_USAGE);
	}

	nframes = 0;
	rv = EXIT_FAILURE;
	while ((entp = readdir(dirp)) != NULL) {
		if (nframes >= MAX_FRAMES) {
			warnx("max %d frames supported", MAX_FRAMES);
			break;
		}

		if (strcmp(entp->d_name, ".") == 0 ||
		    strcmp(entp->d_name, "..") == 0)
			continue;

		if (strcmp(entp->d_name + strlen(entp->d_name) -
		    sizeof (".png") + 1, ".png") != 0)
			continue;

		len = snprintf(NULL, 0, "%s/%s", argv[0], entp->d_name);
		if ((q = malloc(len + 1)) == NULL) {
			warn("malloc");
			break;
		}

		(void) snprintf(q, len + 1, "%s/%s", argv[0], entp->d_name);
		framenames[nframes++] = q;
	}

	(void) closedir(dirp);

	if (entp != NULL)
		goto out;

	rv = EXIT_SUCCESS;
	qsort(framenames, nframes, sizeof (framenames[0]), qsort_strcmp);

	for (i = 0; i < nframes; i++) {
		image = img_read(framenames[i]);

		if (image == NULL) {
			warnx("failed to read %s", argv[i]);
			continue;
		}

		kv_vidctx_frame(framenames[i], i,
		    i / KV_FRAMERATE * MILLISEC, image, kvp);
		img_free(image);
	}

out:
	kv_vidctx_free(kvp);

	for (i = 0; i < nframes; i++)
		free(framenames[i]);

	return (rv);
}