📄 qccbitbuffer.3
字号:
.TH QCCBITBUFFER 3 "QCCPACK" "".SH NAMEQccBitBuffer \- generic data structure for bitstream packing.SH SYNOPSIS.B #include "libQccPack.h".sp.BI "int QccBitBufferInitialize(QccBitBuffer *" bit_buffer );.br.BI "int QccBitBufferStart(QccBitBuffer *" bit_buffer );.br.BI "int QccBitBufferEnd(QccBitBuffer *" bit_buffer );.br.BI "int QccBitBufferFlush(QccBitBuffer *" bit_buffer );.br.BI "int QccBitBufferCopy(QccBitBuffer *" output_buffer ", QccBitBuffer *" input_buffer ", int " num_bits );.br.BI "int QccBitBufferPutBit(QccBitBuffer *" bit_buffer ", int " bit_value );.br.BI "int QccBitBufferGetBit(QccBitBuffer *" bit_buffer ", int *" bit_value );.br.BI "int QccBitBufferPutBits(QccBitBuffer *" bit_buffer ", int " val ", int " num_bits );.br.BI "int QccBitBufferGetBits(QccBitBuffer *" bit_buffer ", int *" val ", int " num_bits );.br.BI "int QccBitBufferPutChar(QccBitBuffer *" bit_buffer ", unsigned char " val );.br.BI "int QccBitBufferGetChar(QccBitBuffer *" bit_buffer ", unsigned char *" val );.br.BI "int QccBitBufferPutInt(QccBitBuffer *" bit_buffer ", int " val );.br.BI "int QccBitBufferGetInt(QccBitBuffer *" bit_buffer ", int *" val );.br.BI "int QccBitBufferPutDouble(QccBitBuffer *" bit_buffer ", double " val );.br.BI "int QccBitBufferGetDouble(QccBitBuffer *" bit_buffer ", double *" val );.SH DESCRIPTIONWhen one writes a stream of bits to a file, it is necessary for the bitsto be "packed" into byte-sized characters, as the byte is the smallestinput/output unit supported by the operating system.Likewise, bit "unpacking" from byte-sized charactersis necessary to read a bitstream from a file.QccPack provides the.B QccBitBufferdata structure to facilitate reading and writing bitstreams.For writing, the.B QccBitBufferstructure and related routines automatically handle the packing of consecutivebits into a character and the writing of the resultingcharacter, when full, to the file.For reading, bits are unpacked from the current character; when no more bitsremain to be read from the character, the next character from the fileis read automatically.Consequently, from the user's point of view, the.B QccBitBufferstructure and related routines provided bit-level accessto files.Additional routines allow .B intand.B doubletypes to be read/written from/to the bitstream without regard tothe byte alignment usually required by the operating system..SH "DATA STRUCTURE"The.B QccBitBufferdata structure is defined as:.RS.nftypedef unsigned char QccBitBufferChar;typedef struct{ QccString filename; FILE *fileptr; int type; int bit_cnt; int byte_cnt; int bits_to_go; QccBitBufferChar buffer;} QccBitBuffer;.fi.RE.LPThe fields of.B QccBitBufferare as follows:.TP.I filenameThe name of the file to which the bitstream is to beread/written..TP.I fileptrThe.B FILEpointer to the file..TP.I typeIndicates whether the buffer is output (bitstream is written to file) orinput (bitstream is read from file). Takes values.B QCCBITBUFFER_OUTPUTor.BR QCCBITBUFFER_INPUT ,respectively..TP.I bit_cntNumber of bits read from or written to buffer so far..TP.I byte_cntNumber of bytes read from or written to the file associated to thebuffer..TP.I bits_to_goNumber of bits remaining to pack/unpack into/from current character.For internal use in QccPack routines only..TP.I bufferThe current character being packed/unpacked..SH "FILE FORMAT"The file written or read by.BR QccBitBufferroutinesis merely a sequence of the packed characters representing the bitstream.The orderthat the bits of the bitstream are packed into the charactersis least-significant bit (LSB) to most-significant bit (MSB).There is no header information stored in the file..SH "ROUTINES".B QccBitBufferInitialize()should be called before any use of a.B QccBitBufferstructure..B QccBitBufferInitialize()initializes the fields of.I bit_bufferto the following values:.RS.IR filename :.B NULLstring.br.IR fileptr :.B NULL.br.IR type :.B QCCBITBUFFER_OUTPUT.br.IR bit_cnt :0.br.IR bits_to_go :8.br.IR buffer :0.RE.LP.B QccBitBufferStart()starts a read or write on.IR bit_buffer ..IR bit_buffer -> typemust be set to indicate whether .I bit_bufferwill read a file.RB ( QCCBITBUFFER_INPUT )or write a file.RB ( QCCBITBUFFER_OUTPUT ).If .IR bit_buffer -> fileptr is.BR NULL ,the file indicated by.IR bit_buffer -> filenameis opened and the resulting.B FILEpointer is returned in.IR bit_buffer -> fileptr .Otherwise, it is assumed that the file is already opened properly forreading or writing as appropriate, and is properly positioned to thestart of binary data in the file (thus,.BR QccBitBufferStart()permits the reading of, say, an ASCII header by some external procedurebefore the reading of binary data commences).In either case, if.I bit_bufferis being started for reading, .IR bit_buffer -> bufferis primed by reading the first character of the file..LP.B QccBitBufferEnd()should be called after all accesses (read or write) to.I bit_bufferare complete..B QccBitBufferEnd()closes .IR bit_buffer -> fileptr ..LP.B QccBitBufferFlush()should be called at the end of writing a bitstreamafter all bits have been writtenbut before .BR QccBitBufferEnd() is called..B QccBitBufferFlush()ensures that the last byte being packed with bits, which may not befull, is output to the file.If any bits are output after a call to.BR QccBitBufferFlush() ,they will be written to the next byte of the file; i.e.,.B QccBitBufferFlush()can be used to align output to byte boundaries in the file..B QccBitBufferFlush()is usually called only for buffers opened for output.In the case of an input buffer,.B QccBitBufferFlush()will cause the next bit read to come from the nextbyte of the file; i.e.,on input,.B QccBitBufferFlush()can be used to enforce alignment to byte boundariesduring reading the file.On output,.BR QccBitBufferFlush()calls.BR QccFileFlush (3)to ensure that all stream buffers are flushed..LP.BR QccBitBufferCopy()copies.I num_bitsfrom.I input_bufferto.I output_bufferby calling.B QccBitBufferGetBit()on.I input_bufferand.B QccBitBufferPutBit()on.I output_buffer.I num_bitstimes..LP.B QccBitBufferPutBit()outputs a bit,.IR bit_value ,to output buffer.IR bit_buffer .The bit is packed into the current byte, and, if this byte is full,it is written to the file..I bit_valuegives the value of the bit;.I bit_value should be zero to denote a 0 bit and nonzero to denote a 1 bit..LP.B QccBitBufferGetBit()inputs a bit,.IR bit_value ,from an input buffer.IR bit_buffer .If the current byte is empty, the next byte is read from the file.Then, a bit is unpacked from the byte.The bit is returned in.IR bit_value ;a 0 is returned for a 0 bit, a 1 is returned for a 1 bit.If the.IR bit_valuepointer is.BR NULL ,then the bit is read from.I input_bufferbut not returned..LP.B QccBitBufferPutBits()outputs the least-significant.I num_bitsbits of.I valto.I bit_buffervia.I num_bitsconsecutive calls to.BR QccBitBufferPutBit() .The bits are output starting with the least-significant bit of.IR val ..LP.BR QccBitBufferGetBits()inputs.I num_bitsbits from.I bit_bufferto the least-significant bits of.IR valvia.I num_bitsconsecutive calls to.BR QccBitBufferGetBit() .The bits are placed into.I valstarting with the least-significant bit first.If the.IR valpointer is.BR NULL ,then the bits are read from.I input_bufferbut not returned..LP.B QccBitBufferPutChar()and.B QccBitBufferGetChar()write or read, respectively, an.B unsigned charto or from the file. That is, these two routines effectuate 8 consecutivebit writes or reads via.B QccBitBufferPutBit()or.BR QccBitBufferGetBit() .If the.IR valpointer is.BR NULL ,then.BR QccBitBufferGetBit() reads the character from.IR input_buffer ,but it is not returned..LP.B QccBitBufferPutInt()and.B QccBitBufferGetInt()use.BR QccBinaryIntToChar (3)and.BR QccBinaryChartoInt (3)to convert between an.B intand four characters. These four characters are read from or written tothe file via four calls to.B QccBitBufferPutChar()or.BR QccBitBufferGetChar() .If the.IR valpointer is.BR NULL ,then.BR QccBitBufferGetInt() reads the integer from.IR input_buffer ,but it is not returned..LP.B QccBitBufferPutDouble()and.B QccBitBufferGetDouble()use.BR QccBinaryFloatToChar (3)and.BR QccBinaryChartoFloat (3)to convert between a.B doubleand four characters. These four characters are read from or written tothe file via four calls to.B QccBitBufferPutChar()or.BR QccBitBufferGetChar() ..B QccBitBufferPutDouble()and.B QccBitBufferGetDouble()actually read and write .B doubleas.BR float ;consequently, these routines may incur a loss of precision.If the.IR valpointer is.BR NULL ,then.BR QccBitBufferGetDouble() reads the value from.IR input_buffer ,but it is not returned..SH "RETURN VALUE"Each of these routines return 0 upon successful completion, 1 on error..SH "SEE ALSO".BR QccBinaryIntToChar (3),.BR QccBinaryChartoInt (3),.BR QccBinaryFloatToChar (3),.BR QccBinaryChartoFloat (3),.BR QccPack (3).SH NOTESBecause bitstreams are stored in raw format files with no headers,the routines that read these files (e.g.,.BR QccBitBufferGetBit() )do not know a priori how long the bitstream is.Since a bitstream may end in the middle of a byte,the end of the file may not correspond to the end of the bitstream.It is the user's responsibility to ensure that data is not read from beyondthe end of the bitstream..LPOn a related note,.IR bit_buffer -> bit_cntstores the number of bits actually put into or got out of the buffer.On the other hand,.IR bit_buffer -> byte_cntstores the number of bytes that have been read from or written to thefile associated to the buffer.Calls to.B QccBitBufferFlush()may cause these two to appear to be "out of sync."For example, when the last byte of a file is written, if this last byte is notcompletely full,.IR bit_buffer -> bit_cntis incremented by only the number of bits actually put into the buffer(something less than 8),while.IR bit_buffer -> byte_cntis incremented once indicating that a byte (8 bits) was written to thefile. In this case,.IR bit_buffer -> bit_cntwill not be equal to 8 times.IR bit_buffer -> byte_cntas might have been expected.This issue is important to keep in mind should.B QccBitBufferFlush()be called multiple times during reading or writing in order toalign to byte boundaries; in this case, the bits in the filethat are "skipped" to get tothe start of the next byte are not included in the.IR bit_buffer -> bit_cntcount..SH AUTHORCopyright (C) 1997-2009 James E. Fowler.\" The programs herein are free software; you can redistribute them an.or.\" modify them under the terms of the GNU General Public License.\" as published by the Free Software Foundation; either version 2.\" of the License, or (at your option) any later version..\" .\" These programs are distributed in the hope that they will be useful,.\" but WITHOUT ANY WARRANTY; without even the implied warranty of.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the.\" GNU General Public License for more details..\" .\" You should have received a copy of the GNU General Public License.\" along with these programs; if not, write to the Free Software.\" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -