35#define IRIS_MAGIC 0732
57#define HEADER_SIZE 512
65#define ILUM(r, g, b) (int(RINTLUM * (r) + GINTLUM * (g) + BINTLUM * (b)) >> 8)
72#define CHANOFFSET(z) (3 - (z))
77#define ITYPE_RLE 0x0100
78#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE)
80#define BPP(type) ((type) & BPPMASK)
81#define RLE(bpp) (ITYPE_RLE | (bpp))
92#define MFILE_DATA(inf) ((void)0, ((inf)->_file_data + (inf)->_file_offset))
93#define MFILE_STEP(inf, step) \
95 (inf)->_file_offset += step; \
98#define MFILE_SEEK(inf, pos) \
100 (inf)->_file_offset = pos; \
105#define DIRTY_FLAG_EOF (1 << 0)
106#define DIRTY_FLAG_ENCODING (1 << 1)
122 float *optr,
const float *optr_end,
const uchar *iptr,
const uchar *iptr_end,
int z);
181 return (
uint(buf[0]) << 24) + (
uint(buf[1]) << 16) + (
uint(buf[2]) << 8) + (
uint(buf[3]) << 0);
190 fwrite(buf, 2, 1, outf);
197 buf[0] = (val >> 24);
198 buf[1] = (val >> 16);
201 return fwrite(buf, 4, 1, outf);
220 fseek(outf, 0, SEEK_SET);
230 return fwrite(
"no name", 8, 1, outf);
253#define GS(x) (((uchar *)(x))[0] << 8 | ((uchar *)(x))[1])
265 uint *base, *lptr =
nullptr;
266 float *fbase, *fptr =
nullptr;
269 MFileOffset _inf_data = {mem, 0}, *inf = &_inf_data;
271 int bpp, rle, cur, badorder;
272 ImBuf *ibuf =
nullptr;
273 uchar dirty_flag = 0;
291 if (!
ELEM(bpp, 1, 2)) {
292 CLOG_ERROR(&
LOG,
"Image must have 1 or 2 byte per pix chan");
304 const int xsize = image.
xsize;
305 const int ysize = image.
ysize;
307 const int zsize_file = image.
zsize;
319 size_t tablen = size_t(ysize) * size_t(zsize_file) *
sizeof(int);
325#define MFILE_CAPACITY_AT_PTR_OK_OR_FAIL(p) \
326 if (UNLIKELY((p) > mem_end)) { \
327 dirty_flag |= DIRTY_FLAG_EOF; \
334 readtab(inf, starttab, tablen);
335 readtab(inf, lengthtab, tablen);
340 for (
size_t y = 0;
y < ysize;
y++) {
341 for (
size_t z = 0;
z < zsize_file;
z++) {
342 if (starttab[
y +
z * ysize] < cur) {
346 cur = starttab[
y +
z * ysize];
363 for (
size_t z = 0;
z < zsize_read;
z++) {
365 for (
size_t y = 0;
y < ysize;
y++) {
370 uint *lptr_next = lptr + xsize;
379 for (
size_t y = 0;
y < ysize;
y++) {
381 uint *lptr_next = lptr + xsize;
383 for (
size_t z = 0;
z < zsize_read;
z++) {
391 (
uchar *)lptr, (
uchar *)lptr_next, rledat, rledat_next, 3 -
z);
411 for (
size_t z = 0;
z < zsize_read;
z++) {
413 for (
size_t y = 0;
y < ysize;
y++) {
419 float *fptr_next = fptr + (xsize * 4);
420 dirty_flag |=
expandrow2(fptr, fptr_next, rledat, rledat_next, 3 -
z);
427 float *fptr_next = fptr + (xsize * 4);
429 for (
size_t y = 0;
y < ysize;
y++) {
431 for (
size_t z = 0;
z < zsize_read;
z++) {
437 dirty_flag |=
expandrow2(fptr, fptr_next, rledat, rledat_next, 3 -
z);
443#undef MFILE_CAPACITY_AT_PTR_OK_OR_FAIL
454#define MFILE_CAPACITY_AT_PTR_OK_OR_FAIL(p) \
455 if (UNLIKELY((p) > mem_end)) { \
456 dirty_flag |= DIRTY_FLAG_EOF; \
457 goto fail_uncompressed; \
465 goto fail_uncompressed;
474 for (
size_t z = 0;
z < zsize_read;
z++) {
483 for (
size_t y = 0;
y < ysize;
y++) {
484 const uchar *rledat_next = rledat + xsize;
485 const int z_ofs = 3 -
z;
488 rledat = rledat_next;
497 goto fail_uncompressed;
505 for (
size_t z = 0;
z < zsize_read;
z++) {
509 for (
size_t y = 0;
y < ysize;
y++) {
510 const uchar *rledat_next = rledat + xsize * 2;
511 const int z_ofs = 3 -
z;
514 rledat = rledat_next;
519#undef MFILE_CAPACITY_AT_PTR_OK_OR_FAIL
529 if (image.
zsize == 1) {
531 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
533 rect[1] = rect[2] = rect[3];
537 else if (image.
zsize == 2) {
540 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
542 rect[1] = rect[2] = rect[3];
546 else if (image.
zsize == 3) {
549 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
557 if (image.
zsize == 1) {
559 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
561 fbase[1] = fbase[2] = fbase[3];
565 else if (image.
zsize == 2) {
568 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
570 fbase[1] = fbase[2] = fbase[3];
574 else if (image.
zsize == 3) {
577 for (
size_t x =
size_t(ibuf->
x) *
size_t(ibuf->
y);
x > 0;
x--) {
615 *lptr = ((cptr[0] << 8) | (cptr[1] << 0)) /
float(0xFFFF);
622 float *optr,
const float *optr_end,
const uchar *iptr,
const uchar *iptr_end,
int z)
627#define EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL(iptr_next) \
628 if (UNLIKELY(iptr_next > iptr_end)) { \
633#define EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL(optr_next) \
634 if (UNLIKELY(optr_next > optr_end)) { \
642 const uchar *iptr_next = iptr + 2;
644 pixel = (iptr[0] << 8) | (iptr[1] << 0);
647 if (!(
count = (pixel & 0x7f))) {
650 const float *optr_next = optr +
count;
653 iptr_next = iptr + (
count * 2);
656 optr[0 * 4] = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
657 optr[1 * 4] = ((iptr[2] << 8) | (iptr[3] << 0)) /
float(0xFFFF);
658 optr[2 * 4] = ((iptr[4] << 8) | (iptr[5] << 0)) /
float(0xFFFF);
659 optr[3 * 4] = ((iptr[6] << 8) | (iptr[7] << 0)) /
float(0xFFFF);
660 optr[4 * 4] = ((iptr[8] << 8) | (iptr[9] << 0)) /
float(0xFFFF);
661 optr[5 * 4] = ((iptr[10] << 8) | (iptr[11] << 0)) /
float(0xFFFF);
662 optr[6 * 4] = ((iptr[12] << 8) | (iptr[13] << 0)) /
float(0xFFFF);
663 optr[7 * 4] = ((iptr[14] << 8) | (iptr[15] << 0)) /
float(0xFFFF);
669 *optr = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
676 iptr_next = iptr + 2;
678 pixel_f = ((iptr[0] << 8) | (iptr[1] << 0)) /
float(0xFFFF);
682 optr[0 * 4] = pixel_f;
683 optr[1 * 4] = pixel_f;
684 optr[2 * 4] = pixel_f;
685 optr[3 * 4] = pixel_f;
686 optr[4 * 4] = pixel_f;
687 optr[5 * 4] = pixel_f;
688 optr[6 * 4] = pixel_f;
689 optr[7 * 4] = pixel_f;
703#undef EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL
704#undef EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL
714#define EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL(iptr_next) \
715 if (UNLIKELY(iptr_next > iptr_end)) { \
720#define EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL(optr_next) \
721 if (UNLIKELY(optr_next > optr_end)) { \
729 const uchar *iptr_next = iptr + 1;
733 if (!(
count = (pixel & 0x7f))) {
736 const uchar *optr_next = optr + (int(
count) * 4);
740 iptr_next = iptr +
count;
743 optr[0 * 4] = iptr[0];
744 optr[1 * 4] = iptr[1];
745 optr[2 * 4] = iptr[2];
746 optr[3 * 4] = iptr[3];
747 optr[4 * 4] = iptr[4];
748 optr[5 * 4] = iptr[5];
749 optr[6 * 4] = iptr[6];
750 optr[7 * 4] = iptr[7];
762 iptr_next = iptr + 1;
788#undef EXPAND_CAPACITY_AT_INPUT_OK_OR_FAIL
789#undef EXPAND_CAPACITY_AT_OUTPUT_OK_OR_FAIL
817 uint *starttab, *lengthtab;
820 int rlebuflen, goodwrite;
828 tablen = ysize * zsize *
sizeof(int);
833 rlebuflen = 1.05 * xsize + 10;
846 image->
xsize = xsize;
847 image->
ysize = ysize;
848 image->
zsize = zsize;
855 for (
y = 0;
y < ysize;
y++) {
856 for (
z = 0;
z < zsize;
z++) {
866 else if (
z < 8 && zptr) {
871 BLI_assert_msg(
len <= rlebuflen,
"The length calculated for 'rlebuflen' was too small!");
873 goodwrite *= fwrite(rlebuf,
len, 1, outf);
874 starttab[
y +
z * ysize] =
pos;
875 lengthtab[
y +
z * ysize] =
len;
885 goodwrite *=
writetab(outf, starttab, tablen);
886 goodwrite *=
writetab(outf, lengthtab, tablen);
915 const uchar *iptr, *ibufend, *sptr;
922 ibufend = iptr + row_len * 4;
925 while (iptr < ibufend) {
928 while ((iptr < ibufend) && ((iptr[-8] != iptr[-4]) || (iptr[-4] != iptr[0]))) {
932 count = (iptr - sptr) / 4;
936 *optr++ = 0x80 | todo;
938 optr[0] = sptr[0 * 4];
939 optr[1] = sptr[1 * 4];
940 optr[2] = sptr[2 * 4];
941 optr[3] = sptr[3 * 4];
942 optr[4] = sptr[4 * 4];
943 optr[5] = sptr[5 * 4];
944 optr[6] = sptr[6 * 4];
945 optr[7] = sptr[7 * 4];
959 while ((iptr < ibufend) && (*iptr == cc)) {
962 count = (iptr - sptr) / 4;
971 return optr - rlebuf;
976 const uint limit = std::numeric_limits<ushort>::max();
977 if (ibuf->
x > limit || ibuf->
y > limit) {
982 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)
#define CLOG_ERROR(clg_ref,...)
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