This commit is contained in:
Kim Woelders 2016-02-07 08:08:00 +01:00
parent 5cabf38155
commit 93e6176764
1 changed files with 149 additions and 130 deletions

View File

@ -7,156 +7,175 @@
#define LEN(x) (sizeof((x)) / sizeof(*(x))) #define LEN(x) (sizeof((x)) / sizeof(*(x)))
char char
load(ImlibImage *im, ImlibProgressFunction progress, load(ImlibImage * im, ImlibProgressFunction progress,
char progress_granularity, char immediate_load) char progress_granularity, char immediate_load)
{ {
FILE *f; FILE *f;
size_t rowlen, i, j; size_t rowlen, i, j;
uint32_t hdr[2 + 1 + 1], w, h; uint32_t hdr[2 + 1 + 1], w, h;
uint16_t *row; uint16_t *row;
uint8_t *dat; uint8_t *dat;
/* open the file for reading */ /* open the file for reading */
if (!(f = fopen(im->real_file, "rb"))) { if (!(f = fopen(im->real_file, "rb")))
return 0; {
} return 0;
}
/* read and check the header */ /* read and check the header */
if (!im->data) { if (!im->data)
if (fread(hdr, sizeof(uint32_t), LEN(hdr), f) != LEN(hdr) || {
memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { if (fread(hdr, sizeof(uint32_t), LEN(hdr), f) != LEN(hdr) ||
fclose(f); memcmp("farbfeld", hdr, sizeof("farbfeld") - 1))
return 0; {
} fclose(f);
im->w = ntohl(hdr[2]); return 0;
im->h = ntohl(hdr[3]); }
if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) { im->w = ntohl(hdr[2]);
fclose(f); im->h = ntohl(hdr[3]);
return 0; if (!IMAGE_DIMENSIONS_OK(im->w, im->h))
} {
fclose(f);
return 0;
}
/* set format */ /* set format */
if (!im->loader) { if (!im->loader)
if (!(im->format = strdup("ff"))) { {
fclose(f); if (!(im->format = strdup("ff")))
return 0; {
} fclose(f);
} return 0;
SET_FLAG(im->flags, F_HAS_ALPHA); }
} }
SET_FLAG(im->flags, F_HAS_ALPHA);
}
/* load the data */ /* load the data */
if (im->loader || immediate_load || progress) { if (im->loader || immediate_load || progress)
w = im->w; {
h = im->h; w = im->w;
rowlen = w * (sizeof("RGBA") - 1); h = im->h;
rowlen = w * (sizeof("RGBA") - 1);
free(im->data); free(im->data);
if (!(im->data = malloc(rowlen * h)) || if (!(im->data = malloc(rowlen * h)) ||
!(row = malloc(rowlen * sizeof(uint16_t)))) { !(row = malloc(rowlen * sizeof(uint16_t))))
free(im->data); {
im->data = NULL; free(im->data);
free(row); im->data = NULL;
fclose(f); free(row);
return 0; fclose(f);
} return 0;
}
dat = (uint8_t *)im->data; dat = (uint8_t *) im->data;
for (i = 0; i < h; i++, dat += rowlen) { for (i = 0; i < h; i++, dat += rowlen)
if (fread(row, sizeof(uint16_t), rowlen, f) != {
rowlen) { if (fread(row, sizeof(uint16_t), rowlen, f) != rowlen)
free(im->data); {
im->data = NULL; free(im->data);
free(row); im->data = NULL;
fclose(f); free(row);
return 0; fclose(f);
} return 0;
for (j = 0; j < rowlen; j += 4) { }
/* for (j = 0; j < rowlen; j += 4)
* 16-Bit to 8-Bit (RGBA -> BGRA) {
* 255 * 257 = 65535 = 2^16-1 = UINT16_MAX /*
*/ * 16-Bit to 8-Bit (RGBA -> BGRA)
dat[j + 2] = ntohs(row[j + 0]) / 257; * 255 * 257 = 65535 = 2^16-1 = UINT16_MAX
dat[j + 1] = ntohs(row[j + 1]) / 257; */
dat[j + 0] = ntohs(row[j + 2]) / 257; dat[j + 2] = ntohs(row[j + 0]) / 257;
dat[j + 3] = ntohs(row[j + 3]) / 257; dat[j + 1] = ntohs(row[j + 1]) / 257;
} dat[j + 0] = ntohs(row[j + 2]) / 257;
} dat[j + 3] = ntohs(row[j + 3]) / 257;
if (progress) { }
progress(im, 100, 0, 0, im->w, im->h); }
} if (progress)
{
progress(im, 100, 0, 0, im->w, im->h);
}
free(row); free(row);
} }
fclose(f); fclose(f);
return 1; return 1;
} }
char char
save(ImlibImage *im, ImlibProgressFunction progress, char progress_gran) save(ImlibImage * im, ImlibProgressFunction progress, char progress_gran)
{ {
FILE *f; FILE *f;
size_t rowlen, i, j; size_t rowlen, i, j;
uint32_t tmp32; uint32_t tmp32;
uint16_t *row; uint16_t *row;
uint8_t *dat; uint8_t *dat;
/* open the file for writing */ /* open the file for writing */
if (!(f = fopen(im->real_file, "wb"))) { if (!(f = fopen(im->real_file, "wb")))
return 0; {
} return 0;
}
/* write header */ /* write header */
fputs("farbfeld", f); fputs("farbfeld", f);
tmp32 = htonl(im->w); tmp32 = htonl(im->w);
if (fwrite(&tmp32, sizeof(uint32_t), 1, f) != 1) { if (fwrite(&tmp32, sizeof(uint32_t), 1, f) != 1)
fclose(f); {
return 0; fclose(f);
} return 0;
tmp32 = htonl(im->h); }
if (fwrite(&tmp32, sizeof(uint32_t), 1, f) != 1) { tmp32 = htonl(im->h);
fclose(f); if (fwrite(&tmp32, sizeof(uint32_t), 1, f) != 1)
return 0; {
} fclose(f);
return 0;
}
/* write data */ /* write data */
rowlen = im->w * (sizeof("RGBA") - 1); rowlen = im->w * (sizeof("RGBA") - 1);
if (!(row = malloc(rowlen * sizeof(uint16_t)))) { if (!(row = malloc(rowlen * sizeof(uint16_t))))
fclose(f); {
return 0; fclose(f);
} return 0;
dat = (uint8_t *)im->data; }
for (i = 0; i < (uint32_t)im->h; ++i, dat += rowlen) { dat = (uint8_t *) im->data;
for (j = 0; j < rowlen; j += 4) { for (i = 0; i < (uint32_t) im->h; ++i, dat += rowlen)
/* {
* 8-Bit to 16-Bit for (j = 0; j < rowlen; j += 4)
* 255 * 257 = 65535 = 2^16-1 = UINT16_MAX {
*/ /*
row[j + 0] = htons(dat[j + 2] * 257); * 8-Bit to 16-Bit
row[j + 1] = htons(dat[j + 1] * 257); * 255 * 257 = 65535 = 2^16-1 = UINT16_MAX
row[j + 2] = htons(dat[j + 0] * 257); */
row[j + 3] = htons(dat[j + 3] * 257); row[j + 0] = htons(dat[j + 2] * 257);
} row[j + 1] = htons(dat[j + 1] * 257);
if (fwrite(row, sizeof(uint16_t), rowlen, f) != rowlen) { row[j + 2] = htons(dat[j + 0] * 257);
free(row); row[j + 3] = htons(dat[j + 3] * 257);
fclose(f); }
return 0; if (fwrite(row, sizeof(uint16_t), rowlen, f) != rowlen)
} {
} free(row);
if (progress) { fclose(f);
progress(im, 100, 0, 0, im->w, im->h); return 0;
} }
}
if (progress)
{
progress(im, 100, 0, 0, im->w, im->h);
}
free(row); free(row);
fclose(f); fclose(f);
return 1; return 1;
} }
void void
formats(ImlibLoader *l) formats(ImlibLoader * l)
{ {
l->num_formats = 1; l->num_formats = 1;
l->formats = malloc(sizeof(char *)); l->formats = malloc(sizeof(char *));
*(l->formats) = strdup("ff"); *(l->formats) = strdup("ff");
} }