int lwGetPolygonTags( picoMemStream_t *fp, int cksize, lwTagList *tlist, lwPolygonList *plist ){ unsigned int type; int rlen = 0, i, j; set_flen( 0 ); type = getU4( fp ); rlen = get_flen(); if ( rlen < 0 ) { return 0; } if ( type != ID_SURF && type != ID_PART && type != ID_SMGP ) { _pico_memstream_seek( fp, cksize - 4, PICO_SEEK_CUR ); return 1; } while ( rlen < cksize ) { i = getVX( fp ) + plist->offset; j = getVX( fp ) + tlist->offset; rlen = get_flen(); if ( rlen < 0 || rlen > cksize ) { return 0; } switch ( type ) { case ID_SURF: plist->pol[ i ].surf = ( lwSurface * ) ( (size_t)j ); break; case ID_PART: plist->pol[ i ].part = j; break; case ID_SMGP: plist->pol[ i ].smoothgrp = j; break; } } return 1; }
lwSurface *lwGetSurface( picoMemStream_t *fp, int cksize ){ lwSurface *surf; lwTexture *tex; lwPlugin *shdr; unsigned int id, type; unsigned short sz; int pos, rlen; /* allocate the Surface structure */ surf = _pico_calloc( 1, sizeof( lwSurface ) ); if ( !surf ) { goto Fail; } /* non-zero defaults */ surf->color.rgb[ 0 ] = 0.78431f; surf->color.rgb[ 1 ] = 0.78431f; surf->color.rgb[ 2 ] = 0.78431f; surf->diffuse.val = 1.0f; surf->glossiness.val = 0.4f; surf->bump.val = 1.0f; surf->eta.val = 1.0f; surf->sideflags = 1; /* remember where we started */ set_flen( 0 ); pos = _pico_memstream_tell( fp ); /* names */ surf->name = getS0( fp ); surf->srcname = getS0( fp ); /* first subchunk header */ id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) { goto Fail; } /* process subchunks as they're encountered */ while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_COLR: surf->color.rgb[ 0 ] = getF4( fp ); surf->color.rgb[ 1 ] = getF4( fp ); surf->color.rgb[ 2 ] = getF4( fp ); surf->color.eindex = getVX( fp ); break; case ID_LUMI: surf->luminosity.val = getF4( fp ); surf->luminosity.eindex = getVX( fp ); break; case ID_DIFF: surf->diffuse.val = getF4( fp ); surf->diffuse.eindex = getVX( fp ); break; case ID_SPEC: surf->specularity.val = getF4( fp ); surf->specularity.eindex = getVX( fp ); break; case ID_GLOS: surf->glossiness.val = getF4( fp ); surf->glossiness.eindex = getVX( fp ); break; case ID_REFL: surf->reflection.val.val = getF4( fp ); surf->reflection.val.eindex = getVX( fp ); break; case ID_RFOP: surf->reflection.options = getU2( fp ); break; case ID_RIMG: surf->reflection.cindex = getVX( fp ); break; case ID_RSAN: surf->reflection.seam_angle = getF4( fp ); break; case ID_TRAN: surf->transparency.val.val = getF4( fp ); surf->transparency.val.eindex = getVX( fp ); break; case ID_TROP: surf->transparency.options = getU2( fp ); break; case ID_TIMG: surf->transparency.cindex = getVX( fp ); break; case ID_RIND: surf->eta.val = getF4( fp ); surf->eta.eindex = getVX( fp ); break; case ID_TRNL: surf->translucency.val = getF4( fp ); surf->translucency.eindex = getVX( fp ); break; case ID_BUMP: surf->bump.val = getF4( fp ); surf->bump.eindex = getVX( fp ); break; case ID_SMAN: surf->smooth = getF4( fp ); break; case ID_SIDE: surf->sideflags = getU2( fp ); break; case ID_CLRH: surf->color_hilite.val = getF4( fp ); surf->color_hilite.eindex = getVX( fp ); break; case ID_CLRF: surf->color_filter.val = getF4( fp ); surf->color_filter.eindex = getVX( fp ); break; case ID_ADTR: surf->add_trans.val = getF4( fp ); surf->add_trans.eindex = getVX( fp ); break; case ID_SHRP: surf->dif_sharp.val = getF4( fp ); surf->dif_sharp.eindex = getVX( fp ); break; case ID_GVAL: surf->glow.val = getF4( fp ); surf->glow.eindex = getVX( fp ); break; case ID_LINE: surf->line.enabled = 1; if ( sz >= 2 ) { surf->line.flags = getU2( fp ); } if ( sz >= 6 ) { surf->line.size.val = getF4( fp ); } if ( sz >= 8 ) { surf->line.size.eindex = getVX( fp ); } break; case ID_ALPH: surf->alpha_mode = getU2( fp ); surf->alpha = getF4( fp ); break; case ID_AVAL: surf->alpha = getF4( fp ); break; case ID_BLOK: type = getU4( fp ); switch ( type ) { case ID_IMAP: case ID_PROC: case ID_GRAD: tex = lwGetTexture( fp, sz - 4, type ); if ( !tex ) { goto Fail; } if ( !add_texture( surf, tex ) ) { lwFreeTexture( tex ); } set_flen( 4 + get_flen() ); break; case ID_SHDR: shdr = lwGetShader( fp, sz - 4 ); if ( !shdr ) { goto Fail; } lwListInsert( (void **) &surf->shader, shdr, (void *) compare_shaders ); ++surf->nshaders; set_flen( 4 + get_flen() ); break; } break; default: break; } /* error while reading current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) { goto Fail; } /* skip unread parts of the current subchunk */ if ( rlen < sz ) { _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR ); } /* end of the SURF chunk? */ if ( cksize <= _pico_memstream_tell( fp ) - pos ) { break; } /* get the next subchunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) { goto Fail; } } return surf; Fail: if ( surf ) { lwFreeSurface( surf ); } return NULL; }
int lwGetImageMap( picoMemStream_t *fp, int rsz, lwTexture *tex ){ unsigned int id; unsigned short sz; int rlen, pos; pos = _pico_memstream_tell( fp ); id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) { return 0; } while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_TMAP: if ( !lwGetTMap( fp, sz, &tex->tmap ) ) { return 0; } break; case ID_PROJ: tex->param.imap.projection = getU2( fp ); break; case ID_VMAP: tex->param.imap.vmap_name = getS0( fp ); break; case ID_AXIS: tex->param.imap.axis = getU2( fp ); break; case ID_IMAG: tex->param.imap.cindex = getVX( fp ); break; case ID_WRAP: tex->param.imap.wrapw_type = getU2( fp ); tex->param.imap.wraph_type = getU2( fp ); break; case ID_WRPW: tex->param.imap.wrapw.val = getF4( fp ); tex->param.imap.wrapw.eindex = getVX( fp ); break; case ID_WRPH: tex->param.imap.wraph.val = getF4( fp ); tex->param.imap.wraph.eindex = getVX( fp ); break; case ID_AAST: tex->param.imap.aas_flags = getU2( fp ); tex->param.imap.aa_strength = getF4( fp ); break; case ID_PIXB: tex->param.imap.pblend = getU2( fp ); break; case ID_STCK: tex->param.imap.stck.val = getF4( fp ); tex->param.imap.stck.eindex = getVX( fp ); break; case ID_TAMP: tex->param.imap.amplitude.val = getF4( fp ); tex->param.imap.amplitude.eindex = getVX( fp ); break; default: break; } /* error while reading the current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) { return 0; } /* skip unread parts of the current subchunk */ if ( rlen < sz ) { _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR ); } /* end of the image map? */ if ( rsz <= _pico_memstream_tell( fp ) - pos ) { break; } /* get the next subchunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) { return 0; } } set_flen( _pico_memstream_tell( fp ) - pos ); return 1; }
int lwGetTMap( picoMemStream_t *fp, int tmapsz, lwTMap *tmap ){ unsigned int id; unsigned short sz; int rlen, pos, i; pos = _pico_memstream_tell( fp ); id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) { return 0; } while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_SIZE: for ( i = 0; i < 3; i++ ) tmap->size.val[ i ] = getF4( fp ); tmap->size.eindex = getVX( fp ); break; case ID_CNTR: for ( i = 0; i < 3; i++ ) tmap->center.val[ i ] = getF4( fp ); tmap->center.eindex = getVX( fp ); break; case ID_ROTA: for ( i = 0; i < 3; i++ ) tmap->rotate.val[ i ] = getF4( fp ); tmap->rotate.eindex = getVX( fp ); break; case ID_FALL: tmap->fall_type = getU2( fp ); for ( i = 0; i < 3; i++ ) tmap->falloff.val[ i ] = getF4( fp ); tmap->falloff.eindex = getVX( fp ); break; case ID_OREF: tmap->ref_object = getS0( fp ); break; case ID_CSYS: tmap->coord_sys = getU2( fp ); break; default: break; } /* error while reading the current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) { return 0; } /* skip unread parts of the current subchunk */ if ( rlen < sz ) { _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR ); } /* end of the TMAP subchunk? */ if ( tmapsz <= _pico_memstream_tell( fp ) - pos ) { break; } /* get the next subchunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) { return 0; } } set_flen( _pico_memstream_tell( fp ) - pos ); return 1; }
int lwGetTHeader( picoMemStream_t *fp, int hsz, lwTexture *tex ){ unsigned int id; unsigned short sz; int pos, rlen; /* remember where we started */ set_flen( 0 ); pos = _pico_memstream_tell( fp ); /* ordinal string */ tex->ord = getS0( fp ); /* first subchunk header */ id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) { return 0; } /* process subchunks as they're encountered */ while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_CHAN: tex->chan = getU4( fp ); break; case ID_OPAC: tex->opac_type = getU2( fp ); tex->opacity.val = getF4( fp ); tex->opacity.eindex = getVX( fp ); break; case ID_ENAB: tex->enabled = getU2( fp ); break; case ID_NEGA: tex->negative = getU2( fp ); break; case ID_AXIS: tex->axis = getU2( fp ); break; default: break; } /* error while reading current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) { return 0; } /* skip unread parts of the current subchunk */ if ( rlen < sz ) { _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR ); } /* end of the texture header subchunk? */ if ( hsz <= _pico_memstream_tell( fp ) - pos ) { break; } /* get the next subchunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) { return 0; } } set_flen( _pico_memstream_tell( fp ) - pos ); return 1; }
lwEnvelope *lwGetEnvelope( FILE *fp, int cksize ) { lwEnvelope *env; lwKey *key; lwPlugin *plug; unsigned int id; unsigned short sz; float f[ 4 ]; int i, nparams, pos, rlen; /* allocate the Envelope structure */ env = calloc( 1, sizeof( lwEnvelope )); if ( !env ) goto Fail; /* remember where we started */ set_flen( 0 ); pos = ftell( fp ); /* index */ env->index = getVX( fp ); /* first subchunk header */ id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) goto Fail; /* process subchunks as they're encountered */ while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_TYPE: env->type = getU2( fp ); break; case ID_NAME: env->name = getS0( fp ); break; case ID_PRE: env->behavior[ 0 ] = getU2( fp ); break; case ID_POST: env->behavior[ 1 ] = getU2( fp ); break; case ID_KEY: key = calloc( 1, sizeof( lwKey )); if ( !key ) goto Fail; key->time = getF4( fp ); key->value = getF4( fp ); lwListInsert( &env->key, key, compare_keys ); env->nkeys++; break; case ID_SPAN: if ( !key ) goto Fail; key->shape = getU4( fp ); nparams = ( sz - 4 ) / 4; if ( nparams > 4 ) nparams = 4; for ( i = 0; i < nparams; i++ ) f[ i ] = getF4( fp ); switch ( key->shape ) { case ID_TCB: key->tension = f[ 0 ]; key->continuity = f[ 1 ]; key->bias = f[ 2 ]; break; case ID_BEZI: case ID_HERM: case ID_BEZ2: for ( i = 0; i < nparams; i++ ) key->param[ i ] = f[ i ]; break; } break; case ID_CHAN: plug = calloc( 1, sizeof( lwPlugin )); if ( !plug ) goto Fail; plug->name = getS0( fp ); plug->flags = getU2( fp ); plug->data = getbytes( fp, sz - get_flen() ); lwListAdd( &env->cfilter, plug ); env->ncfilters++; break; default: break; } /* error while reading current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) goto Fail; /* skip unread parts of the current subchunk */ if ( rlen < sz ) fseek( fp, sz - rlen, SEEK_CUR ); /* end of the ENVL chunk? */ rlen = ftell( fp ) - pos; if ( cksize < rlen ) goto Fail; if ( cksize == rlen ) break; /* get the next subchunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) goto Fail; } return env; Fail: lwFreeEnvelope( env ); return NULL; }
lwClip *lwGetClip( FILE *fp, int cksize ) { lwClip *clip; lwPlugin *filt; unsigned int id; unsigned short sz; int pos, rlen; /* allocate the Clip structure */ clip = calloc( 1, sizeof( lwClip )); if ( !clip ) goto Fail; clip->contrast.val = 1.0f; clip->brightness.val = 1.0f; clip->saturation.val = 1.0f; clip->gamma.val = 1.0f; /* remember where we started */ set_flen( 0 ); pos = ftell( fp ); /* index */ clip->index = getI4( fp ); /* first subchunk header */ clip->type = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) goto Fail; sz += sz & 1; set_flen( 0 ); switch ( clip->type ) { case ID_STIL: clip->source.still.name = getS0( fp ); break; case ID_ISEQ: clip->source.seq.digits = getU1( fp ); clip->source.seq.flags = getU1( fp ); clip->source.seq.offset = getI2( fp ); getU2( fp ); /* Legacy Cruft: Nothing to see here */ clip->source.seq.start = getI2( fp ); clip->source.seq.end = getI2( fp ); clip->source.seq.prefix = getS0( fp ); clip->source.seq.suffix = getS0( fp ); break; case ID_ANIM: clip->source.anim.name = getS0( fp ); clip->source.anim.server = getS0( fp ); rlen = get_flen(); clip->source.anim.data = getbytes( fp, sz - rlen ); break; case ID_XREF: clip->source.xref.index = getI4( fp ); clip->source.xref.string = getS0( fp ); break; case ID_STCC: clip->source.cycle.lo = getI2( fp ); clip->source.cycle.hi = getI2( fp ); clip->source.cycle.name = getS0( fp ); break; default: break; } /* error while reading current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) goto Fail; /* skip unread parts of the current subchunk */ if ( rlen < sz ) fseek( fp, sz - rlen, SEEK_CUR ); /* end of the CLIP chunk? */ rlen = ftell( fp ) - pos; if ( cksize < rlen ) goto Fail; if ( cksize == rlen ) return clip; /* process subchunks as they're encountered */ id = getU4( fp ); sz = getU2( fp ); if ( 0 > get_flen() ) goto Fail; while ( 1 ) { sz += sz & 1; set_flen( 0 ); switch ( id ) { case ID_TIME: clip->start_time = getF4( fp ); clip->duration = getF4( fp ); clip->frame_rate = getF4( fp ); break; case ID_CONT: clip->contrast.val = getF4( fp ); clip->contrast.eindex = getVX( fp ); break; case ID_BRIT: clip->brightness.val = getF4( fp ); clip->brightness.eindex = getVX( fp ); break; case ID_SATR: clip->saturation.val = getF4( fp ); clip->saturation.eindex = getVX( fp ); break; case ID_HUE: clip->hue.val = getF4( fp ); clip->hue.eindex = getVX( fp ); break; case ID_GAMM: clip->gamma.val = getF4( fp ); clip->gamma.eindex = getVX( fp ); break; case ID_NEGA: clip->negative = getU2( fp ); break; case ID_IFLT: case ID_PFLT: filt = calloc( 1, sizeof( lwPlugin )); if ( !filt ) goto Fail; filt->name = getS0( fp ); filt->flags = getU2( fp ); rlen = get_flen(); filt->data = getbytes( fp, sz - rlen ); if ( id == ID_IFLT ) { lwListAdd( &clip->ifilter, filt ); clip->nifilters++; } else { lwListAdd( &clip->pfilter, filt ); clip->npfilters++; } break; default: break; } /* error while reading current subchunk? */ rlen = get_flen(); if ( rlen < 0 || rlen > sz ) goto Fail; /* skip unread parts of the current subchunk */ if ( rlen < sz ) fseek( fp, sz - rlen, SEEK_CUR ); /* end of the CLIP chunk? */ rlen = ftell( fp ) - pos; if ( cksize < rlen ) goto Fail; if ( cksize == rlen ) break; /* get the next chunk header */ set_flen( 0 ); id = getU4( fp ); sz = getU2( fp ); if ( 6 != get_flen() ) goto Fail; } return clip; Fail: lwFreeClip( clip ); return NULL; }