static int write_bmhd( FILE *fp, BMHD *bmhd ) { BMHD b = *bmhd; unsigned long ck[ 2 ] = { ID_BMHD, 20 }; revbytes( &ck[ 1 ], 4, 1 ); revbytes( &b.w, 2, 4 ); revbytes( &b.pw, 2, 2 ); if ( 1 != fwrite( ck, 8, 1, fp )) return 0; if ( 1 != fwrite( &b, 20, 1, fp )) return 0; return 1; }
int lwGetPoints( picoMemStream_t *fp, int cksize, lwPointList *point ) { float *f; int np, i, j; if ( cksize == 1 ) return 1; /* extend the point array to hold the new points */ np = cksize / 12; point->offset = point->count; point->count += np; if ( !_pico_realloc( (void *) &point->pt, (point->count - np) * sizeof( lwPoint ), point->count * sizeof( lwPoint )) ) return 0; memset( &point->pt[ point->offset ], 0, np * sizeof( lwPoint )); /* read the whole chunk */ f = ( float * ) getbytes( fp, cksize ); if ( !f ) return 0; revbytes( f, 4, np * 3 ); /* assign position values */ for ( i = 0, j = 0; i < np; i++, j += 3 ) { point->pt[ i ].pos[ 0 ] = f[ j ]; point->pt[ i ].pos[ 1 ] = f[ j + 1 ]; point->pt[ i ].pos[ 2 ] = f[ j + 2 ]; } _pico_free( f ); return 1; }
float sgetF4( unsigned char **bp ) { float f; if ( flen == FLEN_ERROR ) return 0.0f; memcpy( &f, *bp, 4 ); revbytes( &f, 4, 1 ); flen += 4; *bp += 4; return f; }
unsigned int sgetU4( unsigned char **bp ) { unsigned int i; if ( flen == FLEN_ERROR ) return 0; memcpy( &i, *bp, 4 ); revbytes( &i, 4, 1 ); flen += 4; *bp += 4; return i; }
short sgetI2( unsigned char **bp ) { short i; if ( flen == FLEN_ERROR ) return 0; memcpy( &i, *bp, 2 ); revbytes( &i, 2, 1 ); flen += 2; *bp += 2; return i; }
static int write_cmap( FILE *fp, RGBTriple *cmap, int ncolors ) { unsigned long ck[ 2 ] = { ID_CMAP, ncolors * 3 }; revbytes( &ck[ 1 ], 4, 1 ); if ( 1 != fwrite( ck, 8, 1, fp )) return 0; if ( 1 != fwrite( cmap, ncolors * 3, 1, fp )) return 0; return 1; }
float getF4( picoMemStream_t *fp ) { float f; if ( flen == FLEN_ERROR ) return 0.0f; if ( 1 != _pico_memstream_read( fp, &f, 4 )) { flen = FLEN_ERROR; return 0.0f; } revbytes( &f, 4, 1 ); flen += 4; return f; }
unsigned int getU4( picoMemStream_t *fp ) { unsigned int i; if ( flen == FLEN_ERROR ) return 0; if ( 1 != _pico_memstream_read( fp, &i, 4 )) { flen = FLEN_ERROR; return 0; } revbytes( &i, 4, 1 ); flen += 4; return i; }
unsigned short getU2( picoMemStream_t *fp ) { unsigned short i; if ( flen == FLEN_ERROR ) return 0; if ( 1 != _pico_memstream_read( fp, &i, 2 )) { flen = FLEN_ERROR; return 0; } revbytes( &i, 2, 1 ); flen += 2; return i; }
float getF4( FILE *fp ) { float f; if ( flen == FLEN_ERROR ) return 0.0f; if ( 1 != fread( &f, 4, 1, fp )) { flen = FLEN_ERROR; return 0.0f; } revbytes( &f, 4, 1 ); flen += 4; return f; }
unsigned int getU4( FILE *fp ) { unsigned int i; if ( flen == FLEN_ERROR ) return 0; if ( 1 != fread( &i, 4, 1, fp )) { flen = FLEN_ERROR; return 0; } revbytes( &i, 4, 1 ); flen += 4; return i; }
unsigned short getU2( FILE *fp ) { unsigned short i; if ( flen == FLEN_ERROR ) return 0; if ( 1 != fread( &i, 2, 1, fp )) { flen = FLEN_ERROR; return 0; } revbytes( &i, 2, 1 ); flen += 2; return i; }
int CloseILBM( PicInfo *pic ) { unsigned long size, pos; /* size of the BODY chunk */ size = ftell( pic->fp ) - pic->BODYpos; /* if odd, add a pad byte (not part of the BODY size) */ if ( size & 1 ) fputc( 0, pic->fp ); /* FORM size */ pos = ftell( pic->fp ) - 8; /* seek to the start of the BODY chunk and write the size */ fseek( pic->fp, pic->BODYpos - 4, SEEK_SET ); revbytes( &size, 4, 1 ); fwrite( &size, 4, 1, pic->fp ); /* seek to the start of the file and write the FORM size */ fseek( pic->fp, 4, SEEK_SET ); revbytes( &pos, 4, 1 ); fwrite( &pos, 4, 1, pic->fp ); /* close the file */ fclose( pic->fp ); pic->fp = NULL; return TRUE; }
PicInfo *ReadILBM( const char *filename, int *result ) { long ck[ 3 ]; int i; PicInfo *pic; /* allocate a PicInfo */ *result = EPNOMEM; pic = ( PicInfo * ) calloc( sizeof( PicInfo ), 1 ); if ( !pic ) return NULL; /* cache the filename */ pic->filename = malloc( strlen( filename ) + 1 ); if ( !pic->filename ) return FreeILBM( pic ); strcpy( pic->filename, filename ); /* open the image file */ *result = EPNOFILE; pic->fp = fopen( filename, "rb" ); if ( !pic->fp ) return FreeILBM( pic ); /* read the FORM header */ *result = EPNOILBM; fread( ck, 12, 1, pic->fp ); /* FORM ILBM? */ if ( ck[ 0 ] != ID_FORM || ck[ 2 ] != ID_ILBM ) return FreeILBM( pic ); /* remember the FORM size */ ck[ 2 ] = ck[ 1 ]; revbytes( &ck[ 2 ], 4, 1 ); /* get BMHD, CAMG, CMAP and stop on BODY */ fread( ck, 8, 1, pic->fp ); revbytes( &ck[ 1 ], 4, 1 ); while ( 1 ) { ck[ 1 ] += ( ck[ 1 ] & 1 ); if ( ck[ 0 ] == ID_BODY ) break; if ( ck[ 0 ] == ID_BMHD ) { fread( &pic->bmhd, ck[ 1 ], 1, pic->fp ); revbytes( &pic->bmhd.w, 2, 4 ); revbytes( &pic->bmhd.transparentColor, 2, 1 ); revbytes( &pic->bmhd.pw, 2, 2 ); } else if ( ck[ 0 ] == ID_CMAP ) { *result = EPNOMEM; pic->cmap = malloc( ck[ 1 ] ); if ( !pic->cmap ) return FreeILBM( pic ); pic->ncolors = ck[ 1 ] / 3; fread( pic->cmap, ck[ 1 ], 1, pic->fp ); } else if ( ck[ 0 ] == ID_CAMG ) { fread( &pic->camg, ck[ 1 ], 1, pic->fp ); revbytes( &pic->camg, ck[ 1 ], 1 ); } else fseek( pic->fp, ck[ 1 ], SEEK_CUR ); if ( ck[ 2 ] < ftell( pic->fp )) /* past the end of the FORM? */ break; fread( ck, 8, 1, pic->fp ); revbytes( &ck[ 1 ], 4, 1 ); } /* found a BODY? */ *result = EPNOBODY; if ( ck[ 0 ] != ID_BODY ) return FreeILBM( pic ); /* found a BMHD? */ *result = EPNOBMHD; if ( pic->bmhd.w == 0 ) return FreeILBM( pic ); /* number of planes makes sense? */ *result = EPPLANES; if ( pic->bmhd.nPlanes > 8 && pic->bmhd.nPlanes != 24 ) return FreeILBM( pic ); /* if no CMAP, assume gray and create our own color table */ if ( pic->bmhd.nPlanes <= 8 && !pic->cmap ) { pic->isgray = TRUE; pic->ncolors = 1 << pic->bmhd.nPlanes; *result = EPNOMEM; pic->cmap = calloc( pic->ncolors, sizeof( RGBTriple )); if ( !pic->cmap ) return FreeILBM( pic ); for ( i = 0; i < pic->ncolors; i++ ) pic->cmap[ i ].red = pic->cmap[ i ].green = pic->cmap[ i ].blue = i * 255 / pic->ncolors; } /* if EHB, extend the color table */ if ( pic->camg & CAMG_EHB ) { pic->cmap = realloc( pic->cmap, pic->ncolors * 6 ); if ( !pic->cmap ) return FreeILBM( pic ); for ( i = pic->ncolors; i < pic->ncolors * 2; i++ ) { pic->cmap[ i ].red = pic->cmap[ i - pic->ncolors ].red >> 1; pic->cmap[ i ].green = pic->cmap[ i - pic->ncolors ].green >> 1; pic->cmap[ i ].blue = pic->cmap[ i - pic->ncolors ].blue >> 1; } pic->ncolors *= 2; }
Layer *flexReadLayer( Flex *flex, int fnum, int layerType, char *dst ) { Layer *layer; char *buf, *src, *dst0; int i, rowbytes, ok; layer = flexFindLayer( flex, fnum, layerType ); if ( !layer ) return NULL; if ( !flex->fp ) return NULL; fseek( flex->fp, layer->offset, SEEK_SET ); if ( layer->hdr.compression == NoCompression ) { ok = fread( dst, layer->size, 1, flex->fp ); if ( ok && ( layer->hdr.layerDepth > 1 )) revbytes( dst, layer->hdr.layerDepth, layer->w * layer->h ); return layer; } buf = malloc( layer->size ); if ( !buf ) return NULL; if ( 1 != fread( buf, layer->size, 1, flex->fp )) { free( buf ); return NULL; } src = buf; dst0 = dst; rowbytes = layer->hdr.layerDepth * layer->w; switch ( layer->hdr.compression ) { case HorizontalRLE: for ( i = 0; i < layer->h; i++ ) { ok = unpackRLE( &src, dst, rowbytes, 1 ); if ( !ok ) break; dst += rowbytes; } break; case VerticalRLE: for ( i = 0; i < rowbytes; i++ ) { ok = unpackRLE( &src, dst, layer->h, rowbytes ); if ( !ok ) break; dst++; } break; case HorizontalDelta: revbytes( dst, layer->hdr.layerDepth, layer->w * layer->h ); ok = unpackDelta( src, dst, rowbytes * layer->h, 1, rowbytes ); break; case VerticalDelta: revbytes( dst, layer->hdr.layerDepth, layer->w * layer->h ); ok = unpackDelta( src, dst, rowbytes * layer->h, rowbytes, 1 ); break; default: ok = 0; break; } free( buf ); if ( ok ) { revbytes( dst0, layer->hdr.layerDepth, layer->w * layer->h ); return layer; } return NULL; }
static Envelope *read_envb( LWChannelID *chan ) { LWChanGroupID group; LWEnvelopeID lwenv; LWSaveState *ss; LWError err; FILE *fp; Envelope *env; Key *key, *tail = NULL; float f[ 4 ]; unsigned int id; unsigned short esz, sz, w; int i, nparams; group = chinfo->channelParent( chan ); lwenv = chinfo->channelEnvelope( chan ); /* save the envelope to a file */ ss = filef->openSave( "test.env", LWIO_BINARY ); if ( !ss ) return NULL; err = envf->save( lwenv, ss ); filef->closeSave( ss ); if ( err ) { msgf->info( "Error while saving envelope:", err ); return NULL; } /* open the file */ fp = fopen( "test.env", "rb" ); if ( !fp ) { msgf->info( "Couldn't open test.env", NULL ); return NULL; } fread( &id, 4, 1, fp ); revbytes( &id, 4, 1 ); fread( &esz, 2, 1, fp ); revbytes( &esz, 2, 1 ); if ( id != ID_ENVL ) { fclose( fp ); return NULL; } /* allocate the Envelope structure */ env = calloc( 1, sizeof( Envelope )); if ( !env ) goto Fail; /* 2-byte index */ fseek( fp, 2, SEEK_CUR ); /* first subchunk header */ fread( &id, 4, 1, fp ); revbytes( &id, 4, 1 ); fread( &sz, 2, 1, fp ); revbytes( &sz, 2, 1 ); /* process subchunks as they're encountered */ while ( 1 ) { sz += sz & 1; switch ( id ) { case ID_PRE: if ( sz < 2 ) goto Fail; fread( &w, 2, 1, fp ); revbytes( &w, 2, 1 ); env->behavior[ 0 ] = w; if ( sz > 2 ) fseek( fp, sz - 2, SEEK_CUR ); break; case ID_POST: if ( sz < 2 ) goto Fail; fread( &w, 2, 1, fp ); revbytes( &w, 2, 1 ); env->behavior[ 1 ] = w; if ( sz > 2 ) fseek( fp, sz - 2, SEEK_CUR ); break; case ID_KEY: if ( sz < 8 ) goto Fail; key = calloc( 1, sizeof( Key )); if ( !key ) goto Fail; key->prev = tail; if ( tail ) tail->next = key; else env->key = key; tail = key; env->nkeys++; fread( f, 8, 1, fp ); revbytes( f, 4, 2 ); key->time = f[ 0 ]; key->value = f[ 1 ]; if ( sz > 8 ) fseek( fp, sz - 8, SEEK_CUR ); break; case ID_SPAN: if ( !key ) goto Fail; if ( sz < 4 ) goto Fail; fread( &id, 4, 1, fp ); revbytes( &id, 4, 1 ); nparams = ( sz - 4 ) / 4; if ( nparams > 4 ) nparams = 4; if ( nparams ) { fread( f, 4, nparams, fp ); revbytes( f, 4, nparams ); } i = 4 * ( nparams + 1 ); if ( sz > i ) fseek( fp, sz - i, SEEK_CUR ); switch ( id ) { case ID_TCB: if ( nparams < 3 ) goto Fail; key->shape = SHAPE_TCB; key->tension = f[ 0 ]; key->continuity = f[ 1 ]; key->bias = f[ 2 ]; break; case ID_BEZ2: key->shape = SHAPE_BEZ2; for ( i = 0; i < nparams; i++ ) key->param[ i ] = f[ i ]; break; case ID_BEZI: key->shape = SHAPE_BEZI; for ( i = 0; i < nparams; i++ ) key->param[ i ] = f[ i ]; break; case ID_HERM: key->shape = SHAPE_HERM; for ( i = 0; i < nparams; i++ ) key->param[ i ] = f[ i ]; break; case ID_LINE: key->shape = SHAPE_LINE; break; case ID_STEP: key->shape = SHAPE_STEP; break; default: break; } break; default: fseek( fp, sz, SEEK_CUR ); } /* end of the ENVL chunk? */ if ( esz <= ftell( fp ) - 8 ) break; /* get the next chunk header */ fread( &id, 4, 1, fp ); revbytes( &id, 4, 1 ); fread( &sz, 2, 1, fp ); revbytes( &sz, 2, 1 ); } fclose( fp ); return env; Fail: if ( fp ) fclose( fp ); if ( env ) free_env( env ); return NULL; }