📄 libmng.txt
字号:
libmng - Multiple-image Network Graphics (MNG) Reference Library 1.0.4DESCRIPTIONThe libmng library supports decoding, displaying, encoding, and variousother manipulations of the Multiple-image Network Graphics (MNG) formatimage files. It uses the zlib compression library, and optionally theJPEG library by the Independant JPEG Group (IJG) and/orlcms (little cms), a color-management library by Marti Maria Saguer.I. IntroductionThis file describes how to use and modify the MNG reference library(known as libmng) for your own use. There are seven sections to thisfile: introduction, callbacks, housekeeping, reading, displaying, writing, and modification and configuration notes for various specialplatforms. We assume that libmng is already installed; see theINSTALL.README file for instructions on how to install libmng.Libmng was written to support and promote the MNG specification.The latest MNG specification (currently 1.0) is available at http://www.libpng.org/pub/mngOther information about MNG can be found at the MNG home page at http://www.libpng.org/pub/mngThe latest version of libmng can be found at its own homepage at http://www.libmng.comIn most cases the library will not need to be changed.For standardization purposes the library contains both a Windows DLLand a makefile for building a shared library (SO). The library iswritten in C, but an interface for Borland Delphi is also available.Libmng has been designed to handle multiple sessions at one time,to be easily modifiable, to be portable to the vast majority ofmachines (ANSI, K&R, 32-, and 64-bit) available, and to be easyto use.Libmng uses zlib for its compression and decompression of MNG files.Further information about zlib, and the latest version of zlib, can befound at the zlib home page, <http://www.info-zip.org/pub/infozip/zlib/>.The zlib compression utility is a general purpose utility that isuseful for more than MNG/PNG files, and can be used without libmng.See the documentation delivered with zlib for more details.Libmng optionally uses the JPEG library by the Independant JPEG Group(IJG). This library is used for the JNG sub-format, which is part ofthe MNG specification, and allows for inclusion of JPEG decoded andthus highly compressed (photographic) images.Further information about the IJG JPEG library and the latest sourcescan be found at <http://www.ijg.org>.Libmng can also optionally use the lcms (little CMS) library byMarti Maria Saguer. This library provides an excellent color-managementsystem (CMS), which gives libmng the ability to provide fullcolor-correction for images with the proper color-information encoded.Further information and the latest sources can be found at<http://www.littlecms.com/>.Libmng is thread safe, provided the threads are using differenthandles as returned by the initialization call.Each thread should have its own handle and thus its own image.Libmng does not protect itself against two threads using thesame instance of a handle.The libmng.h header file is the single reference needed for programmingwith libmng:#include <libmng.h>II. CallbacksLibmng makes extensive use of callback functions. This is meant tokeep the library as platform-independant and flexible as possible.Actually, the first call you will make to the library, already containsthree parameters you can use to provide callback entry-points.Most functions must return a mng_bool (boolean). Returning MNG_FALSEindicates the library the callback failed in some way and the librarywill immediately return from whatever it was doing back to theapplication. Returning MNG_TRUE indicates there were no problems andprocessing can continue.Let's step through each of the possible callbacks. The sections onreading, displaying and writing will also explain which callbacks areneeded when and where.- mng_ptr mng_memalloc (mng_size_t iLen)A very basic function which the library uses to allocate a memory-blockwith the given size. A typical implementation would be: mng_ptr my_alloc (mng_size_t iLen) { return calloc (1, iSize); }Note that the library requires you to zero-out the memory-block!!!- void mng_memfree (mng_ptr pPtr, mng_size_t iLen)Counterpart of the previous function. Typically: void my_free (mng_ptr pPtr, mng_size_t iLen) { free (pPtr); }- mng_bool mng_openstream (mng_handle hHandle)- mng_bool mng_closestream (mng_handle hHandle)These are called by the library just before it starts to process(either read or write) a file and just after the processing stops.This is the recommended place to do I/O initialization & finalization.Whether you do or not, is up to you. The library does not put anymeaning into the calls. They are simply provided for your convenience.- mng_bool mng_readdata (mng_handle hHandle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead) This function is called when the library needs some more input whilereading an image. The reading process supports two modes:Suspension-mode (SMOD) and non-suspension-mode (NSMOD).See mng_set_suspensionmode() for a more detailed description.In NSMOD, the library requires you to return exactly the amount of bytesrequested (= iBuflen). Any lesser amount indicates the input fileis exhausted and the library will return a MNG_UNEXPECTEDEOF errorcode.In SMOD, you may return a smaller amount of bytes than requested.This tells the library it should temporarily wait for more input toarrive. The lib will return with MNG_NEEDMOREDATA, and will expect acall to mng_read_resume() or mng_display_resume() next, as soon asmore input-data has arrived.For NSMOD this function could be as simple as: mng_bool my_read (mng_handle hHandle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead) { *pRead = fread (pBuf, 1, iBuflen, myfile); return MNG_TRUE; }- mng_bool mng_writedata (mng_handle hHandle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pWritten)This function is called during the mng_write() function to actuallyoutput data to the file. There is no suspension-mode during write,so the application must return the exact number of bytes the libraryrequests to be written.A typical implementation could be: mng_bool my_write (mng_handle hHandle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pWritten) { *pWritten = fwrite (pBuf, 1, iBuflen, myfile); return MNG_TRUE; }- mng_bool mng_errorproc (mng_handle hHandle, mng_int32 iErrorcode, mng_int8 iSeverity, mng_chunkid iChunkname, mng_uint32 iChunkseq, mng_int32 iExtra1, mng_int32 iExtra2, mng_pchar zErrortext)This function is called whenever an error is detected inside thelibrary. This may be caused by invalid input, callbacks indicatingfailure, or wrongfully calling functions out of place.If you do not provide this callback the library will still returnan errorcode from the called function, and the mng_getlasterror()function can be used to retrieve the other parameters.This function is currently only provided for convenience, but mayat some point be used to indicate certain errors may be acceptable,and processing should continue.- mng_bool mng_traceproc (mng_handle hHandle, mng_int32 iFuncnr, mng_int32 iFuncseq, mng_pchar zFuncname)This function is provided to allow a functional analysis of thelibrary. This may be useful if you encounter certain errors andcannot determine what the problem is.Almost all functions inside the library will activate thiscallback with an appropriate function-name at the start and endof the function. Please note that large images may generate anenormous amount of calls.- mng_bool mng_processheader (mng_handle hHandle, mng_uint32 iWidth, mng_uint32 iHeight)This function is called once the header information of an input-image has been processed. At this point the image dimensions areavailable and also some other properties depending on the typeof the image. Eg. for a MNG the frame-/layercount, playtime &simplicity fields are known.The primary purpose of this callback is to inform the applicationof the size of the image, and for the application to initializethe drawing canvas to be used by the library. This is also a goodpoint to set the canvas-style. Eg. mng_set_canvasstyle().- mng_bool mng_processtext (mng_handle hHandle, mng_uint8 iType, mng_pchar zKeyword, mng_pchar zText, mng_pchar zLanguage, mng_pchar zTranslation)This callback is activated for each textual chunk in the input-image. These are tEXt, zTXt & iTXt. It may be used to retainspecific comments for presentation to the user.- mng_bool mng_processsave (mng_handle hHandle)- mng_bool mng_processseek (mng_handle hHandle, mng_pchar zName)The purpose of these callbacks is to signal the processing of theSAVE & SEEK chunks in a MNG input-file. This may be used in thefuture to specify some special processing. At the moment thesefunctions are only provided as a signal.- mng_ptr mng_getcanvasline (mng_handle hHandle, mng_uint32 iLinenr)- mng_ptr mng_getbkgdline (mng_handle hHandle, mng_uint32 iLinenr)- mng_ptr mng_getalphaline (mng_handle hHandle, mng_uint32 iLinenr)These callbacks are used to access the drawing canvas, backgroundcanvas and an optional separate alpha-channel canvas. The latter isused only with the MNG_CANVAS_RGB8_A8 canvas-style.If the getbkgdline() callback is not supplied the library willcomposite full or partially transparent pixels in the image againsta specified background color. See mng_set_bgcolor() for more details.If a chosen canvas-style includes an alpha-channel, this callbackis very likely not needed.The application is responsible for returning a pointer to a line ofpixels, which should be in the exact format as defined by the callto mng_set_canvasstyle() and mng_set_bkgdstyle(), without gaps betweenthe representation of each pixel.- mng_bool mng_refresh (mng_handle hHandle, mng_uint32 iX, mng_uint32 iY, mng_uint32 iWidth, mng_uint32 iHeight)This callback is called when the library has drawn a complete frameonto the drawing canvas, and it is ready to be displayed.The application is responsible for transferring the drawing canvasfrom memory onto the actual output device.- mng_uint32 mng_gettickcount (mng_handle hHandle)This function should return the number of milliseconds on some internalclock. The entire animation timing depends heavily on this function,1and the number returned should be as accurate as possible.- mng_bool mng_settimer (mng_handle hHandle, mng_uint32 iMsecs)This callback is activated every time the library requires a "pause".Note that the function itself should NOT execute the wait. It shouldsimply store the time-field and allow the library to return. Libmngwill return with the MNG_NEEDTIMERWAIT code, indicating the callbackwas called and it is now time to execute the pause.After the indicated number of milliseconds have elapsed, the applicationshould call mng_display_resume(), to resume the animation as planned.This method allows for both a real timer or a simple wait command in theapplication. Whichever method you select, both the gettickcount() andsettimer() callbacks are crucial for proper animation timing.- mng_bool mng_processgamma (mng_handle hHandle, mng_uint32 iGamma)- mng_bool mng_processchroma (mng_handle hHandle, mng_uint32 iWhitepointx, mng_uint32 iWhitepointy, mng_uint32 iRedx, mng_uint32 iRedy, mng_uint32 iGreenx, mng_uint32 iGreeny, mng_uint32 iBluex, mng_uint32 iBluey)- mng_bool mng_processsrgb (mng_handle hHandle, mng_uint8 iRenderingintent)- mng_bool mng_processiccp (mng_handle hHandle, mng_uint32 iProfilesize, mng_ptr pProfile)- mng_bool mng_processarow (mng_handle hHandle, mng_uint32 iRowsamples, mng_bool bIsRGBA16, mng_ptr pRow)These callbacks are only required when you selected the MNG_APP_CMSdirective during compilation of the library. See the configurationsection for more details.- mng_bool mng_iteratechunk (mng_handle hHandle, mng_handle hChunk, mng_chunkid iChunkid, mng_uint32 iChunkseq)This callback is only used for the mng_iterate_chunks() function.It is called exactly once for each chunk stored.III. Housekeeping> Memory managementThe library can use internal memory allocation/deallocation or useprovided callbacks for its memory management. The choice is made atcompilation time. See the section on customization for details.If internal management has been selected, the memory callback functionsneed not be supplied. Even if you do supply them they will not be used.The actual code used is similar to the code discussed in the callbacksection: pPtr = calloc (1, iSize); free (pPtr);If your compiler does not support these functions, or you wish to monitorthe library's use of memory for certain reasons, you can choose tocompile the library with external memory management. In this case thememory callback functions MUST be supplied, and should function as if theabove code was used.> InitializationThe basic initialization of the library is short and swift: myhandle = mng_initialize (myuserdata, my_alloc, my_free, MNG_NULL); if (myhandle == MNG_NULL) /* process error */;The first field is an application-only parameter. It is saved inlibmng's internal structures and available at all times through themng_get_userdata() function. This is especially handy in callback functionsif your program may be handling multiple files at the same time.The second and third field supply the library with the memory callback1function entry-points. These are described in more detail in the callbacksection and the previous paragraph.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -