📄 img.c
字号:
#include "img.h"
//--------------------------------------------------------------------------------------------
// Allocate image buffer
//--------------------------------------------------------------------------------------------
Image *ImageAlloc(int XSize, int YSize)
{
Image *img = (Image *)malloc(sizeof(Image));
if (img==NULL){
ImageError("Fail to allocate image.");
}
img->XSize = XSize; img->YSize = YSize;
img->Pixel = (unsigned char *)calloc(XSize*YSize, sizeof(unsigned char));
if (img->Pixel==NULL){
ImageError("Fail to allocate image.\n");
}
return img;
}
//--------------------------------------------------------------------------------------------
LImage *LImageAlloc(int XSize, int YSize)
{
LImage *img = (LImage *)malloc(sizeof(LImage));
if (img==NULL){
ImageError("Fail to allocate image.");
}
img->XSize = XSize; img->YSize = YSize;
img->Pixel = (int *)calloc(XSize*YSize, sizeof(int));
if (img->Pixel==NULL){
ImageError("Fail to allocate image.\n");
}
return img;
}
//--------------------------------------------------------------------------------------------
FImage *FImageAlloc(int XSize, int YSize)
{
FImage *img = (FImage *)malloc(sizeof(FImage));
if (img==NULL){
ImageError("Fail to allocate image.");
}
img->XSize = XSize; img->YSize = YSize;
img->Pixel = (REAL *)calloc(XSize*YSize, sizeof(REAL));
if (img->Pixel==NULL){
ImageError("Fail to allocate image.\n");
}
return img;
}
//--------------------------------------------------------------------------------------------
// Deallocate image buffers
//--------------------------------------------------------------------------------------------
void LImageDealloc(LImage *img)
{
if (img!=NULL){
free(img->Pixel);
free(img);
}
}
//--------------------------------------------------------------------------------------------
void FImageDealloc(FImage *img)
{
if (img!=NULL){
free(img->Pixel);
free(img);
}
}
//--------------------------------------------------------------------------------------------
void ImageDealloc(Image *img)
{
if (img!=NULL){
free(img->Pixel);
free(img);
}
}
//--------------------------------------------------------------------------------------------
// Read from 8 bit raw image
//--------------------------------------------------------------------------------------------
Image *ImageReadRaw(int xsize, int ysize, char *filename)
{
Image *img;
int i;
FILE *fp;
img = ImageAlloc(xsize, ysize);
if ((fp=fopen(filename, "rb"))==NULL){
ImageError("Fail to read raw image.");
}
i=fread(img->Pixel, sizeof(unsigned char), xsize*ysize, fp);
fclose(fp);
if (i!=ysize*xsize){
ImageError("Fail to read raw image.");
}
return img;
}
//--------------------------------------------------------------------------------------------
FImage *FImageReadRaw(int xsize, int ysize, char *filename)
{
FImage *img;
Image *temp;
int i;
temp = ImageReadRaw(xsize, ysize, filename);
img = FImageAlloc(xsize, ysize);
ImageToFImage(img, temp);
ImageDealloc(temp);
return img;
}
//--------------------------------------------------------------------------------------------
// Write as 8 bit raw image
//--------------------------------------------------------------------------------------------
int ImageWriteRaw(Image *img, char *filename)
{
int i;
FILE *fp;
if((fp=fopen(filename, "wb"))==NULL){
ImageError("Fail to write raw image.\n");
}
i=fwrite(img->Pixel, sizeof(unsigned char), img->YSize*img->XSize, fp);
fclose(fp);
if (i!=(img->YSize)*(img->XSize)){
ImageWarning("Fail to write raw image.\n");
return 0;
}
else{
return i;
}
}
//--------------------------------------------------------------------------------------------
int LImageWriteRaw(LImage *img, char *filename)
{
int i;
Image *temp = ImageAlloc(img->XSize, img->YSize);
if (temp!=NULL){
LImageToImage(temp, img);
i = ImageWriteRaw(temp, filename);
ImageDealloc(temp);
return i;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------------------
int FImageWriteRaw(FImage *img, char *filename)
{
int i;
Image *temp = ImageAlloc(img->XSize, img->YSize);
if (temp!=NULL){
FImageToImage(temp, img);
i = ImageWriteRaw(temp, filename);
ImageDealloc(temp);
return i;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------------------
// Read PGM image
//--------------------------------------------------------------------------------------------
Image *ImageReadPGM(char *PGMFileName)
{
FILE *fp;
Image *img;
char type_p, type_c, str[256];
int XSize, YSize;
if ((fp=fopen(PGMFileName, "rb"))==NULL){
ImageError("Fail to read PGM image.\n");
}
fread(&type_p, sizeof(char), 1, fp);
fread(&type_c, sizeof(char), 1, fp);
rewind(fp);
if ((type_p == 'P') && (type_c == '5')){
fgets (str, 256, fp);
// skip the comments
do {
fgets (str, 256, fp);
} while (str[0] == '#');
sscanf (str, "%d%d", &XSize, &YSize);
fgets (str, 256, fp); // maximum value, always 255
}
else{
ImageWarning("Only binary PGM is supported.\n");
fclose(fp);
return NULL;
}
img = ImageAlloc(XSize, YSize);
/* Offset from the end of the file - avoid reading comments etc. */
fseek(fp, -1*XSize*YSize, SEEK_END);
if (fread(img->Pixel, sizeof(unsigned char),
img->XSize*img->YSize, fp) != (unsigned int)img->XSize*img->YSize){
fclose(fp);
ImageDealloc(img);
return NULL;
}
else{
fclose(fp);
return img;
}
}
//--------------------------------------------------------------------------------------------
LImage *LImageReadPGM(char *PGMFileName)
{
LImage *img;
Image *temp = ImageReadPGM(PGMFileName);
if (temp!=NULL){
img = LImageAlloc(temp->XSize, temp->YSize);
ImageToLImage(img, temp);
ImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
FImage *FImageReadPGM(char *PGMFileName)
{
FImage *img;
Image *temp = ImageReadPGM(PGMFileName);
if (temp!=NULL){
img = FImageAlloc(temp->XSize, temp->YSize);
ImageToFImage(img, temp);
ImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
// Write PGM image
//--------------------------------------------------------------------------------------------
int ImageWritePGM(Image *img, char *PGMFileName)
{
FILE *fp;
int i;
if ((fp=fopen(PGMFileName, "wb+"))==NULL){
return 0;
}
fprintf(fp, "P5\n%d %d\n255\n", img->XSize, img->YSize);
i=fwrite(img->Pixel, sizeof(unsigned char), img->XSize*img->YSize, fp);
fclose(fp);
if (i!=img->XSize*img->YSize){
return 0;
}
else{
return i;
}
}
//--------------------------------------------------------------------------------------------
int LImageWritePGM(LImage *img, char *PGMFileName)
{
FILE *fp;
int i;
Image *temp = ImageAlloc(img->XSize, img->YSize);
if (temp!=NULL){
LImageToImage(temp, img);
i = ImageWritePGM(temp, PGMFileName);
ImageDealloc(temp);
return i;
}
else {
return 0;
}
}
//--------------------------------------------------------------------------------------------
int FImageWritePGM(FImage *img, char *PGMFileName)
{
FILE *fp;
int i;
Image *temp = ImageAlloc(img->XSize, img->YSize);
if (temp!=NULL){
FImageToImage(temp, img);
i = ImageWritePGM(temp, PGMFileName);
ImageDealloc(temp);
return i;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------------------
// Copy image
//--------------------------------------------------------------------------------------------
void ImageCopy(Image *pDest, Image *pSrc)
{
assert(pDest); assert(pSrc);
memcpy(pDest->Pixel, pSrc->Pixel, sizeof(unsigned char)*pSrc->XSize*pSrc->YSize);
}
//--------------------------------------------------------------------------------------------
void LImageCopy(LImage *pDest, LImage *pSrc)
{
assert(pDest); assert(pSrc);
memcpy(pDest->Pixel, pSrc->Pixel, sizeof(int)*pSrc->XSize*pSrc->YSize);
}
//--------------------------------------------------------------------------------------------
void FImageCopy(FImage *pDest, FImage *pSrc)
{
assert(pDest); assert(pSrc);
memcpy(pDest->Pixel, pSrc->Pixel, sizeof(REAL)*pSrc->XSize*pSrc->YSize);
}
//--------------------------------------------------------------------------------------------
// Load image data from buffer
//--------------------------------------------------------------------------------------------
void ImageLoad(Image *img, unsigned char *in)
{
assert(img); assert(in);
memcpy(img->Pixel, in, sizeof(unsigned char)*img->XSize*img->YSize);
}
//--------------------------------------------------------------------------------------------
void FImageLoad(FImage *img, REAL *in)
{
assert(img); assert(in);
memcpy(img->Pixel, in, sizeof(REAL)*img->XSize*img->YSize);
}
//--------------------------------------------------------------------------------------------
// Unload image data to buffer
//--------------------------------------------------------------------------------------------
void ImageUnload(Image *img, unsigned char *out)
{
assert(img); assert(out);
memcpy(out, img->Pixel, sizeof(unsigned char)*img->XSize*img->YSize);
}
//--------------------------------------------------------------------------------------------
void FImageUnload(FImage *img, REAL *out)
{
assert(img); assert(out);
memcpy(out, img->Pixel, sizeof(REAL)*img->XSize*img->YSize);
}
//--------------------------------------------------------------------------------------------
// Convert image to 8 bit raw image
//--------------------------------------------------------------------------------------------
void FImageToImage(Image *pDest, FImage *pSrc)
{
int i;
REAL temp;
assert(pDest); assert(pSrc);
for (i=0; i<pDest->XSize*pDest->YSize; i++){
temp = (REAL)(pSrc->Pixel[i]+0.499);
pDest->Pixel[i] = (unsigned char)(temp<0.0 ? 0 : (temp>255.0? 255 : temp));
}
}
//--------------------------------------------------------------------------------------------
void LImageToImage(Image *pDest, LImage *pSrc)
{
int i;
assert(pDest); assert(pSrc);
for (i=0; i<pDest->XSize*pDest->YSize; i++){
pDest->Pixel[i]
= (unsigned char)(pSrc->Pixel[i]<0? 0: (pSrc->Pixel[i]>255 ? 255: pSrc->Pixel[i]));
}
}
//--------------------------------------------------------------------------------------------
// Convert 8 bit raw image to image
//--------------------------------------------------------------------------------------------
void ImageToFImage(FImage *pDest, Image *pSrc)
{
int i;
assert(pDest); assert(pSrc);
for (i=0; i<pDest->XSize*pDest->YSize; i++){
pDest->Pixel[i] = (REAL) pSrc->Pixel[i];
}
}
//--------------------------------------------------------------------------------------------
void ImageToLImage(LImage *pDest, Image *pSrc)
{
int i;
assert(pDest); assert(pSrc);
for (i=0; i<pDest->XSize*pDest->YSize; i++){
pDest->Pixel[i] = (int) pSrc->Pixel[i];
}
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
// Allocate image buffers
//--------------------------------------------------------------------------------------------
CoImage *CoImageAlloc(int XSize[3], int YSize[3], int color)
{
int c;
CoImage *img = (CoImage *)malloc(sizeof(CoImage));
if (img!=NULL){
img->color = color;
if (color==1 || color==3){
for (c=0; c<3; c++){
img->XSize[c] = XSize[c]; img->YSize[c] = YSize[c];
img->C[c] = NULL; img->Pixel[c] = NULL;
}
for (c=0; c<color; c++){
img->C[c] = ImageAlloc(XSize[c], YSize[c]);
img->Pixel[c] = img->C[c]->Pixel;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -