📄 libjpeg.doc
字号:
where the last line invokes the standard source module.WARNING: it is critical that the binary compressed data be read unchanged.On non-Unix systems the stdio library may perform newline translation orotherwise corrupt binary data. To suppress this behavior, you may need to usea "b" option to fopen (as shown above), or use setmode() or another routine toput the stdio stream in binary mode. See cjpeg.c and djpeg.c for code thathas been found to work on many systems.You may not change the data source between calling jpeg_read_header() andjpeg_finish_decompress(). If you wish to read a series of JPEG images froma single source file, you should repeat the jpeg_read_header() tojpeg_finish_decompress() sequence without reinitializing either the JPEGobject or the data source module; this prevents buffered input data frombeing discarded.3. Call jpeg_read_header() to obtain image info.Typical code for this step is just jpeg_read_header(&cinfo, TRUE);This will read the source datastream header markers, up to the beginningof the compressed data proper. On return, the image dimensions and otherinfo have been stored in the JPEG object. The application may wish toconsult this information before selecting decompression parameters.More complex code is necessary if * A suspending data source is used --- in that case jpeg_read_header() may return before it has read all the header data. See "I/O suspension", below. The normal stdio source manager will NOT cause this to happen. * Abbreviated JPEG files are to be processed --- see the section on abbreviated datastreams. Standard applications that deal only in interchange JPEG files need not be concerned with this case either.It is permissible to stop at this point if you just wanted to find out theimage dimensions and other header info for a JPEG file. In that case,call jpeg_destroy() when you are done with the JPEG object, or calljpeg_abort() to return it to an idle state before selecting a new datasource and reading another header.4. Set parameters for decompression.jpeg_read_header() sets appropriate default decompression parameters based onthe properties of the image (in particular, its colorspace). However, youmay well want to alter these defaults before beginning the decompression.For example, the default is to produce full color output from a color file.If you want colormapped output you must ask for it. Other options allow thereturned image to be scaled and allow various speed/quality tradeoffs to beselected. "Decompression parameter selection", below, gives details.If the defaults are appropriate, nothing need be done at this step.Note that all default values are set by each call to jpeg_read_header().If you reuse a decompression object, you cannot expect your parametersettings to be preserved across cycles, as you can for compression.You must set desired parameter values each time.5. jpeg_start_decompress(...);Once the parameter values are satisfactory, call jpeg_start_decompress() tobegin decompression. This will initialize internal state, allocate workingmemory, and prepare for returning data.Typical code is just jpeg_start_decompress(&cinfo);If you have requested a multi-pass operating mode, such as 2-pass colorquantization, jpeg_start_decompress() will do everything needed before dataoutput can begin. In this case jpeg_start_decompress() may take quite a whileto complete. With a single-scan (non progressive) JPEG file and defaultdecompression parameters, this will not happen; jpeg_start_decompress() willreturn quickly.After this call, the final output image dimensions, including any requestedscaling, are available in the JPEG object; so is the selected colormap, ifcolormapped output has been requested. Useful fields include output_width image width and height, as scaled output_height out_color_components # of color components in out_color_space output_components # of color components returned per pixel colormap the selected colormap, if any actual_number_of_colors number of entries in colormapoutput_components is 1 (a colormap index) when quantizing colors; otherwise itequals out_color_components. It is the number of JSAMPLE values that will beemitted per pixel in the output arrays.Typically you will need to allocate data buffers to hold the incoming image.You will need output_width * output_components JSAMPLEs per scanline in youroutput buffer, and a total of output_height scanlines will be returned.Note: if you are using the JPEG library's internal memory manager to allocatedata buffers (as djpeg does), then the manager's protocol requires that yourequest large buffers *before* calling jpeg_start_decompress(). This is alittle tricky since the output_XXX fields are not normally valid then. Youcan make them valid by calling jpeg_calc_output_dimensions() after setting therelevant parameters (scaling, output color space, and quantization flag).6. while (scan lines remain to be read) jpeg_read_scanlines(...);Now you can read the decompressed image data by calling jpeg_read_scanlines()one or more times. At each call, you pass in the maximum number of scanlinesto be read (ie, the height of your working buffer); jpeg_read_scanlines()will return up to that many lines. The return value is the number of linesactually read. The format of the returned data is discussed under "Dataformats", above. Don't forget that grayscale and color JPEGs will returndifferent data formats!Image data is returned in top-to-bottom scanline order. If you must writeout the image in bottom-to-top order, you can use the JPEG library's virtualarray mechanism to invert the data efficiently. Examples of this can befound in the sample application djpeg.The library maintains a count of the number of scanlines returned so farin the output_scanline field of the JPEG object. Usually you can just usethis variable as the loop counter, so that the loop test looks like"while (cinfo.output_scanline < cinfo.output_height)". (Note that the testshould NOT be against image_height, unless you never use scaling. Theimage_height field is the height of the original unscaled image.)The return value always equals the change in the value of output_scanline.If you don't use a suspending data source, it is safe to assume thatjpeg_read_scanlines() reads at least one scanline per call, until thebottom of the image has been reached.If you use a buffer larger than one scanline, it is NOT safe to assume thatjpeg_read_scanlines() fills it. (The current implementation returns only afew scanlines per call, no matter how large a buffer you pass.) So you mustalways provide a loop that calls jpeg_read_scanlines() repeatedly until thewhole image has been read.7. jpeg_finish_decompress(...);After all the image data has been read, call jpeg_finish_decompress() tocomplete the decompression cycle. This causes working memory associatedwith the JPEG object to be released.Typical code: jpeg_finish_decompress(&cinfo);If using the stdio source manager, don't forget to close the source stdiostream if necessary.It is an error to call jpeg_finish_decompress() before reading the correcttotal number of scanlines. If you wish to abort decompression, calljpeg_abort() as discussed below.After completing a decompression cycle, you may dispose of the JPEG object asdiscussed next, or you may use it to decompress another image. In that casereturn to step 2 or 3 as appropriate. If you do not change the sourcemanager, the next image will be read from the same source.8. Release the JPEG decompression object.When you are done with a JPEG decompression object, destroy it by callingjpeg_destroy_decompress() or jpeg_destroy(). The previous discussion ofdestroying compression objects applies here too.Typical code: jpeg_destroy_decompress(&cinfo);9. Aborting.You can abort a decompression cycle by calling jpeg_destroy_decompress() orjpeg_destroy() if you don't need the JPEG object any more, orjpeg_abort_decompress() or jpeg_abort() if you want to reuse the object.The previous discussion of aborting compression cycles applies here too.Mechanics of usage: include files, linking, etc-----------------------------------------------Applications using the JPEG library should include the header file jpeglib.hto obtain declarations of data types and routines. Before includingjpeglib.h, include system headers that define at least the typedefs FILE andsize_t. On ANSI-conforming systems, including <stdio.h> is sufficient; onolder Unix systems, you may need <sys/types.h> to define size_t.If the application needs to refer to individual JPEG library error codes, alsoinclude jerror.h to define those symbols.jpeglib.h indirectly includes the files jconfig.h and jmorecfg.h. If you areinstalling the JPEG header files in a system directory, you will want toinstall all four files: jpeglib.h, jerror.h, jconfig.h, jmorecfg.h.The most convenient way to include the JPEG code into your executable programis to prepare a library file ("libjpeg.a", or a corresponding name on non-Unixmachines) and reference it at your link step. If you use only half of thelibrary (only compression or only decompression), only that much code will beincluded from the library, unless your linker is hopelessly brain-damaged.The supplied makefiles build libjpeg.a automatically (see install.doc).While you can build the JPEG library as a shared library if the whim strikesyou, we don't really recommend it. The trouble with shared libraries is thatat some point you'll probably try to substitute a new version of the librarywithout recompiling the calling applications. That generally doesn't workbecause the parameter struct declarations usually change with each newversion. In other words, the library's API is *not* guaranteed binarycompatible across versions; we only try to ensure source-code compatibility.(In hindsight, it might have been smarter to hide the parameter structs fromapplications and introduce a ton of access functions instead. Too late now,however.)On some systems your application may need to set up a signal handler to ensurethat temporary files are deleted if the program is interrupted. This is mostcritical if you are on MS-DOS and use the jmemdos.c memory manager back end;it will try to grab extended memory for temp files, and that space will NOT befreed automatically. See cjpeg.c or djpeg.c for an example signal handler.It may be worth pointing out that the core JPEG library does not actuallyrequire the stdio library: only the default source/destination managers anderror handler need it. You can use the library in a stdio-less environmentif you replace those modules and use jmemnobs.c (or another memory manager ofyour own devising). More info about the minimum system library requirementsmay be found in jinclude.h.ADVANCED FEATURES=================Compression parameter selection-------------------------------This section describes all the optional parameters you can set for JPEGcompression, as well as the "helper" routines provided to assist in thistask. Proper setting of some parameters requires detailed understandingof the JPEG standard; if you don't know what a parameter is for, it's bestnot to mess with it! See REFERENCES in the README file for pointers tomore info about JPEG.It's a good idea to call jpeg_set_defaults() first, even if you plan to setall the parameters; that way your code is more likely to work with future JPEGlibraries that have additional parameters. For the same reason, we recommendyou use a helper routine where one is provided, in preference to twiddlingcinfo fields directly.The helper routines are:jpeg_set_defaults (j_compress_ptr cinfo) This routine sets all JPEG parameters to reasonable defaults, using only the input image's color space (field in_color_space, which must already be set in cinfo). Many applications will only need to use this routine and perhaps jpeg_set_quality().jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace) Sets the JPEG file's colorspace (field jpeg_color_space) as specified, and sets other color-space-dependent parameters appropriately. See "Special color spaces", below, before using this. A large number of parameters, including all per-component parameters, are set by this routine; if you want to twiddle individual parameters you should call jpeg_set_colorspace() before rather than after.jpeg_default_colorspace (j_compress_ptr cinfo) Selects an appropriate JPEG colorspace based on cinfo->in_color_space, and calls jpeg_set_colorspace(). This is actually a subroutine of jpeg_set_defaults(). It's broken out in case you want to change just the colorspace-dependent JPEG parameters.jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline) Constructs JPEG quantization tables appropriate for the indicated
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -