public static void stbiw__write_run_data(stbi__write_context s, int length, byte databyte) { byte lengthbyte = (byte)((length + 128) & 0xff); s.func(s.context, &lengthbyte, (int)(1)); s.func(s.context, &databyte, (int)(1)); }
public static void stbiw__write_dump_data(stbi__write_context s, int length, byte *data) { byte lengthbyte = (byte)((length) & 0xff); s.func(s.context, &lengthbyte, (int)(1)); s.func(s.context, data, (int)(length)); }
public static void stbiw__write_pixels(stbi__write_context s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) { uint zero = (uint)(0); int i; int j; int j_end; if (y <= 0) { return; } if ((vdir) < (0)) { j_end = (int)(-1); j = (int)(y - 1); } else { j_end = (int)(y); j = (int)(0); } for (; j != j_end; j += (int)(vdir)) { for (i = (int)(0); (i) < (x); ++i) { byte *d = (byte *)(data) + (j * x + i) * comp; stbiw__write_pixel(s, (int)(rgb_dir), (int)(comp), (int)(write_alpha), (int)(expand_mono), d); } s.func(s.context, &zero, (int)(scanline_pad)); } }
public static int stbi_write_hdr_core(stbi__write_context s, int x, int y, int comp, float *data) { if ((y <= 0) || (x <= 0) || (data == null)) { return(0); } var scratch = (byte *)(CRuntime.malloc((ulong)(x * 4))); int i; var header = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; var bytes = Encoding.UTF8.GetBytes(header); fixed(byte *ptr = bytes) { s.func(s.context, ((sbyte *)ptr), bytes.Length); } var str = string.Format("EXPOSURE= 1.0000000000000\n\n-Y {0} +X {1}\n", y, x); bytes = Encoding.UTF8.GetBytes(str); fixed(byte *ptr = bytes) { s.func(s.context, ((sbyte *)ptr), bytes.Length); } for (i = 0; i < y; i++) { stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp * i * x); } CRuntime.free(scratch); return(1); }
public static void stbiw__write3(stbi__write_context s, byte a, byte b, byte c) { byte *arr = stackalloc byte[3]; arr[0] = (byte)(a); arr[1] = (byte)(b); arr[2] = (byte)(c); s.func(s.context, arr, (int)(3)); }
public static int stbiw__outfile(stbi__write_context s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, string fmt, params object[] v) { if ((y < 0) || (x < 0)) { return(0); } stbiw__writefv(s, fmt, v); stbiw__write_pixels(s, rgb_dir, vdir, x, y, comp, data, alpha, pad, expand_mono); return(1); }
public static int stbi_write_bmp_core(stbi__write_context s, int x, int y, int comp, void *data) { int pad = (int)((-x * 3) & 3); return ((int) (stbiw__outfile(s, (int)(-1), (int)(-1), (int)(x), (int)(y), (int)(comp), (int)(1), data, (int)(0), (int)(pad), "11 4 22 44 44 22 444444", (int)('B'), (int)('M'), (int)(14 + 40 + (x * 3 + pad) * y), (int)(0), (int)(0), (int)(14 + 40), (int)(40), (int)(x), (int)(y), (int)(1), (int)(24), (int)(0), (int)(0), (int)(0), (int)(0), (int)(0), (int)(0)))); }
public static void stbiw__write_pixel(stbi__write_context s, int rgb_dir, int comp, int write_alpha, int expand_mono, byte *d) { byte *bg = stackalloc byte[3]; bg[0] = (byte)(255); bg[1] = (byte)(0); bg[2] = (byte)(255); byte *px = stackalloc byte[3]; int k; if ((write_alpha) < (0)) { s.func(s.context, &d[comp - 1], (int)(1)); } switch (comp) { case 1: s.func(s.context, d, (int)(1)); break; case 2: if ((expand_mono) != 0) { stbiw__write3(s, (byte)(d[0]), (byte)(d[0]), (byte)(d[0])); } else { s.func(s.context, d, (int)(1)); } break; case 3: case 4: if (((comp) == (4)) && (write_alpha == 0)) { for (k = (int)(0); (k) < (3); ++k) { px[k] = (byte)(bg[k] + ((d[k] - bg[k]) * d[3]) / 255); } stbiw__write3(s, (byte)(px[1 - rgb_dir]), (byte)(px[1]), (byte)(px[1 + rgb_dir])); break; } stbiw__write3(s, (byte)(d[1 - rgb_dir]), (byte)(d[1]), (byte)(d[1 + rgb_dir])); break; } if ((write_alpha) > (0)) { s.func(s.context, &d[comp - 1], (int)(1)); } }
public static int stbi_write_bmp_to_func(WriteCallback func, void *context, int x, int y, int comp, void *data) { var s = new stbi__write_context(); stbi__start_write_callbacks(s, func, context); return(stbi_write_bmp_core(s, x, y, comp, data)); }
public static int stbi_write_jpg_to_func(WriteCallback func, void *context, int x, int y, int comp, void *data, int quality) { stbi__write_context s = new stbi__write_context(); stbi__start_write_callbacks(s, func, context); return(stbi_write_jpg_core(s, x, y, comp, data, quality)); }
public static void stbiw__writefv(stbi__write_context s, string fmt, params object[] v) { var vindex = 0; for (var i = 0; i < fmt.Length; ++i) { var c = fmt[i]; switch (c) { case ' ': { break; } case '1': { var x = (byte)((int)v[vindex++] & 0xff); s.func(s.context, &x, 1); break; } case '2': { var x = (int)v[vindex++]; var b = stackalloc byte[2]; b[0] = (byte)(x & 0xff); b[1] = (byte)((x >> 8) & 0xff); s.func(s.context, b, 2); break; } case '4': { var x = (int)v[vindex++]; var b = stackalloc byte[4]; b[0] = (byte)(x & 0xff); b[1] = (byte)((x >> 8) & 0xff); b[2] = (byte)((x >> 16) & 0xff); b[3] = (byte)((x >> 24) & 0xff); s.func(s.context, b, 4); break; } } } }
public static void stbiw__writef(stbi__write_context s, string fmt, params object[] v) { stbiw__writefv(s, fmt, v); }
public static void stbi__start_write_callbacks(stbi__write_context s, WriteCallback c, void *context) { s.func = c; s.context = context; }
public static int stbi_write_tga_core(stbi__write_context s, int x, int y, int comp, void *data) { int has_alpha = (((comp) == (2)) || ((comp) == (4))) ? 1 : 0; int colorbytes = (int)((has_alpha) != 0 ? comp - 1 : comp); int format = (int)((colorbytes) < (2) ? 3 : 2); if (((y) < (0)) || ((x) < (0))) { return((int)(0)); } if (stbi_write_tga_with_rle == 0) { return ((int) (stbiw__outfile(s, (int)(-1), (int)(-1), (int)(x), (int)(y), (int)(comp), (int)(0), data, (int)(has_alpha), (int)(0), "111 221 2222 11", (int)(0), (int)(0), (int)(format), (int)(0), (int)(0), (int)(0), (int)(0), (int)(0), (int)(x), (int)(y), (int)((colorbytes + has_alpha) * 8), (int)(has_alpha * 8)))); } else { int i; int j; int k; stbiw__writef(s, "111 221 2222 11", (int)(0), (int)(0), (int)(format + 8), (int)(0), (int)(0), (int)(0), (int)(0), (int)(0), (int)(x), (int)(y), (int)((colorbytes + has_alpha) * 8), (int)(has_alpha * 8)); for (j = (int)(y - 1); (j) >= (0); --j) { byte *row = (byte *)(data) + j * x * comp; int len; for (i = (int)(0); (i) < (x); i += (int)(len)) { byte *begin = row + i * comp; int diff = (int)(1); len = (int)(1); if ((i) < (x - 1)) { ++len; diff = (int)(memcmp(begin, row + (i + 1) * comp, (ulong)(comp))); if ((diff) != 0) { byte *prev = begin; for (k = (int)(i + 2); ((k) < (x)) && ((len) < (128)); ++k) { if ((memcmp(prev, row + k * comp, (ulong)(comp))) != 0) { prev += comp; ++len; } else { --len; break; } } } else { for (k = (int)(i + 2); ((k) < (x)) && ((len) < (128)); ++k) { if (memcmp(begin, row + k * comp, (ulong)(comp)) == 0) { ++len; } else { break; } } } } if ((diff) != 0) { byte header = (byte)((len - 1) & 0xff); s.func(s.context, &header, (int)(1)); for (k = (int)(0); (k) < (len); ++k) { stbiw__write_pixel(s, (int)(-1), (int)(comp), (int)(has_alpha), (int)(0), begin + k * comp); } } else { byte header = (byte)((len - 129) & 0xff); s.func(s.context, &header, (int)(1)); stbiw__write_pixel(s, (int)(-1), (int)(comp), (int)(has_alpha), (int)(0), begin); } } } } return((int)(1)); }
public static void stbiw__write_hdr_scanline(stbi__write_context s, int width, int ncomp, byte *scratch, float *scanline) { byte *scanlineheader = stackalloc byte[4]; scanlineheader[0] = (byte)(2); scanlineheader[1] = (byte)(2); scanlineheader[2] = (byte)(0); scanlineheader[3] = (byte)(0); byte * rgbe = stackalloc byte[4]; float *linear = stackalloc float[3]; int x; scanlineheader[2] = (byte)((width & 0xff00) >> 8); scanlineheader[3] = (byte)(width & 0x00ff); if (((width) < (8)) || ((width) >= (32768))) { for (x = (int)(0); (x) < (width); x++) { switch (ncomp) { case 4: case 3: linear[2] = (float)(scanline[x * ncomp + 2]); linear[1] = (float)(scanline[x * ncomp + 1]); linear[0] = (float)(scanline[x * ncomp + 0]); break; default: linear[0] = (float)(linear[1] = (float)(linear[2] = (float)(scanline[x * ncomp + 0]))); break; } stbiw__linear_to_rgbe(rgbe, linear); s.func(s.context, rgbe, (int)(4)); } } else { int c; int r; for (x = (int)(0); (x) < (width); x++) { switch (ncomp) { case 4: case 3: linear[2] = (float)(scanline[x * ncomp + 2]); linear[1] = (float)(scanline[x * ncomp + 1]); linear[0] = (float)(scanline[x * ncomp + 0]); break; default: linear[0] = (float)(linear[1] = (float)(linear[2] = (float)(scanline[x * ncomp + 0]))); break; } stbiw__linear_to_rgbe(rgbe, linear); scratch[x + width * 0] = (byte)(rgbe[0]); scratch[x + width * 1] = (byte)(rgbe[1]); scratch[x + width * 2] = (byte)(rgbe[2]); scratch[x + width * 3] = (byte)(rgbe[3]); } s.func(s.context, scanlineheader, (int)(4)); for (c = (int)(0); (c) < (4); c++) { byte *comp = &scratch[width * c]; x = (int)(0); while ((x) < (width)) { r = (int)(x); while ((r + 2) < (width)) { if (((comp[r]) == (comp[r + 1])) && ((comp[r]) == (comp[r + 2]))) { break; } ++r; } if ((r + 2) >= (width)) { r = (int)(width); } while ((x) < (r)) { int len = (int)(r - x); if ((len) > (128)) { len = (int)(128); } stbiw__write_dump_data(s, (int)(len), &comp[x]); x += (int)(len); } if ((r + 2) < (width)) { while (((r) < (width)) && ((comp[r]) == (comp[x]))) { ++r; } while ((x) < (r)) { int len = (int)(r - x); if ((len) > (127)) { len = (int)(127); } stbiw__write_run_data(s, (int)(len), (byte)(comp[x])); x += (int)(len); } } } } } }