📄 jpeg.c
字号:
//// $Id: jpeg.c,v 1.2 2000/08/29 08:05:33 weiym Exp $// // jpg.c: Low-level JPEG file read/save routines.// // Copyright (C) 2000, BluePoint Software.// Copyright (C) 2000, Wei Yongming//// Current maintainer: Wei Yongming//// Create date: 2000/08/29/*** This library is free software; you can redistribute it and/or** modify it under the terms of the GNU Library General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** This library is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU** Library General Public License for more details.**** You should have received a copy of the GNU Library General Public** License along with this library; if not, write to the Free** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,** MA 02111-1307, USA*//* * JPEG decompression routine * * JPEG support must be enabled (see README.txt in contrib/jpeg) * * SOME FINE POINTS: (from libjpeg) * In the below code, we ignored the return value of jpeg_read_scanlines, * which is the number of scanlines actually read. We could get away with * this because we asked for only one line at a time and we weren't using * a suspending data source. See libjpeg.doc for more info. * * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress(); * we should have done it beforehand to ensure that the space would be * counted against the JPEG max_memory setting. In some systems the above * code would risk an out-of-memory error. However, in general we don't * know the output image dimensions before jpeg_start_decompress(), unless we * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this. * * Scanlines are returned in the same order as they appear in the JPEG file, * which is standardly top-to-bottom. If you must emit data bottom-to-top, * you can use one of the virtual arrays provided by the JPEG memory manager * to invert the data. See wrbmp.c for an example. * * As with compression, some operating modes may require temporary files. * On some systems you may need to set up a signal handler to ensure that * temporary files are deleted if the program is interrupted. See libjpeg.doc. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "../image/jpeglib.h"#include "common.h"#include "gdi.h"#include "readbmp.h"int load_jpg (FILE *fp, MYBITMAP* bmp, RGB* pal){ int i; int ret = ERR_BMP_LOAD; /* image load error*/ unsigned char magic[4]; /* This struct contains the JPEG decompression parameters * and pointers to working space * (which is allocated as needed by the JPEG library). */ struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; /* first determine if JPEG file since decoder will error if not*/ fseek(fp, 0L, 0); if(!fread(magic, 2, 1, fp)) return ERR_BMP_IMAGE_TYPE; /* not JPEG image*/ if (magic[0] != 0xFF || magic[1] != 0xD8) return ERR_BMP_IMAGE_TYPE; /* not JPEG image*/ fread(magic, 4, 1, fp); fread(magic, 4, 1, fp); if (strncmp(magic, "JFIF", 4) != 0) return ERR_BMP_IMAGE_TYPE; /* not JPEG image*/ fseek(fp, 0L, 0); /* Step 1: allocate and initialize JPEG decompression object */ /* We set up the normal JPEG error routines. */ cinfo.err = jpeg_std_error (&jerr); /* Now we can initialize the JPEG decompression object. */ jpeg_create_decompress (&cinfo); /* Step 2: specify data source (eg, a file) */ jpeg_stdio_src (&cinfo, fp); /* Step 3: read file parameters with jpeg_read_header() */ jpeg_read_header (&cinfo, TRUE); /* Step 4: set parameters for decompression */ cinfo.out_color_space = (bmp->flags & LOADBMP_FLAG_GRAYSCALE) ? JCS_GRAYSCALE: JCS_RGB; cinfo.quantize_colors = FALSE; if (!bmp->flags & LOADBMP_FLAG_GRAYSCALE) { if (bmp->depth <= 8) { cinfo.quantize_colors = TRUE; /* Get system palette */ cinfo.actual_number_of_colors = 0x01 << bmp->depth; /* Allocate jpeg colormap space */ cinfo.colormap = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, (JDIMENSION)cinfo.actual_number_of_colors, (JDIMENSION)3); /* Set colormap from system palette */ for(i = 0; i < cinfo.actual_number_of_colors; ++i) { cinfo.colormap[0][i] = pal[i].r >> 2; cinfo.colormap[1][i] = pal[i].g >> 2; cinfo.colormap[2][i] = pal[i].b >> 2; } } } else { /* Grayscale output asked */ cinfo.quantize_colors = TRUE; cinfo.out_color_space = JCS_GRAYSCALE; cinfo.desired_number_of_colors = 256; for (i = 0; i < 256; i++) { pal [i].r = i; pal [i].g = i; pal [i].b = i; } } jpeg_calc_output_dimensions (&cinfo); ret = ERR_BMP_MEM; bmp->w = cinfo.output_width; bmp->h = cinfo.output_height; bmp->frames = 1; bmp->depth = (bmp->flags & LOADBMP_FLAG_GRAYSCALE) ? 8 : cinfo.output_components * 8; bmpComputePitch (bmp->depth, bmp->w, &bmp->pitch, TRUE); bmp->rgb_order = RGB_ORDER_RGB; bmp->bits = malloc(bmp->pitch * bmp->h); if (!bmp->bits) goto err; /* Step 5: Start decompressor */ jpeg_start_decompress (&cinfo); /* Step 6: while (scan lines remain to be read) */ while (cinfo.output_scanline < cinfo.output_height) { JSAMPROW rowptr[1]; BYTE* bits; bits = bmp->bits + cinfo.output_scanline * bmp->pitch; rowptr[0] = (JSAMPROW)(bits); jpeg_read_scanlines (&cinfo, rowptr, 1); } ret = ERR_BMP_OK;err: /* Step 7: Finish decompression */ jpeg_finish_decompress (&cinfo); /* Step 8: Release JPEG decompression object */ jpeg_destroy_decompress (&cinfo); /* May want to check to see whether any corrupt-data * warnings occurred (test whether jerr.pub.num_warnings is nonzero). */ return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -