📄 jpegll_enc.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "idc.h"
/*****************<*****************************************************
* *
* File: jpegll_enc.c *
* Function: decorrelates an image using the lossless JPEG predictors *
* Author (aka person to blame) : K. Sayood *
* Last mod: 5/12/95 *
* Usage: see usage() *
***********************************************************************/
/*
* Modified by Jim Diamond 20080212 to fix a couple of bugs (some minor,
* one show stopper.
*/
/*******************************************************************************
* NOTICE: *
* This code is believed by the author to be bug free. You are free to use *
* and modify this code with the understanding that any use, either direct or *
* derivative, must contain acknowledgement of its author and source. The *
* author makes no warranty of any kind, expressed or implied, of *
* merchantability or fitness for a particular purpose. The author shall *
* not be held liable for any incidental or consequential damages in *
* connection with or arising out of the furnishing, performance, or use of *
* this software. This software is not authorized for use in life support *
* devices or systems. *
******************************************************************************/
void usage(void);
int
main(int argc, char *argv[])
{
int row, col, row_size, col_size, temp, mode,pred;
char * inimage, * resimage;
unsigned char ** image_in, ** res_image;
int c;
FILE * ofp;
extern int optind;
extern char * optarg;
ofp = stdout;
resimage = "standard out";
mode = -1;
row_size = -1;
col_size = -1;
while((c = getopt(argc,argv,"i:o:m:x:y:h")) != EOF)
{
switch (c)
{
case 'i':
inimage = strdup(optarg);
break;
case 'o':
resimage = strdup(optarg);
ofp = fopen(optarg, "wb");
if (ofp == NULL)
{
fprintf(stderr, "%s: Unable to open '%s' for writing. Bye!\n",
argv[0], optarg);
return 1;
}
break;
case 'm':
sscanf(optarg,"%d", &mode);
break;
case 'x':
sscanf(optarg,"%d", &row_size);
break;
case 'y':
sscanf(optarg,"%d", &col_size);
break;
case 'h':
usage();
return 1;
}
}
if (mode < 0)
{
fprintf(stderr, "Enter lossless JPEG mode (0 .. 7) : ");
scanf("%d", &mode);
}
fprintf(stderr, "\n\n\t\tJPEG Lossless Compression \n");
fprintf(stderr, "This program generates the prediction errors "
"(residuals)\n");
fprintf(stderr, "using the different JPEG lossless predictive modes.\n");
fprintf(stderr, "The selected JPEG mode is %d and the residual\n", mode);
fprintf(stderr, "image is written to %s\n\n", resimage);
fprintf(stderr, "In order to obtain compression, these residuals should\n");
fprintf(stderr, "be encoded using a variable rate coder.\n");
/*
* If the image dimensions have not been provided find the image
* dimensions
*/
if (row_size < 0 || col_size < 0)
image_size(inimage, &row_size, &col_size);
fprintf(stderr, "\nImage size: %d X %d\n", col_size, row_size);
/* Allocate space for the input and residual images */
image_in = (unsigned char **)malloc(row_size * sizeof(char *));
for(row=0; row<row_size; row++)
image_in[row] = (unsigned char *)malloc(col_size * sizeof(char));
res_image = (unsigned char **)malloc(row_size * sizeof(char *));
for(row = 0; row < row_size; row++)
res_image[row] = (unsigned char *)malloc(col_size * sizeof(char));
/* Read the image to be decorrelated */
readimage(inimage, image_in, row_size, col_size);
/* Generate prediction using the prediction mode selected */
for (row = 0; row < row_size; row++)
for (col = 0; col < col_size; col++)
{
switch(mode)
{
case 0:
pred = 0;
break;
case 1:
if (row == 0 && col == 0)
pred = 0;
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = image_in[row - 1][col];
break;
case 2:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else
pred = image_in[row][col - 1];
break;
case 3:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = image_in[row - 1][col - 1];
break;
case 4:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = image_in[row][col - 1]
+ image_in[row - 1][col] - image_in[row - 1][col - 1];
break;
case 5:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = image_in[row][col - 1]
+ (image_in[row-1][col] - image_in[row-1][col- 1]) / 2;
break;
case 6:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = image_in[row - 1][col]
+ (image_in[row][col-1] - image_in[row-1][col - 1]) / 2;
break;
case 7:
if (row == 0 && col == 0)
pred = 0;
else if (col == 0)
pred = image_in[row - 1][col];
else if (row == 0)
pred = image_in[row][col - 1];
else
pred = (image_in[row][col - 1] + image_in[row-1][col]) / 2;
break;
default:
fprintf(stderr, "No valid JPEG mode was specified");
return 1;
}
/* Generate the residual */
temp = image_in[row][col] - pred;
/* Represent the residual modulo 256 */
while (temp < 0)
temp += 256;
while (temp > 255)
temp -= 256;
res_image[row][col] = temp;
#ifdef DEBUG
fprintf(stderr, "row %d col %d: pixel %d, pred %d, resid %d; "
"changed from %d\n", row, col, image_in[row][col],
pred, temp, image_in[row][col] - pred);
#endif
}
/* Store JPEG mode and dimensions of the image */
fwrite(&mode, 1, sizeof(int), ofp);
fwrite(&col_size, 1, sizeof(int), ofp);
fwrite(&row_size, 1, sizeof(int), ofp);
/* Store residual image */
for (row = 0; row < row_size; row++)
for (col = 0; col < col_size; col++)
putc(res_image[row][col], ofp);
return 0;
}
void
usage(void)
{
fprintf(stderr, "Usage: jpegll_enc [-i infile] [-o outfile] [-m mode]\n");
fprintf(stderr, "\t [-x row_size] [-y [col_size]\n");
fprintf(stderr, "\tinfile: The file containing the image. It is assumed\n");
fprintf(stderr, "\t\tthat the image is a grey level (8 bits/pixel) image\n");
fprintf(stderr, "\t\tstored in raw format. To read from standard in modify\n");
fprintf(stderr, "\t\treadimage.c\n");
fprintf(stderr, "\toutfile: The file containing the residual or decorrelated\n");
fprintf(stderr, "\t\timage. No variable lengrh coding has been performed so\n");
fprintf(stderr, "\t\tthe outfile will be bigger than the infile. To get a\n");
fprintf(stderr, "\t\tcompressed file use a variable length coder such as huff_enc\n");
fprintf(stderr, "\t\ton outfile. To pipe the output of this program directly\n");
fprintf(stderr, "\t\tto another program omit this option. This will result in\n");
fprintf(stderr, "\t\tthe output being written to standard out.\n");
fprintf(stderr, "\tmode: an integer between 0 and 7 corresponding\n");
fprintf(stderr, "\t\tto the eight lossless JPEG modes\n");
fprintf(stderr, "\trow_size: Length of a row of the image\n");
fprintf(stderr, "\tcol_size: Length of a column of the image\n");
fprintf(stderr, "\t\tIf the column or row size is not provided the program\n");
fprintf(stderr, "\t\twill check to see if the iamge corresponds to any of the\n");
fprintf(stderr, "\t\tstandard sizes it is familiar with. To add to the list of\n");
fprintf(stderr, "\t\tstandard sizes, edit image_size.c\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -