⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ljpgtopnm.c

📁 很好的JPEG图象解码器,基于VC环境开发。
💻 C
字号:
/* * ljpgtopnm.c -- * * This is the main routine for the lossless JPEG decoder.  Large * parts are stolen from the IJG code, so: * * Copyright (C) 1991, 1992, Thomas G. Lane. * Part of the Independent JPEG Group's software. * See the file Copyright for more details. * * Copyright (c) 1993 Brian C. Smith, The Regents of the University * of California * All rights reserved. *  * Copyright (c) 1994 Kongji Huang and Brian C. Smith. * Cornell University * All rights reserved. *  * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. *  * IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#include "jpeg.h"#include "mcu.h"#include "proto.h"/*  * input and output file pointers  */FILE *inFile, *outFile;/* *-------------------------------------------------------------- * * ReadJpegData -- * *	This is an interface routine to the JPEG library.  The *	JPEG library calls this routine to "get more data" * * Results: *	Number of bytes actually returned. * * Side effects: *	None. * *-------------------------------------------------------------- */intReadJpegData (buffer, numBytes)    char *buffer;		/* Place to put new data */    int numBytes;		/* Number of bytes to put */{    return fread(buffer, 1, numBytes, inFile);}/* *-------------------------------------------------------------- * * WritePmHeader -- * *	Output Portable Pixmap (PPM) or Portable *	Graymap (PGM) image header. * * Results: *	None. * * Side effects: *      The PPM or PGM header is written to file *	pointed by outFile. * *-------------------------------------------------------------- */voidWritePmHeader(dcInfo)DecompressInfo dcInfo;{    switch(dcInfo.numComponents) {    case 1: /* pgm */       if (dcInfo.dataPrecision==8) {          fprintf(outFile,"P5\n%d %d\n255\n",                  dcInfo.imageWidth,dcInfo.imageHeight);       } else {          fprintf(outFile,"P5\n%d %d\n%d\n",                  dcInfo.imageWidth,dcInfo.imageHeight,                  ((1<<dcInfo.dataPrecision)-1));       }       break;    case 3: /* ppm */       if (dcInfo.dataPrecision==8) {          fprintf(outFile,"P6\n%d %d\n255\n",                  dcInfo.imageWidth,dcInfo.imageHeight);       } else {          fprintf(outFile,"P6\n%d %d\n%d\n",                  dcInfo.imageWidth,dcInfo.imageHeight,                  ((1<<dcInfo.dataPrecision)-1));       }       break;    default:      fprintf(stderr,"Error: Unsupported image format.\n");      exit(-1);    }}/* *-------------------------------------------------------------- * * ArgParser -- * *      Command line parser. * * Results: *      Command line parameters and options are passed out. * * Side effects: *      None. * *-------------------------------------------------------------- */static voidArgParser(argc,argv,verbose,inFile,outFile)    int argc;    char **argv;    int *verbose;    FILE **inFile, **outFile;{    int argn;    char *arg;    char *usage="ppmtoljpeg [ -v -h ] [ inFile [outFile] ]";    int NumOfFile=0;    int i,selValue;    /*     * default values     */    *inFile=stdin;    *outFile=stdout;    *verbose=0;    for (argn = 1; argn < argc; argn++) {        arg=argv[argn];        if (*arg != '-') { /* process a file name */           if (NumOfFile==0) {              if ((*inFile=fopen(arg,"r"))==NULL) {                 fprintf(stderr,"Can't open %s\n",arg);                 exit(-1);              }           }           if (NumOfFile==1) {              if ((*outFile=fopen(arg,"w"))==NULL) {                 fprintf(stderr,"Can't open %s\n",arg);                 exit(-1);              }           }           if (NumOfFile>1) {              fprintf(stderr,"%s\n",usage);              exit(-1);           }           NumOfFile++;        }        else { /* precess a option */           arg++;           switch (*arg) {             case 'h':                       /* help flag */                       fprintf(stderr,"Decode a lossless JPEG image into ");                       fprintf(stderr,"a PPM or PGM image.\n");                       fprintf(stderr,"Usage:\n");                       fprintf(stderr,"%s\n",usage);                       fprintf(stderr,"Default  input: stdin\n");                       fprintf(stderr,"Default output: stdout\n");                       fprintf(stderr,"-h  help\n");                       fprintf(stderr,"-v  verbose\n");                       exit(1);                       break;             case 'v':                       /* verbose flag */                       *verbose=1;                       break;             default:                       fprintf(stderr,"%s\n",usage);                       exit(-1);           }        }    }}intmain(argc, argv)    int argc;    char **argv;{    DecompressInfo dcInfo;    int verbose;    /*     * Process command line parameters.     */    MEMSET(&dcInfo, 0, sizeof(dcInfo));    ArgParser(argc,argv,&verbose,&inFile,&outFile);    /*     * Read the JPEG File header, up to scan header, and initialize all     * the variables in the decompression information structure.     */    ReadFileHeader (&dcInfo);    /*     * Loop through each scan in image. ReadScanHeader returns     * 0 once it consumes and EOI marker.     */    if (!ReadScanHeader (&dcInfo)) {	fprintf (stderr, "Empty JPEG file\n");	exit (1);    }    /*     * Output image parameter if verbose flag is on.     */    if (verbose) {       fprintf(stderr,"sample precision=%d\n",dcInfo.dataPrecision);       fprintf(stderr,"image height=%d\n",dcInfo.imageHeight);       fprintf(stderr,"image width=%d\n",dcInfo.imageWidth);       fprintf(stderr,"component=%d\n",dcInfo.numComponents);    }    /*      * Write PPM or PGM image header. Decode the image bits     * stream. Clean up everything when finished decoding.     */    WritePmHeader(dcInfo);    DecoderStructInit(&dcInfo);    HuffDecoderInit(&dcInfo);    DecodeImage(&dcInfo);    FreeArray2D(mcuROW1);    FreeArray2D(mcuROW2);    if (ReadScanHeader (&dcInfo)) {	fprintf (stderr, "Warning: multiple scans detected in JPEG file\n");	fprintf (stderr, "         not currently supported\n");	fprintf (stderr, "         ignoring extra scans\n");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -