31#define IRIS_MAGIC 0732
53#define HEADER_SIZE 512
61#define ILUM(r, g, b) (int(RINTLUM * (r) + GINTLUM * (g) + BINTLUM * (b)) >> 8)
68#define CHANOFFSET(z) (3 - (z))
73#define ITYPE_RLE 0x0100
74#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE)
76#define BPP(type) ((type) & BPPMASK)
77#define RLE(bpp) (ITYPE_RLE | (bpp))
88#define MFILE_DATA(inf) ((void)0, ((inf)->_file_data + (inf)->_file_offset))
89#define MFILE_STEP(inf, step) \
91 (inf)->_file_offset += step; \
94#define MFILE_SEEK(inf, pos) \
96 (inf)->_file_offset = pos; \
101#define DIRTY_FLAG_EOF (1 << 0)
102#define DIRTY_FLAG_ENCODING (1 << 1)
118 float *optr,
const float *optr_end,
const uchar *iptr,
const uchar *iptr_end,
int z);
177 return (
uint(buf[0]) << 24) + (
uint(buf[1]) << 16) + (
uint(buf[2]) << 8) + (
uint(buf[3]) << 0);
186 fwrite(buf, 2, 1, outf);
193 buf[0] = (val >> 24);
194 buf[1] = (val >> 16);
197 return fwrite(buf, 4, 1, outf);
216 fseek(outf, 0, SEEK_SET);
226 return fwrite(
"no name", 8, 1, outf);
249#define GS(x) (((uchar *)(x))[0] << 8 | ((uchar *)(x))[1])
261 uint *base, *lptr =
nullptr;
262 float *fbase, *fptr =
nullptr;
265 MFileOffset _inf_data = {mem, 0}, *inf = &_inf_data;
267 int bpp, rle, cur, badorder;
268 ImBuf *ibuf =
nullptr;
269 uchar dirty_flag = 0;
287 if (!
ELEM(bpp, 1, 2)) {
288 fprintf(stderr,
"%s: image must have 1 or 2 byte per pix chan\n", __func__);
292 fprintf(stderr,
"%s: channels over 8 not supported\n", __func__);
296 fprintf(stderr,
"%s: zero size image found\n", __func__);
300 const int xsize = image.
xsize;
301 const int ysize = image.
ysize;
303 const int zsize_file = image.
zsize;
315 size_t tablen = size_t(ysize) * size_t(zsize_file) *
sizeof(int);
321#define MFILE_CAPACITY_AT_PTR_OK_OR_FAIL(p) \
322 if (UNLIKELY((p) > mem_end)) { \
323 dirty_flag |= DIRTY_FLAG_EOF; \
330 readtab(inf, starttab, tablen);
331 readtab(inf, lengthtab, tablen);
336 for (
size_t y = 0;
y < ysize;
y++) {
337 for (
size_t z = 0;
z < zsize_file;
z++) {
338 if (starttab[
y +
z * ysize] < cur) {
342 cur = starttab[
y +
z * ysize];
359 for (
size_t z = 0;
z < zsize_read;
z++) {
361 for (
size_t y = 0;
y < ysize;
y++) {
366 uint *lptr_next = lptr + xsize;
375 for (
size_t y = 0;
y < ysize;
y++) {
377 uint *lptr_next = lptr + xsize;
379 for (
size_t z = 0;
z < zsize_read;
z++) {
387 (
uchar *)lptr, (
uchar *)lptr_next, rledat, rledat_next, 3 -
z);
407 for (
size_t z = 0;
z < zsize_read;
z++) {
409 for (
size_t y = 0;
y < ysize;
y++) {
415 float *fptr_next = fptr + (xsize * 4);
416 dirty_flag |=
expandrow2(fptr, fptr_next, rledat, rledat_next, 3 -
z);
423 float *fptr_next = fptr + (xsize * 4);
425 for (
size_t y = 0;
y < ysize;
y++) {
427 for (
size_t z = 0;
z < zsize_read;
z++) {
433 dirty_flag |=
expandrow2(fptr, fptr_next, rledat, rledat_next, 3 -
z);
439#undef MFILE_CAPACITY_AT_PTR_OK_OR_FAIL
450#define MFILE_CAPACITY_AT_PTR_OK_OR_FAIL(p) \
451 if (UNLIKELY((p) > mem_end)) { \
452 dirty_flag |= DIRTY_FLAG_EOF; \
453 goto fail_uncompressed; \
461 goto fail_uncompressed;
470 for (
size_t z = 0;
z < zsize_read;
z++) {
479 for (
size_t y = 0;
y < ysize;
y++) {
480 const uchar *rledat_next = rledat + xsize;
481 const int z_ofs = 3 -
z;
484 rledat = rledat_next;
493 goto fail_uncompressed;
501 for (
size_t z = 0;
z < zsize_read;
z++) {
505 for (
size_t y = 0;
y < ysize;
y++) {
506 const uchar *rledat_next = rledat + xsize * 2;
507 const int z_ofs = 3 -
z;
510 rledat = rledat_next;
515#undef MFILE_CAPACITY_AT_PTR_OK_OR_FAIL
525 if (image.
zsize == 1) {
527 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
529 rect[1] = rect[2] = rect[3];
533 else if (image.
zsize == 2) {
536 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
538 rect[1] = rect[2] = rect[3];
542 else if (image.
zsize == 3) {
545 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
553 if (image.
zsize == 1) {
555 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
557 fbase[1] = fbase[2] = fbase[3];
561 else if (image.
zsize == 2) {
564 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
566 fbase[1] = fbase[2] = fbase[3];
570 else if (image.
zsize == 3) {
573 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
585 fprintf(stderr,
"%s: corrupt file content (%d)\n", __func__, dirty_flag);
611 *lptr = ((cptr[0] << 8) | (cptr[1] << 0)) /
float(0xFFFF);
618 float *optr,
const float *optr_end,
const uchar *iptr,
const uchar *iptr_end,
int z)
623#define EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL(iptr_next) \
624 if (UNLIKELY(iptr_next > iptr_end)) { \
629#define EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL(optr_next) \
630 if (UNLIKELY(optr_next > optr_end)) { \
638 const uchar *iptr_next = iptr + 2;
640 pixel = (iptr[0] << 8) | (iptr[1] << 0);
643 if (!(
count = (pixel & 0x7f))) {
646 const float *optr_next = optr +
count;
649 iptr_next = iptr + (
count * 2);
652 optr[0 * 4] = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
653 optr[1 * 4] = ((iptr[2] << 8) | (iptr[3] << 0)) /
float(0xFFFF);
654 optr[2 * 4] = ((iptr[4] << 8) | (iptr[5] << 0)) /
float(0xFFFF);
655 optr[3 * 4] = ((iptr[6] << 8) | (iptr[7] << 0)) /
float(0xFFFF);
656 optr[4 * 4] = ((iptr[8] << 8) | (iptr[9] << 0)) /
float(0xFFFF);
657 optr[5 * 4] = ((iptr[10] << 8) | (iptr[11] << 0)) /
float(0xFFFF);
658 optr[6 * 4] = ((iptr[12] << 8) | (iptr[13] << 0)) /
float(0xFFFF);
659 optr[7 * 4] = ((iptr[14] << 8) | (iptr[15] << 0)) /
float(0xFFFF);
665 *optr = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
672 iptr_next = iptr + 2;
674 pixel_f = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
678 optr[0 * 4] = pixel_f;
679 optr[1 * 4] = pixel_f;
680 optr[2 * 4] = pixel_f;
681 optr[3 * 4] = pixel_f;
682 optr[4 * 4] = pixel_f;
683 optr[5 * 4] = pixel_f;
684 optr[6 * 4] = pixel_f;
685 optr[7 * 4] = pixel_f;
699#undef EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL
700#undef EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL
710#define EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL(iptr_next) \
711 if (UNLIKELY(iptr_next > iptr_end)) { \
716#define EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL(optr_next) \
717 if (UNLIKELY(optr_next > optr_end)) { \
725 const uchar *iptr_next = iptr + 1;
729 if (!(
count = (pixel & 0x7f))) {
732 const uchar *optr_next = optr + (int(
count) * 4);
736 iptr_next = iptr +
count;
739 optr[0 * 4] = iptr[0];
740 optr[1 * 4] = iptr[1];
741 optr[2 * 4] = iptr[2];
742 optr[3 * 4] = iptr[3];
743 optr[4 * 4] = iptr[4];
744 optr[5 * 4] = iptr[5];
745 optr[6 * 4] = iptr[6];
746 optr[7 * 4] = iptr[7];
758 iptr_next = iptr + 1;
784#undef EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL
785#undef EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL
813 uint *starttab, *lengthtab;
816 int rlebuflen, goodwrite;
824 tablen = ysize * zsize *
sizeof(int);
829 rlebuflen = 1.05 * xsize + 10;
842 image->
xsize = xsize;
843 image->
ysize = ysize;
844 image->
zsize = zsize;
851 for (
y = 0;
y < ysize;
y++) {
852 for (
z = 0;
z < zsize;
z++) {
862 else if (
z < 8 && zptr) {
867 BLI_assert_msg(
len <= rlebuflen,
"The length calculated for 'rlebuflen' was too small!");
869 goodwrite *= fwrite(rlebuf,
len, 1, outf);
870 starttab[
y +
z * ysize] =
pos;
871 lengthtab[
y +
z * ysize] =
len;
881 goodwrite *=
writetab(outf, starttab, tablen);
882 goodwrite *=
writetab(outf, lengthtab, tablen);
893 fprintf(stderr,
"%s: not enough space for image!!\n", __func__);
911 const uchar *iptr, *ibufend, *sptr;
918 ibufend = iptr + row_len * 4;
921 while (iptr < ibufend) {
924 while ((iptr < ibufend) && ((iptr[-8] != iptr[-4]) || (iptr[-4] != iptr[0]))) {
928 count = (iptr - sptr) / 4;
932 *optr++ = 0x80 | todo;
934 optr[0] = sptr[0 * 4];
935 optr[1] = sptr[1 * 4];
936 optr[2] = sptr[2 * 4];
937 optr[3] = sptr[3 * 4];
938 optr[4] = sptr[4 * 4];
939 optr[5] = sptr[5 * 4];
940 optr[6] = sptr[6 * 4];
941 optr[7] = sptr[7 * 4];
955 while ((iptr < ibufend) && (*iptr == cc)) {
958 count = (iptr - sptr) / 4;
967 return optr - rlebuf;
972 const uint limit = std::numeric_limits<ushort>::max();
973 if (ibuf->
x > limit || ibuf->
y > limit) {
974 fprintf(stderr,
"%s: image x/y exceeds %u\n", __func__, limit);
978 const short zsize = (ibuf->
planes + 7) >> 3;
#define BLI_STATIC_ASSERT(a, msg)
#define BLI_assert_msg(a, msg)
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE int min_ii(int a, int b)
void IMB_byte_from_float(ImBuf *ibuf)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
void * MEM_mallocN(size_t len, const char *str)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer