📄 bpe_encoder.c
字号:
/*
Implementation of CCSDS 122.0-B-1 Recommended Standard
Please note:
(1) Before you download and use the program, you must read and agree the license agreement carefully.
(2) We supply the source code and program WITHOUT ANY WARRANTIES. The users will be responsible
for any loses or damages caused by the use of the source code and the program.
Author:
Hongqiang Wang
Department of Electrical Engineering
University of Nebraska-Lincoln
Email: hqwang@bigred.unl.edu, hqwang@eecomm.unl.edu
Your comment and suggestions are welcome. Please report bugs to me via email and I would greatly appreciate it.
Jan. 21, 2007
*/
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include "global.h"
extern void DWT_(StructCodingPara *, int **, int **);
extern void HeaderUpdate(HeaderStruct * );
extern void DCEncoding(StructCodingPara *, long **, BitPlaneBits *);
extern void ACBpeEncoding(StructCodingPara *, BitPlaneBits *);
int ImageSize(StructCodingPara * );
short ImageRead(StructCodingPara *, int **);
int ImageSize(StructCodingPara * PtrStructCodingPara)
{
/*
Written by James R. Nau, 6-Oct-89
Attempt to find the size of an image in file pointed to by *file.
Number of rows (down), and the number of columns (across) is returned.
Uses a stat call to find the size of the file. Then, attempts to find
a pertinent number of rows and columns that we normally use. Currently
supported formats:
row x column size Format
128x128 16,384 Small Image
320x200 64,000 IBM VGA Style
256x256 65,536 Standard NASA Image (small)
256x384 98,304 IVG Low-Res Image
512x384 196,608 IVG High-Res Image
512x512 262,144 Standard NASA Image (large)
640x480 307,200 Super VGA (from GIF usually)
720x480 345,600
576x720 414,720 JPEG images
Parameters:
*file: pointer to filename containing image
StructCodingPara->row_sizeow: pointer to the number of rows we decided on
StructCodingPara->col_sizeolumn: pointer to number of columns we decided on
Return values:
0: ok
-1: Couldn't use stat function
-2: Unknown image size
*/
struct stat status;
int res;
UINT32 img_len;
char buffer[BUFFER_LENGTH];
// FILE *FF = fopen("spot.raw", "r");
res = stat(PtrStructCodingPara->InputFile, &status);
if (res != 0)
{
return (-1);
}
img_len = status.st_size;
if((PtrStructCodingPara->ImageRows > 0 ) && (PtrStructCodingPara->ImageWidth > 0)) // user did specify the image size.
{
short temp = PtrStructCodingPara->PtrHeader->Header.Part4.PixelBitDepth_4Bits;
if ((temp == 0) || temp > 8)
temp = 16;
else
temp = 8;
if (img_len == ((PtrStructCodingPara->ImageRows) * (PtrStructCodingPara->ImageWidth) * temp / 8)) // user defined
return BPE_OK;
else
ErrorMsg(BPE_FILE_ERROR);
}
if (img_len == 16384L) /* For BUD's images */
{
PtrStructCodingPara->ImageRows = 128;
PtrStructCodingPara->ImageWidth = 128;
}
else if (img_len == 64000L) /* IBM VGA Screen */
{
PtrStructCodingPara->ImageRows = 200;
PtrStructCodingPara->ImageWidth = 320;
}
else if (img_len == 65536L) /* Standard */
{
PtrStructCodingPara->ImageRows = 256;
PtrStructCodingPara->ImageWidth = 256;
}
else if (img_len == 98304L) /* Low-Res IVG images */
{
PtrStructCodingPara->ImageRows = 256;
PtrStructCodingPara->ImageWidth = 384;
}
else if (img_len == 196608L) /* Hi-Res IVG images */
{
PtrStructCodingPara->ImageRows = 512;
PtrStructCodingPara->ImageWidth = 384;
}
else if (img_len == 262144L) /* Standard, but large... */
{
PtrStructCodingPara->ImageRows = 512;
PtrStructCodingPara->ImageWidth = 512;
}
else if (img_len == 307200L) /* SVGA Hi-Res from GIFs */
{
PtrStructCodingPara->ImageRows = 480;
PtrStructCodingPara->ImageWidth = 640;
}
else if (img_len == 345600L) /* mpeg frame 720 x 480 */
{
PtrStructCodingPara->ImageRows = 720;
PtrStructCodingPara->ImageWidth = 480;
}
else if (img_len == 414720L) /* jpeg test image 576 x 720 */
{
PtrStructCodingPara->ImageRows = 576;
PtrStructCodingPara->ImageWidth = 720;
}
else if (img_len == 524288)
{
PtrStructCodingPara->ImageRows = 512;
PtrStructCodingPara->ImageWidth = 512;
PtrStructCodingPara->PtrHeader->Header.Part4.PixelBitDepth_4Bits = 0;
}
else
{
sprintf(buffer, "\nError: unknown image size: %ld\n", img_len);
DebugInfo(buffer);
ErrorMsg(BPE_FILE_ERROR);
}
if ((PtrStructCodingPara->ImageRows) * (PtrStructCodingPara->ImageWidth)
* (PtrStructCodingPara->PtrHeader->Header.Part4.PixelBitDepth_4Bits) / 8 != img_len)
{
sprintf(buffer, "\nError: incorrect image size: %ld\n", img_len);
DebugInfo(buffer);
ErrorMsg(BPE_FILE_ERROR);
}
return (BPE_OK);
}
short ImageRead(StructCodingPara *StrPtr,
int **image)
{
UINT32 r = 0;
UINT32 i = 0;
FILE *infile = NULL;
infile = fopen(StrPtr->InputFile,"rb");
if (infile == NULL)
{
DebugInfo("Error: Unable to read the image file!\n");
ErrorMsg(BPE_FILE_ERROR);
}
if (StrPtr->PtrHeader->Header.Part4.PixelBitDepth_4Bits <= 8
&& StrPtr->PtrHeader->Header.Part4.PixelBitDepth_4Bits != 0)
{
if(StrPtr->PtrHeader->Header.Part4.SignedPixels == FALSE) // unsigned image
{
UCHAR8 *temp;
temp = (UCHAR8 *)calloc(sizeof(UCHAR8), StrPtr->ImageWidth);
for(r = 0; r < StrPtr->ImageRows; r++)
{
fread(temp, StrPtr->ImageWidth, sizeof(char),infile);
for(i = 0; i < StrPtr->ImageWidth; i++)
image[r][i] = temp[i];
}
free(temp);
}
else
{
char *temp;
temp = (char *)calloc(sizeof(char), StrPtr->ImageWidth);
for(r = 0; r < StrPtr->ImageRows; r++)
{
fread(temp, StrPtr->ImageWidth, sizeof(char),infile);
for(i = 0; i < StrPtr->ImageWidth; i++)
image[r][i] = temp[i];
}
free(temp);
}
}
else if(StrPtr->PtrHeader->Header.Part4.PixelBitDepth_4Bits == 0 ||
StrPtr->PtrHeader->Header.Part4.PixelBitDepth_4Bits <= 15) // it is 16 bits
{
if(StrPtr->PtrHeader->Header.Part4.SignedPixels == FALSE) // unsigned image
{
WORD16 *temp_16;
temp_16 = (WORD16 *)calloc(sizeof(WORD16), StrPtr->ImageWidth);
for(r=0; r<StrPtr->ImageRows; r++)
{
fread(temp_16, StrPtr->ImageWidth, sizeof(short),infile);
for(i = 0; i < StrPtr->ImageWidth; i++)
{
image[r][i] = ((temp_16[i] & 0x00FF) << 8) + ((temp_16[i] & 0xFF00) >> 8);
}
}
free(temp_16);
}
else
{
short *temp_16;
temp_16 = (short *)calloc(sizeof(short), StrPtr->ImageWidth);
for(r=0; r<StrPtr->ImageRows; r++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -