home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
REND.LZH
/
PIC1600
/
F_RGB8.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-12
|
5KB
|
215 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "piclib.h"
typedef enum {TYPE_RGB, TYPE_PPM} PicType;
typedef struct {
FILE *fp;
unsigned char buffer[BUFFERSIZE];
unsigned int nowpoint;
PicType type;
} PicWork;
int PicReadHeader_RGB888(PicData *pd, FILE *fp, unsigned char *magic)
{
int i;
FILE *hfp;
char buf[512];
char fname[512];
int x,y,px,py;
extern PicFunction PicFunction_RGB888;
i = strlen(pd->filename);
/* if (i < 4 || strcmpi(pd->filename+i-4, ".rgb") != 0) {*/
if (i < 4 || pd->filename[i-4] != '.') {
return FALSE;
}
if (tolower(pd->filename[i-3]) == 'r'
&& tolower(pd->filename[i-2]) == 'g'
&& tolower(pd->filename[i-1]) == 'b') {
strcpy(fname, pd->filename);
i = strlen(fname);
strcpy(fname+i-3, "IPR");
if ((hfp = fopen(fname, "r")) == NULL) {
return FALSE;
}
if (fgets(buf, 512, hfp) == NULL) {
fclose(hfp);
return FALSE;
}
if (sscanf(buf, "%d %d %d %d", &x,&y,&px,&py) != 4) {
fclose(hfp);
return FALSE;
}
if (x <= 0 || y <= 0) {
fclose(hfp);
return FALSE;
}
} else if (tolower(pd->filename[i-3]) == 'p'
&& tolower(pd->filename[i-2]) == 'p'
&& tolower(pd->filename[i-1]) == 'm'
&& magic[0] == 'P' && magic[1] == '6') {
x = y = 0;
} else {
return FALSE;
}
pd->func = &PicFunction_RGB888;
pd->mode = NON;
pd->pixelX = (unsigned short)x;
pd->pixelY = (unsigned short)y;
return TRUE;
}
static inline Pixel Convert_RGB888_32(unsigned char r, unsigned char g, unsigned char b)
{
return ((unsigned long)r << 16L) | ((unsigned long)g << 24L) | ((unsigned long)b << 8L);
}
static void SkipSpace(PicWork *work)
{
int i;
i = work->nowpoint;
while (i < BUFFERSIZE) {
if (isspace(work->buffer[i])) {
i++;
} else if (work->buffer[i] == '#') {
while (i < BUFFERSIZE && work->buffer[i] != '\n') {
i++;
}
} else {
break;
}
}
work->nowpoint = i;
}
int PicOpen_RGB888(PicData *pd, FILE *fp, int flag)
{
PicWork *work;
if ((work = malloc(sizeof(PicWork))) == NULL) {
return FALSE;
}
pd->work = work;
work->fp = fp;
if (pd->flag == PIC_READ) {
fread(work->buffer, 1, BUFFERSIZE*sizeof(char), work->fp);
work->nowpoint = 0;
if (pd->pixelX == 0 && pd->pixelY == 0) {
int x, y, d;
work->nowpoint = 2;
SkipSpace(work);
x = atoi(work->buffer+work->nowpoint);
while (isdigit(work->buffer[work->nowpoint])) {
work->nowpoint++;
}
SkipSpace(work);
y = atoi(work->buffer+work->nowpoint);
while (isdigit(work->buffer[work->nowpoint])) {
work->nowpoint++;
}
SkipSpace(work);
d = atoi(work->buffer+work->nowpoint);
while (isdigit(work->buffer[work->nowpoint])) {
work->nowpoint++;
}
work->nowpoint++;
pd->pixelX = (unsigned short)x;
pd->pixelY = (unsigned short)y;
}
} else {
int i;
i = strlen(pd->filename);
if (tolower(pd->filename[i-3]) == 'r'
&& tolower(pd->filename[i-2]) == 'g'
&& tolower(pd->filename[i-1]) == 'b') {
char fname[512];
FILE *hfp;
strcpy(fname, pd->filename);
i = strlen(fname);
strcpy(fname+i-3, "IPR");
if ((hfp = fopen(fname, "w")) == NULL) {
return FALSE;
}
fprintf(hfp, "%d %d 0 0\n", pd->pixelX, pd->pixelY);
fclose(hfp);
} else {
fprintf(fp, "P6\n%ld %ld\n255\n", pd->pixelX, pd->pixelY);
}
pd->mode = flag;
work->nowpoint = 0;
}
return TRUE;
}
int PicOutput_RGB888(PicData *pd, Pixel *pixel)
{
int pixels = pd->pixelX;
PicWork *work = pd->work;
unsigned char *nowp;
if (BUFFERSIZE - work->nowpoint < pixels*3) {
fwrite(work->buffer, 1, work->nowpoint, work->fp);
work->nowpoint = 0;
}
nowp = work->buffer+work->nowpoint;
work->nowpoint += pixels*3;
for (; pixels > 0; --pixels) {
*nowp++ = (*pixel >> 16) & 255;
*nowp++ = (*pixel >> 24) & 255;
*nowp++ = (*pixel >> 8) & 255;
pixel++;
}
return TRUE;
}
int PicInput_RGB888(PicData *pd, Pixel *pixel)
{
int pixels = pd->pixelX;
PicWork *work = pd->work;
unsigned char *nowp;
nowp = work->buffer+work->nowpoint;
work->nowpoint += pixels*3;
for (; pixels > 0; --pixels) {
*pixel++ = Convert_RGB888_32(nowp[0], nowp[1], nowp[2]);
nowp+=3;
}
if (work->nowpoint > BUFFERSIZE - pd->pixelX*3) {
int rest = BUFFERSIZE - work->nowpoint;
memcpy(work->buffer, work->buffer+work->nowpoint, rest);
fread(work->buffer+rest, 1, (work->nowpoint)*sizeof(char), work->fp);
work->nowpoint = 0;
}
return TRUE;
}
int PicClose_RGB888(PicData *pd)
{
PicWork *work = pd->work;
if (pd->flag == PIC_WRITE) {
if (work->nowpoint > 0) {
fwrite(work->buffer, 1, work->nowpoint, work->fp);
}
}
fclose(work->fp);
free(work);
return TRUE;
}
PicFunction PicFunction_RGB888 = {
"RGB|PPM",
PicReadHeader_RGB888,
PicOpen_RGB888,
PicOutput_RGB888,
PicInput_RGB888,
PicClose_RGB888
};