void SWFShape_end(SWFShape shape) { int i; byte* buffer; if ( shape->isEnded ) return; shape->isEnded = TRUE; buffer = SWFOutput_getBuffer(shape->out); if (shape->isUsingNewStyles == FALSE) { buffer[0] = (SWFOutput_numBits(shape->nFills2) << 4) + SWFOutput_numBits(shape->nLines2); } for ( i=0; i<shape->nRecords; ++i ) { if ( i < shape->nRecords-1 || shape->records[i].type != SHAPERECORD_STATECHANGE ) { SWFShape_writeShapeRecord(shape, shape->records[i]); } free(shape->records[i].record.stateChange); /* all in union are pointers */ } SWFOutput_writeBits(shape->out, 0, 6); /* end tag */ SWFOutput_byteAlign(shape->out); /* addStyleHeader creates a new output and adds the existing one after itself- so even though it's called afterwards it's written before, as it should be */ if ( BLOCK(shape)->type > 0 ) /* i.e., shape with style */ SWFShape_addStyleHeader(shape); free(shape->records); shape->records = NULL; shape->nRecords = 0; shape->lines = &shape->lines2; shape->fills = &shape->fills2; shape->nLines = &shape->nLines2; shape->nFills = &shape->nFills2; }
/* * Oputput a movie with SWFByteOutputMethod * returns the number of bytes written. */ int SWFMovie_output(SWFMovie movie, SWFByteOutputMethod method, void *data) { SWFOutput swfbuffer; int swflength; byte *buffer; int n; int level = SWF_compression; swfbuffer = SWFMovie_toOutput(movie, level); swflength = SWFOutput_getLength(swfbuffer); buffer = SWFOutput_getBuffer(swfbuffer); for(n = 0 ; n < swflength ; n++) method(*buffer++, data); destroySWFOutput(swfbuffer); return swflength; }
SWFOutput SWFMovie_toOutput(SWFMovie movie, int level) { int swflength; #if USE_ZLIB int status; #endif SWFOutput header, tempbuffer=0, buffer, swfbuffer; SWFBlock lastBlock; unsigned long compresslength; if ( movie->nExports > 0 ) SWFMovie_writeExports(movie); if ( movie->metadata != NULL) { SWFMovie_addBlock(movie, (SWFBlock)movie->metadata); movie->metadata = NULL; // do not destroy with movie if added as block } /* Add a terminating SHOWFRAME tag if not already there */ lastBlock = SWFBlockList_getLastBlock(movie->blockList); if ( ! lastBlock || SWFBlock_getType(lastBlock) != SWF_SHOWFRAME ) { SWFMovie_nextFrame(movie); } while ( movie->nFrames < movie->totalFrames ) SWFMovie_nextFrame(movie); if(movie->symbolClass) SWFMovie_addBlock(movie, (SWFBlock)movie->symbolClass); if(movie->sceneData) SWFMovie_addBlock(movie, (SWFBlock)movie->sceneData); SWFMovie_addBlock(movie, newSWFEndBlock()); // add five for the setbackground block.. swflength = SWFBlockList_completeBlocks(movie->blockList, movie->version); /* XXX - hack */ SWFDisplayList_rewindSoundStream(movie->displayList); header = newSizedSWFOutput(23); SWFOutput_writeRect(header, movie->bounds); SWFOutput_writeUInt16(header, (int)floor(movie->rate*256)); SWFOutput_writeUInt16(header, movie->nFrames); /* SWF >= 8: first block _must_ be SWF_FILEATTRIBUTES */ if(movie->fattrs) writeSWFBlockToMethod((SWFBlock)movie->fattrs, SWFOutputMethod, header); if(movie->backgroundBlock) writeSWFBlockToMethod(movie->backgroundBlock, SWFOutputMethod, header); if(movie->limits) writeSWFBlockToMethod((SWFBlock)movie->limits, SWFOutputMethod, header); SWFOutput_byteAlign(header); swflength += 8 + SWFOutput_getLength(header); // compression level check #if USE_ZLIB if (level < -1) level = -1; if (level > 9) level = 9; #else if ( level != -1 ) { SWF_warn("No zlib support compiled in, " "cannot generate compressed output"); level = -1; } #endif // reserve output buffer if(level >= 0) { // a little bit more than the uncompressed data compresslength = swflength + (swflength/1000) + 15 + 1; swfbuffer = newSizedSWFOutput( compresslength + 8 ); } else swfbuffer = newSizedSWFOutput( swflength ); if (level >= 0) SWFOutput_writeUInt8 (swfbuffer, 'C'); else SWFOutput_writeUInt8 (swfbuffer, 'F'); SWFOutput_writeUInt8 (swfbuffer, 'W'); SWFOutput_writeUInt8 (swfbuffer, 'S'); SWFOutput_writeUInt8 (swfbuffer, movie->version); // Movie length SWFOutput_writeUInt32(swfbuffer, swflength); if(level >= 0) buffer = tempbuffer = newSizedSWFOutput( swflength - 8); else buffer = swfbuffer; SWFOutput_writeToMethod(header, SWFOutputMethod, buffer); destroySWFOutput(header); // fill swfbuffer with blocklist SWFBlockList_writeBlocksToMethod(movie->blockList, SWFOutputMethod, buffer); #if USE_ZLIB if (level >= 0) { status = compress2 ( (Bytef*) SWFOutput_getBuffer(swfbuffer)+8, &compresslength, SWFOutput_getBuffer(tempbuffer), SWFOutput_getLength(tempbuffer), level); if (status == Z_OK) { SWFOutput_truncate(swfbuffer, compresslength+8); destroySWFOutput(tempbuffer); } else SWF_error("compression failed"); } #endif // ndef USE_ZLIB return swfbuffer; }