📄 img.c
字号:
}
return img;
}
else{
ImageWarning("color is 1 or 3 only.\b");
free(img);
return NULL;
}
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
LCoImage *LCoImageAlloc(int XSize[3], int YSize[3], int color)
{
int c;
LCoImage *img = (LCoImage *)malloc(sizeof(LCoImage));
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] = LImageAlloc(XSize[c], YSize[c]);
img->Pixel[c] = img->C[c]->Pixel;
}
return img;
}
else{
ImageWarning("color is 1 or 3 only.\b");
free(img);
return NULL;
}
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
FCoImage *FCoImageAlloc(int XSize[3], int YSize[3], int color)
{
int c;
FCoImage *img = (FCoImage *)malloc(sizeof(FCoImage));
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] = FImageAlloc(XSize[c], YSize[c]);
img->Pixel[c] = img->C[c]->Pixel;
}
return img;
}
else{
ImageWarning("color is 1 or 3 only.\b");
free(img);
return NULL;
}
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
// Deallocate image buffers
//--------------------------------------------------------------------------------------------
void CoImageDealloc(CoImage *img)
{
int c;
if (img){
for (c=0; c<img->color; c++){
ImageDealloc(img->C[c]);
}
free(img);
}
}
//--------------------------------------------------------------------------------------------
void LCoImageDealloc(LCoImage *img)
{
int c;
if (img){
for (c=0; c<img->color; c++){
LImageDealloc(img->C[c]);
}
free(img);
}
}
//--------------------------------------------------------------------------------------------
void FCoImageDealloc(FCoImage *img)
{
int c;
if (img){
for (c=0; c<img->color; c++){
FImageDealloc(img->C[c]);
}
free(img);
}
}
//--------------------------------------------------------------------------------------------
// Read image components
// for interleave format, components have same size
CoImage *CoImageReadRaw(int XSize[3], int YSize[3], int color, int interleave, char *filename)
{
CoImage *img;
FILE *fp;
int i, c, v;
if ((fp=fopen(filename, "rb"))==NULL){
ImageWarning("Fail to open file [%s].\n", filename);
return NULL;
}
if (color == 1){
XSize[1] = XSize[2] = 0;
YSize[1] = YSize[2] = 0;
img = CoImageAlloc(XSize, YSize, 1);
if ((int)fread(img->Pixel[0], sizeof(unsigned char),
img->XSize[0]*img->YSize[0], fp) != img->XSize[0]*img->YSize[0]){
ImageWarning("Fail to read raw image data.\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
}
else {
if (interleave){
for (c=1; c<3; c++){
if (XSize[0] != XSize[c] || YSize[0] != YSize[c]){
ImageError("Interleaved format must have same size components.\n");
}
}
img = CoImageAlloc(XSize, YSize, 3);
for (i=0; i<img->XSize[0]*img->YSize[0]; i++){
for (c=0; c<3; c++){
if ((v = fgetc(fp)) == EOF){
ImageWarning("Fail to read raw image data\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
img->Pixel[c][i] = v;
}
}
}
else{ // planar format
img = CoImageAlloc(XSize, YSize, 3);
for (c=0; c<3; c++){
if ( (int)fread(img->Pixel[c], sizeof(unsigned char),
img->XSize[c]*img->YSize[c], fp) != img->XSize[c]*img->YSize[c] ){
ImageWarning("Fail to read raw image data\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
}
}
}
fclose(fp);
return img;
}
//--------------------------------------------------------------------------------------------
LCoImage *LCoImageReadRaw(int XSize[3], int YSize[3], int color, int interleave,
char *filename)
{
LCoImage *img;
CoImage *temp = CoImageReadRaw(XSize, YSize, color, interleave, filename);
if (temp!=NULL){
img = LCoImageAlloc(XSize, YSize, color);
CoImageToLCoImage(img, temp);
CoImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
FCoImage *FCoImageReadRaw(int XSize[3], int YSize[3], int color, int interleave,
char *filename)
{
FCoImage *img;
CoImage *temp = CoImageReadRaw(XSize, YSize, color, interleave, filename);
if (temp!=NULL){
img = FCoImageAlloc(XSize, YSize, color);
CoImageToFCoImage(img, temp);
CoImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
// Read image components, skip header bytes
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
// Read image components
// for interleave format, components have same size
CoImage *CoImageReadRawWithSkip(int XSize[3], int YSize[3], int color, int interleave,
int skip_count, char *filename)
{
CoImage *img;
FILE *fp;
int i, c, v;
if ((fp=fopen(filename, "rb"))==NULL){
ImageWarning("Fail to open file [%s].\n", filename);
return NULL;
}
// skip the header info if specified
if (fseek(fp, skip_count, SEEK_SET)){
ImageWarning("Fail to read raw image data.\n");
fclose(fp);
return NULL;
}
if (color == 1){
XSize[1] = XSize[2] = 0;
YSize[1] = YSize[2] = 0;
img = CoImageAlloc(XSize, YSize, 1);
if ((int)fread(img->Pixel[0], sizeof(unsigned char),
img->XSize[0]*img->YSize[0], fp) != img->XSize[0]*img->YSize[0]){
ImageWarning("Fail to read raw image data.\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
}
else {
if (interleave){
for (c=1; c<3; c++){
if (XSize[0] != XSize[c] || YSize[0] != YSize[c]){
ImageError("Interleaved format must have same size components.\n");
}
}
img = CoImageAlloc(XSize, YSize, 3);
for (i=0; i<img->XSize[0]*img->YSize[0]; i++){
for (c=0; c<3; c++){
if ((v = fgetc(fp)) == EOF){
ImageWarning("Fail to read raw image data\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
img->Pixel[c][i] = v;
}
}
}
else{ // planar format
img = CoImageAlloc(XSize, YSize, 3);
for (c=0; c<3; c++){
if ( (int)fread(img->Pixel[c], sizeof(unsigned char),
img->XSize[c]*img->YSize[c], fp) != img->XSize[c]*img->YSize[c] ){
ImageWarning("Fail to read raw image data\n");
fclose(fp);
CoImageDealloc(img);
return NULL;
}
}
}
}
fclose(fp);
return img;
}
//--------------------------------------------------------------------------------------------
LCoImage *LCoImageReadRawWithSkip(int XSize[3], int YSize[3], int color, int interleave,
int skip_count, char *filename)
{
LCoImage *img;
CoImage *temp = CoImageReadRawWithSkip(XSize, YSize, color, interleave, skip_count, filename);
if (temp!=NULL){
img = LCoImageAlloc(XSize, YSize, color);
CoImageToLCoImage(img, temp);
CoImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
FCoImage *FCoImageReadRawWithSkip(int XSize[3], int YSize[3], int color, int interleave,
int skip_count, char *filename)
{
FCoImage *img;
CoImage *temp = CoImageReadRawWithSkip(XSize, YSize, color, interleave, skip_count, filename);
if (temp!=NULL){
img = FCoImageAlloc(XSize, YSize, color);
CoImageToFCoImage(img, temp);
CoImageDealloc(temp);
return img;
}
else{
return NULL;
}
}
//--------------------------------------------------------------------------------------------
// Write image components
//--------------------------------------------------------------------------------------------
int CoImageWriteRaw(CoImage *img, int interleave, char *filename)
{
FILE *fp;
int i, c;
if ((fp = fopen(filename,"wb")) == NULL){
ImageWarning("Fail to create file [%s]\n", filename);
return -1;
}
if (img->color == 1){
if ((int)fwrite(img->Pixel[0], sizeof(unsigned char),
img->XSize[0]*img->YSize[0], fp) != img->XSize[0]*img->YSize[0]){
ImageWarning("Fail to write raw image data\n");
fclose(fp);
return -1;
}
}
else{
if (interleave){
for (c=1; c<3; c++){
if (img->XSize[0] != img->XSize[c] || img->YSize[0] != img->YSize[c]){
ImageWarning("Interleaved format must have same size components.\n");
fclose(fp);
return -1;
}
}
for (i=0; i<img->XSize[0]*img->YSize[0]; i++){
for (c=0; c<3; c++){
if (fputc((int)img->Pixel[c][i], fp) == EOF){
ImageWarning("Fail to write raw image data\n");
fclose(fp);
return -1;
}
}
}
}
else{
for (c=0; c<3; c++){
if ((int)fwrite(img->Pixel[c], sizeof(unsigned char),
img->XSize[c]*img->YSize[c], fp) != img->XSize[c]*img->YSize[c]){
ImageWarning("Fail to write raw image data\n");
fclose(fp);
return -1;
}
}
}
}
fclose(fp);
return 0;
}
//--------------------------------------------------------------------------------------------
int LCoImageWriteRaw(LCoImage *img, int interleave, char *filename)
{
int i;
CoImage *temp = CoImageAlloc(img->XSize, img->YSize, img->color);
if (temp!=NULL){
LCoImageToCoImage(temp, img);
i = CoImageWriteRaw(temp, interleave, filename);
CoImageDealloc(temp);
return i;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------------------
int FCoImageWriteRaw(FCoImage *img, int interleave, char *filename)
{
int i;
CoImage *temp = CoImageAlloc(img->XSize, img->YSize, img->color);
if (temp!=NULL){
FCoImageToCoImage(temp, img);
i = CoImageWriteRaw(temp, interleave, filename);
CoImageDealloc(temp);
return i;
}
else{
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -