mirror of
https://github.com/gryf/wmaker.git
synced 2026-01-25 16:55:47 +01:00
ppm.c code style
This patch removes some extra curly brackets, some empty lines, extra spaces,... This source file had some empty if blocks. Now are removed. This patch also changes some preprocessor macros for C code.
This commit is contained in:
committed by
Carlos R. Mafra
parent
cd5e004127
commit
2bdeb693f6
130
wrlib/ppm.c
130
wrlib/ppm.c
@@ -30,53 +30,46 @@
|
|||||||
#include "wraster.h"
|
#include "wraster.h"
|
||||||
#include "imgformat.h"
|
#include "imgformat.h"
|
||||||
|
|
||||||
static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
|
static RImage *load_graymap(FILE *file, int w, int h, int max, int raw)
|
||||||
{
|
{
|
||||||
RImage *image;
|
RImage *image;
|
||||||
|
unsigned char *ptr;
|
||||||
|
char *buf;
|
||||||
|
int x, y;
|
||||||
|
|
||||||
image = RCreateImage(w, h, 0);
|
image = RCreateImage(w, h, 0);
|
||||||
if (!image) {
|
if (!image)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
if (!raw) {
|
|
||||||
|
|
||||||
} else {
|
if (!raw)
|
||||||
if (max < 256) {
|
return image;
|
||||||
unsigned char *ptr;
|
|
||||||
char *buf;
|
|
||||||
int x, y;
|
|
||||||
|
|
||||||
buf = malloc(w + 1);
|
if (max < 256) {
|
||||||
if (!buf) {
|
buf = malloc(w + 1);
|
||||||
|
if (!buf)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
ptr = image->data;
|
||||||
|
for (y = 0; y < h; y++) {
|
||||||
|
if (!fread(buf, w, 1, file)) {
|
||||||
|
free(buf);
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr = image->data;
|
for (x = 0; x < w; x++) {
|
||||||
for (y = 0; y < h; y++) {
|
*(ptr++) = buf[x];
|
||||||
if (!fread(buf, w, 1, file)) {
|
*(ptr++) = buf[x];
|
||||||
free(buf);
|
*(ptr++) = buf[x];
|
||||||
goto short_file;
|
|
||||||
}
|
|
||||||
for (x = 0; x < w; x++) {
|
|
||||||
*(ptr++) = buf[x];
|
|
||||||
*(ptr++) = buf[x];
|
|
||||||
*(ptr++) = buf[x];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
free(buf);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
short_file:
|
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
|
static RImage *load_pixmap(FILE *file, int w, int h, int max, int raw)
|
||||||
{
|
{
|
||||||
RImage *image;
|
RImage *image;
|
||||||
int i;
|
int i;
|
||||||
@@ -84,33 +77,29 @@ static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
|
|||||||
unsigned char *ptr;
|
unsigned char *ptr;
|
||||||
|
|
||||||
image = RCreateImage(w, h, 0);
|
image = RCreateImage(w, h, 0);
|
||||||
if (!image) {
|
if (!image)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
if (!raw)
|
||||||
|
return image;
|
||||||
|
|
||||||
ptr = image->data;
|
ptr = image->data;
|
||||||
if (!raw) {
|
if (max < 256) {
|
||||||
|
i = 0;
|
||||||
} else {
|
while (i < w * h) {
|
||||||
if (max < 256) {
|
if (fread(buf, 1, 3, file) != 3) {
|
||||||
i = 0;
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
while (i < w * h) {
|
return NULL;
|
||||||
if (fread(buf, 1, 3, file) != 3)
|
|
||||||
goto short_file;
|
|
||||||
*(ptr++) = buf[0];
|
|
||||||
*(ptr++) = buf[1];
|
|
||||||
*(ptr++) = buf[2];
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
|
||||||
|
*(ptr++) = buf[0];
|
||||||
|
*(ptr++) = buf[1];
|
||||||
|
*(ptr++) = buf[2];
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
short_file:
|
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RImage *RLoadPPM(const char *file_name)
|
RImage *RLoadPPM(const char *file_name)
|
||||||
@@ -121,8 +110,6 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
int w, h, m;
|
int w, h, m;
|
||||||
int type;
|
int type;
|
||||||
|
|
||||||
#define GETL() if (!fgets(buffer, 255, file)) goto short_file
|
|
||||||
|
|
||||||
file = fopen(file_name, "rb");
|
file = fopen(file_name, "rb");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
RErrorCode = RERR_OPEN;
|
RErrorCode = RERR_OPEN;
|
||||||
@@ -130,7 +117,11 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get signature */
|
/* get signature */
|
||||||
GETL();
|
if (!fgets(buffer, 255, file)) {
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* only accept raw pixmaps or graymaps */
|
/* only accept raw pixmaps or graymaps */
|
||||||
if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
|
if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
|
||||||
@@ -143,19 +134,36 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
|
|
||||||
/* skip comments */
|
/* skip comments */
|
||||||
while (1) {
|
while (1) {
|
||||||
GETL();
|
if (!fgets(buffer, 255, file)) {
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (buffer[0] != '#')
|
if (buffer[0] != '#')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get size */
|
/* get size */
|
||||||
if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1)
|
if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1) {
|
||||||
goto bad_file;
|
/* Short file */
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
GETL();
|
if (!fgets(buffer, 255, file)) {
|
||||||
if (sscanf(buffer, "%i", &m) != 1 || m < 1)
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
goto bad_file;
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sscanf(buffer, "%i", &m) != 1 || m < 1) {
|
||||||
|
/* Short file */
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == '5')
|
if (type == '5')
|
||||||
image = load_graymap(file, w, h, m, type == '5');
|
image = load_graymap(file, w, h, m, type == '5');
|
||||||
@@ -164,10 +172,4 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
bad_file:
|
|
||||||
short_file:
|
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user