Пример #1
0
/**
 * R T _ B I N U N I F _ F R E E
 *
 * Free the storage associated with a binunif_internal object
 */
void
rt_binunif_free( struct rt_binunif_internal *bip) {
    RT_CK_BINUNIF(bip);
    bu_free( (genptr_t) bip->u.uint8, "binunif free uint8" );
    bu_free( bip, "binunif free");
    bip = GENPTR_NULL; /* sanity */
}
Пример #2
0
/**
 * R T _ B I N U N I F _ D U M P
 *
 * Diagnostic routine
 */
void
rt_binunif_dump( struct rt_binunif_internal *bip) {
    RT_CK_BINUNIF(bip);
    bu_log("rt_bin_unif_internal <%x>...\n", bip);
    bu_log("  type = x%x = %d", bip -> type, bip -> type);
    bu_log("  count = %ld  first = 0x%02x", bip -> count,
	   bip->u.uint8[0] & 0x0ff);
    bu_log("- - - - -\n");
}
Пример #3
0
/**
 * R T _ B I N U N I F _ I F R E E
 *
 * Free the storage associated with the rt_db_internal version of this
 * thing.
 */
void
rt_binunif_ifree( struct rt_db_internal	*ip )
{
    struct rt_binunif_internal	*bip;

    RT_CK_DB_INTERNAL(ip);
    bip = (struct rt_binunif_internal *)ip->idb_ptr;
    RT_CK_BINUNIF(bip);
    bu_free( (genptr_t) bip->u.uint8, "binunif ifree" );
    bu_free( ip->idb_ptr, "binunif ifree" );
    ip->idb_ptr = GENPTR_NULL;
}
Пример #4
0
/**
 * R T _ B I N U N I F _ D E S C R I B E
 *
 * Make human-readable formatted presentation of this object.  First
 * line describes type of object.  Additional lines are indented one
 * tab, and give parameter values.
 */
int
rt_binunif_describe( struct bu_vls		*str,
		     const struct rt_db_internal	*ip,
		     int				verbose,
		     double			mm2local )
{
    register struct rt_binunif_internal	*bip;
    char					buf[256];
    unsigned short				wid;

    bip = (struct rt_binunif_internal *) ip->idb_ptr;
    RT_CK_BINUNIF(bip);
    bu_vls_strcat( str, "uniform-array binary object (BINUNIF)\n");
    wid = (bip->type & DB5_MINORTYPE_BINU_WID_MASK) >> 4;
    switch (wid) {
	case 0:
	    sprintf( buf, "%ld ", bip->count ); break;
	case 1:
	    sprintf( buf, "%ld pairs of ", bip->count / 2 ); break;
	case 2:
	    sprintf( buf, "%ld triples of ", bip->count / 3 ); break;
	case 3:
	    sprintf( buf, "%ld quadruples of ", bip->count / 4 ); break;
    }
    bu_vls_strcat( str, buf );
    switch (bip->type & DB5_MINORTYPE_BINU_ATM_MASK) {
	case DB5_MINORTYPE_BINU_FLOAT:
	    bu_vls_strcat( str, "floats\n"); break;
	case DB5_MINORTYPE_BINU_DOUBLE:
	    bu_vls_strcat( str, "doubles\n"); break;
	case DB5_MINORTYPE_BINU_8BITINT:
	    bu_vls_strcat( str, "8-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_16BITINT:
	    bu_vls_strcat( str, "16-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_32BITINT:
	    bu_vls_strcat( str, "32-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_64BITINT:
	    bu_vls_strcat( str, "64-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_8BITINT_U:
	    bu_vls_strcat( str, "unsigned 8-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_16BITINT_U:
	    bu_vls_strcat( str, "unsigned 16-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_32BITINT_U:
	    bu_vls_strcat( str, "unsigned 32-bit ints\n"); break;
	case DB5_MINORTYPE_BINU_64BITINT_U:
	    bu_vls_strcat( str, "unsigned 64-bit ints\n"); break;
	default:
	    bu_log("%s:%d: This shouldn't happen", __FILE__, __LINE__);
	    return(1);
    }

    return(0);
}
Пример #5
0
/*
 * This is a helper routine used in txt_setup() to load a texture either from
 * a file or from a db object.  The resources are released in txt_free()
 * (there is no specific unload_datasource function).
 */
HIDDEN int
txt_load_datasource(struct txt_specific *texture, struct db_i *dbInstance, const unsigned long int size)
{
    struct directory *dirEntry;

    RT_CK_DBI(dbInstance);

    if (texture == (struct txt_specific *)NULL) {
	bu_bomb("ERROR: txt_load_datasource() received NULL arg (struct txt_specific *)\n");
    }

    bu_log("Loading texture %s [%s]...", texture->tx_datasrc==TXT_SRC_AUTO?"from auto-determined datasource":texture->tx_datasrc==TXT_SRC_OBJECT?"from a database object":texture->tx_datasrc==TXT_SRC_FILE?"from a file":"from an unknown source (ERROR)", bu_vls_addr(&texture->tx_name));

    /* if the source is auto or object, we try to load the object */
    if ((texture->tx_datasrc==TXT_SRC_AUTO) || (texture->tx_datasrc==TXT_SRC_OBJECT)) {

	/* see if the object exists */
	if ((dirEntry=db_lookup(dbInstance, bu_vls_addr(&texture->tx_name), LOOKUP_QUIET)) == RT_DIR_NULL) {

	    /* unable to find the texture object */
	    if (texture->tx_datasrc!=TXT_SRC_AUTO) {
		return -1;
	    }
	} else {
	    struct rt_db_internal *dbip;

	    BU_ALLOC(dbip, struct rt_db_internal);

	    RT_DB_INTERNAL_INIT(dbip);
	    RT_CK_DB_INTERNAL(dbip);
	    RT_CK_DIR(dirEntry);

	    /* the object was in the directory, so go get it */
	    if (rt_db_get_internal(dbip, dirEntry, dbInstance, NULL, NULL) <= 0) {
		/* unable to load/create the texture database record object */
		return -1;
	    }

	    RT_CK_DB_INTERNAL(dbip);
	    RT_CK_BINUNIF(dbip->idb_ptr);

	    /* keep the binary object pointer */
	    texture->tx_binunifp=(struct rt_binunif_internal *)dbip->idb_ptr; /* make it so */

	    /* release the database instance pointer struct we created */
	    RT_DB_INTERNAL_INIT(dbip);
	    bu_free(dbip, "txt_load_datasource");

	    /* check size of object */
	    if (texture->tx_binunifp->count < size) {
		bu_log("\nWARNING: %s needs %d bytes, binary object only has %lu\n", bu_vls_addr(&texture->tx_name), size, texture->tx_binunifp->count);
	    } else if (texture->tx_binunifp->count > size) {
		bu_log("\nWARNING: Binary object is larger than specified texture size\n\tBinary Object: %zu pixels\n\tSpecified Texture Size: %zu pixels\n...continuing to load using image subsection...", texture->tx_binunifp->count);
	    }
	}
    }

    /* if we are auto and we couldn't find a database object match, or if source
     * is explicitly a file then we load the file.
     */
    if (((texture->tx_datasrc==TXT_SRC_AUTO) && (texture->tx_binunifp==NULL)) || (texture->tx_datasrc==TXT_SRC_FILE)) {

	texture->tx_mp = bu_open_mapped_file_with_path(dbInstance->dbi_filepath,	bu_vls_addr(&texture->tx_name), NULL);

	if (texture->tx_mp==NULL)
	    return -1;				/* FAIL */

	if (texture->tx_mp->buflen < size) {
	    bu_log("\nWARNING: %s needs %d bytes, file only has %lu\n", bu_vls_addr(&texture->tx_name), size, texture->tx_mp->buflen);
	} else if (texture->tx_mp->buflen > size) {
	    bu_log("\nWARNING: Texture file size is larger than specified texture size\n\tInput File: %zu pixels\n\tSpecified Texture Size: %lu pixels\n...continuing to load using image subsection...", texture->tx_mp->buflen, size);
	}

    }

    bu_log("done.\n");

    return 0;
}
Пример #6
0
int
rt_retrieve_binunif(struct rt_db_internal *intern,
		    struct db_i	*dbip,
		    char *name)
{
    register struct directory	*dp;
    struct rt_binunif_internal	*bip;
    struct bu_external		ext;
    struct db5_raw_internal		raw;
    char				*tmp;

    /*
     *Find the guy we're told to write
     */
    if ( (dp = db_lookup( dbip, name, LOOKUP_NOISY)) == DIR_NULL )
	return -1;

    RT_INIT_DB_INTERNAL(intern);
    if ( rt_db_get_internal5( intern, dp, dbip, NULL, &rt_uniresource)
	 != ID_BINUNIF     || db_get_external( &ext, dp, dbip ) < 0 )
	return -1;

    if (db5_get_raw_internal_ptr(&raw, ext.ext_buf) == NULL) {
	bu_log("%s:%d\n", __FILE__, __LINE__);
	bu_free_external( &ext );
	return -1;
    }
    if (db5_type_descrip_from_codes(&tmp, raw.major_type, raw.minor_type))
	tmp = 0;

    if (RT_G_DEBUG & DEBUG_VOL)
	bu_log("get_body() sees type (%d, %d)='%s'\n",
	       raw.major_type, raw.minor_type, tmp);

    if (raw.major_type != DB5_MAJORTYPE_BINARY_UNIF)
	return -1;

    bip = intern->idb_ptr;
    RT_CK_BINUNIF(bip);
    if (RT_G_DEBUG & DEBUG_HF)
	rt_binunif_dump(bip);

    if (RT_G_DEBUG & DEBUG_VOL)
	bu_log("cmd_export_body() thinks bip->count=%d\n",
	       bip->count);

    switch (bip -> type) {
	case DB5_MINORTYPE_BINU_FLOAT:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... float");
	    break;
	case DB5_MINORTYPE_BINU_DOUBLE:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... double");
	    break;
	case DB5_MINORTYPE_BINU_8BITINT:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 8bitint");
	    break;
	case DB5_MINORTYPE_BINU_8BITINT_U:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 8bituint");
	    break;
	case DB5_MINORTYPE_BINU_16BITINT:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 16bituint");
	    break;
	case DB5_MINORTYPE_BINU_16BITINT_U:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 16bitint");
	    break;
	case DB5_MINORTYPE_BINU_32BITINT:
	case DB5_MINORTYPE_BINU_32BITINT_U:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 32bitint");
	    break;
	case DB5_MINORTYPE_BINU_64BITINT:
	case DB5_MINORTYPE_BINU_64BITINT_U:
	    if (RT_G_DEBUG & DEBUG_VOL)
		bu_log("bip->type switch... 64bitint");
	    break;
	default:
	    /* XXX	This shouln't happen!!    */
	    bu_log("bip->type switch... default");
	    break;
    }

    bu_free_external( &ext );

    return 0;
}
Пример #7
0
/**
 * R T _ B I N U N I F _ E X P O R T 5
 *
 * Create the "body" portion of external form
 */
int
rt_binunif_export5( struct bu_external		*ep,
		    const struct rt_db_internal	*ip,
		    double			local2mm,	/* we ignore */
		    const struct db_i		*dbip,
		    struct resource		*resp,
		    const int			minor_type )
{
    struct rt_binunif_internal	*bip;
    int				i;
    unsigned char			*destp;
    unsigned long			*lsrcp;
    int				in_cookie, out_cookie;
    int				gotten;

    RT_CK_DB_INTERNAL(ip);
    if ( ip->idb_minor_type != minor_type ) {
	bu_log("ip->idb_minor_type(%d) != minor_type(%d)\n",
	       ip->idb_minor_type, minor_type );
	return -1;
    }
    bip = (struct rt_binunif_internal *)ip->idb_ptr;
    RT_CK_BINUNIF(bip);
    if ( bip->type != minor_type ) {
	bu_log("bip->type(%d) != minor_type(%d)\n",
	       bip->type, minor_type );
	return -1;
    }

    BU_INIT_EXTERNAL(ep);

    /*
     * Convert from internal (host) to database (network) format
     */
    switch (bip->type) {
	case DB5_MINORTYPE_BINU_FLOAT:
	    ep->ext_nbytes = bip->count * SIZEOF_NETWORK_FLOAT;
	    ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes,
					       "binunif external");
	    htonf( ep->ext_buf, (unsigned char *) bip->u.uint8, bip->count );
	    break;
	case DB5_MINORTYPE_BINU_DOUBLE:
	    ep->ext_nbytes = bip->count * SIZEOF_NETWORK_DOUBLE;
	    ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes,
					       "binunif external");
	    htond( ep->ext_buf, (unsigned char *) bip->u.uint8, bip->count );
	    break;
	case DB5_MINORTYPE_BINU_8BITINT:
	case DB5_MINORTYPE_BINU_8BITINT_U:
	    ep->ext_nbytes = bip->count;
	    ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes,
					       "binunif external");
	    memcpy((char *) ep->ext_buf, (char *) bip->u.uint8, bip->count);
	    break;
	case DB5_MINORTYPE_BINU_16BITINT:
	case DB5_MINORTYPE_BINU_16BITINT_U:
	    ep->ext_nbytes = bip->count * 2;
	    ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes, "binunif external");
	    in_cookie = bu_cv_cookie("hus");
	    out_cookie = bu_cv_cookie("nus");
	    if (bu_cv_optimize(in_cookie) != bu_cv_optimize(out_cookie)) {
		gotten =
		    bu_cv_w_cookie(ep->ext_buf, out_cookie,
				   ep->ext_nbytes,
				   (genptr_t) bip->u.uint8, in_cookie,
				   bip->count);

		if (gotten != bip->count) {
		    bu_log("%s:%d: Tried to convert %d, did %d",
			   __FILE__, __LINE__, bip->count, gotten);
		    bu_bomb("\n");
		}
	    } else {
		memcpy((char *) ep->ext_buf,
		       (char *) bip->u.uint8,
		       ep->ext_nbytes );
	    }
	    break;
	case DB5_MINORTYPE_BINU_32BITINT:
	case DB5_MINORTYPE_BINU_32BITINT_U:
	    ep->ext_nbytes = bip->count * 4;
	    ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes, "binunif external");

	    lsrcp = (unsigned long *) bip->u.uint8;
	    destp = (unsigned char *) ep->ext_buf;
	    for (i = 0; i < bip->count; ++i, ++destp, ++lsrcp) {
		(void) bu_plong( destp, *lsrcp );
	    }
	    break;
	case DB5_MINORTYPE_BINU_64BITINT:
	case DB5_MINORTYPE_BINU_64BITINT_U:
	    bu_log("rt_binunif_export5() Can't handle 64-bit integers yet\n");
	    return -1;
    }

    return 0;
}
Пример #8
0
/*
 *		C M D _ E X P O R T _ B O D Y ( )
 *
 *	Write an object's body to disk file
 *
 */
int
cmd_export_body(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    register struct directory	*dp;
    int				fd;
    void			*bufp;
    size_t			nbytes = 0;
    long int			written;
    struct bu_external		ext;
    struct db5_raw_internal	raw;
    struct rt_db_internal	intern;
    struct rt_binunif_internal	*bip;
    struct bu_vls		vls;
#if 0
    int				status;
#endif
    char			*tmp;

    CHECK_DBI_NULL;

    if (argc != 3) {
	bu_vls_init(&vls);
	bu_vls_printf(&vls, "help %s", argv[0]);
	Tcl_Eval(interp, bu_vls_addr(&vls));
	bu_vls_free(&vls);
	return TCL_ERROR;
    }

    /*
     *	Find the guy we're told to write
     */
    if ( (dp = db_lookup( dbip, argv[2], LOOKUP_NOISY)) == DIR_NULL ) {
	bu_vls_init( &vls );
	bu_vls_printf( &vls,
		       "Cannot find object %s for writing\n", argv[2] );
	Tcl_SetResult(interp, bu_vls_addr( &vls ), TCL_VOLATILE );
	bu_vls_free( &vls );
	mged_print_result( TCL_ERROR );
	return TCL_ERROR;
    }
    RT_INIT_DB_INTERNAL(&intern);
    if ( rt_db_get_internal5( &intern, dp, dbip, NULL, &rt_uniresource)
	 != ID_BINUNIF
	 || db_get_external( &ext, dp, dbip ) < 0 ) {
	(void)signal( SIGINT, SIG_IGN );
	TCL_READ_ERR_return;
    }
    if (db5_get_raw_internal_ptr(&raw, ext.ext_buf) == NULL)
    {
	bu_free_external( &ext );
	(void)signal( SIGINT, SIG_IGN );
	TCL_READ_ERR_return;
    }

    /*
     *	Do the writing
     */
#ifndef _WIN32
    if ( (fd = creat( argv[1], S_IRWXU | S_IRGRP | S_IROTH  )) == -1 ) {
#else
	if ( (fd = creat( argv[1], _S_IREAD | _S_IWRITE  )) == -1 ) {
#endif
	    bu_free_external( &ext );
	    bu_vls_init( &vls );
	    bu_vls_printf( &vls,
			   "Cannot open file %s for writing\n", argv[1] );
	    Tcl_SetResult(interp, bu_vls_addr( &vls ), TCL_VOLATILE );
	    bu_vls_free( &vls );
	    mged_print_result( TCL_ERROR );
	    return TCL_ERROR;
	}
	if (db5_type_descrip_from_codes(&tmp, raw.major_type, raw.minor_type))
	    tmp = 0;

	if (RT_G_DEBUG & DEBUG_VOL)
	    bu_log("cmd_export_body() sees type (%d, %d)='%s'\n",
		   raw.major_type, raw.minor_type, tmp);
	switch (raw.major_type) {
	    case DB5_MAJORTYPE_BINARY_UNIF:
#if 0
		if ( rt_binunif_import5( &intern, &ext, 0, dbip,
					 &rt_uniresource, raw.minor_type  ) ) {
		    (void)signal( SIGINT, SIG_IGN );
		    TCL_READ_ERR_return;
		}
#endif
		bip = (struct rt_binunif_internal *) intern.idb_ptr;
		RT_CK_BINUNIF(bip);
		rt_binunif_dump(bip);
		bufp = (void *) bip->u.uint8;
		bu_log("cmd_export_body() thinks bip->count=%d\n", bip->count);
		switch (bip -> type) {
		    case DB5_MINORTYPE_BINU_FLOAT:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... float");
			nbytes = (size_t) (bip->count * sizeof(float));
			break;
		    case DB5_MINORTYPE_BINU_DOUBLE:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... double");
			nbytes = (size_t) (bip->count * sizeof(double));
			break;
		    case DB5_MINORTYPE_BINU_8BITINT:
		    case DB5_MINORTYPE_BINU_8BITINT_U:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... 8bitint");
			nbytes = (size_t) (bip->count);
			break;
		    case DB5_MINORTYPE_BINU_16BITINT:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... 16bitint");
			nbytes = (size_t) (bip->count * 2);
			bu_log("data[0] = %u\n", bip->u.uint16[0]);
			break;
		    case DB5_MINORTYPE_BINU_16BITINT_U:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... 16bituint");
			nbytes = (size_t) (bip->count * 2);
			bu_log("data[0] = %u\n", bip->u.uint16[0]);
			break;
		    case DB5_MINORTYPE_BINU_32BITINT:
		    case DB5_MINORTYPE_BINU_32BITINT_U:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... 32bitint");
			nbytes = (size_t) (bip->count * 4);
			break;
		    case DB5_MINORTYPE_BINU_64BITINT:
		    case DB5_MINORTYPE_BINU_64BITINT_U:
			if (RT_G_DEBUG & DEBUG_VOL)
			    bu_log("bip->type switch... 64bitint");
			nbytes = (size_t) (bip->count * 8);
			break;
		    default:
			/* XXX	This shouln't happen!!    */
			bu_log("bip->type switch... default");
			break;
		}
		break;
	    default:
		if (RT_G_DEBUG & DEBUG_VOL)
		    bu_log("I'm in the default\n");
		bufp = (void *) ext.ext_buf;
		nbytes = (size_t) ext.ext_nbytes;
		break;
	}
	if (RT_G_DEBUG & DEBUG_VOL)
	    bu_log("going to write %ld bytes\n", nbytes);

	if ( (written = write(fd, bufp, nbytes) ) != nbytes ) {
	    perror(argv[1]);
	    bu_log("%s:%d\n", __FILE__, __LINE__);
	    bu_free_external( &ext );
	    bu_vls_init( &vls );
	    bu_vls_printf( &vls,
			   "Incomplete write of object %s to file %s, got %ld, s/b=%ld\n",
			   argv[2], argv[1], written, nbytes );
	    Tcl_SetResult(interp, bu_vls_addr( &vls ), TCL_VOLATILE );
	    bu_vls_free( &vls );
	    mged_print_result( TCL_ERROR );
	    return TCL_ERROR;
	}

	bu_free_external( &ext );
	return TCL_OK;
    }