Skip to content
Myr

Merge branch 'master' of https://github.com/fahickman/stb into working

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parents a84daa6, 80a0e90 · Browse files · 1 file+110 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+110 added−29 removed

  • other:+110 added−29 removed

1 file+110 added−29 removed

modified:stb_image_write.h+110 added-29 removed
145 unmodified lines
146147typedef unsigned int stbiw_uint32;148typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];149150static int stbi__write_tga_with_rle = 1;151152static void writefv(FILE *f, const char *fmt, va_list v)153{154 while (*fmt) {155 switch (*fmt++) {
12 unmodified lines
168 }169 }170}171172static void writef(FILE *f, const char *fmt, ...)173{174 va_list v;175 va_start(v, fmt);176 writefv(f, fmt, v);177 va_end(v);178}179180static void write3(FILE *f, unsigned char a, unsigned char b, unsigned char c)181{182 unsigned char arr[3];183 arr[0] = a, arr[1] = b, arr[2] = c;184 fwrite(arr, 3, 1, f);185}186177static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono)187static void write_pixel(FILE *f, int rgb_dir, int comp, int write_alpha, int expand_mono, const unsigned char *d)188{189 unsigned char bg[3] = { 255, 0, 255}, px[3];190 int k;191192 if (write_alpha < 0)193 fwrite(&d[comp - 1], 1, 1, f);194 switch (comp) {195 case 1: fwrite(d, 1, 1, f);196 break;197 case 2: if (expand_mono)198 write3(f, d[0], d[0], d[0]); // monochrome bmp199 else200 fwrite(d, 1, 1, f); // monochrome TGA201 break;202 case 4:203 if (!write_alpha) {204 // composite against pink background205 for (k = 0; k < 3; ++k)206 px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255;207 write3(f, px[1 - rgb_dir], px[1], px[1 + rgb_dir]);208 break;209 }210 /* FALLTHROUGH */211 case 3:212 write3(f, d[1 - rgb_dir], d[1], d[1 + rgb_dir]);213 break;214 }215 if (write_alpha > 0)216 fwrite(&d[comp - 1], 1, 1, f);217}218219static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono)220{221 stbiw_uint32 zero = 0;181 int i,j,k, j_end;222 int i,j, j_end;223224 if (y <= 0)225 return;226
4 unmodified lines
231232 for (; j != j_end; j += vdir) {233 for (i=0; i < x; ++i) {234 unsigned char *d = (unsigned char *) data + (j*x+i)*comp;194 if (write_alpha < 0)195 fwrite(&d[comp-1], 1, 1, f);196 switch (comp) {197 case 1: fwrite(d, 1, 1, f);198 break;199 case 2: if (expand_mono)200 write3(f, d[0],d[0],d[0]); // monochrome bmp201 else202 fwrite(d, 1, 1, f); // monochrome TGA203 break;204 case 4:205 if (!write_alpha) {206 // composite against pink background207 for (k=0; k < 3; ++k)208 px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255;209 write3(f, px[1-rgb_dir],px[1],px[1+rgb_dir]);210 break;211 }212 /* FALLTHROUGH */213 case 3:214 write3(f, d[1-rgb_dir],d[1],d[1+rgb_dir]);215 break;216 }217 if (write_alpha > 0)218 fwrite(&d[comp-1], 1, 1, f);235 write_pixel(f, rgb_dir, comp, write_alpha, expand_mono, d);236 }237 fwrite(&zero,scanline_pad,1,f);238 }239}
27 unmodified lines
267{268 int has_alpha = (comp == 2 || comp == 4);269 int colorbytes = has_alpha ? comp-1 : comp;270 int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3271 FILE *f;272273 if (!stbi__write_tga_with_rle) {254 return outfile(filename, -1,-1, x, y, comp, 0, (void *) data, has_alpha, 0,255 "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8);274 return outfile(filename, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,275 "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);276 }277278 if (y < 0 || x < 0) return 0;279 f = fopen(filename, "wb");280 if (f) {281 int i,j,k;282283 writef(f, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8);284285 for (j = y - 1; j >= 0; --j) {286 const unsigned char *row = (unsigned char *) data + j * x * comp;287 int len;288289 for (i = 0; i < x; i += len) {290 const unsigned char *begin = row + i * comp;291 int diff = 1;292 len = 1;293294 if (i < x - 1) {295 ++len;296 diff = memcmp(begin, row + (i + 1) * comp, comp);297 if (diff) {298 const unsigned char *prev = begin;299 for (k = i + 2; k < x && len < 128; ++k) {300 if (memcmp(prev, row + k * comp, comp)) {301 prev += comp;302 ++len;303 } else {304 --len;305 break;306 }307 }308 } else {309 for (k = i + 2; k < x && len < 128; ++k) {310 if (!memcmp(begin, row + k * comp, comp)) {311 ++len;312 } else {313 break;314 }315 }316 }317 }318319 if (diff) {320 unsigned char header = (unsigned char) (len - 1);321 fwrite(&header, 1, 1, f);322 for (k = 0; k < len; ++k) {323 write_pixel(f, -1, comp, has_alpha, 0, begin + k * comp);324 }325 } else {326 unsigned char header = (unsigned char) (len - 129);327 fwrite(&header, 1, 1, f);328 write_pixel(f, -1, comp, has_alpha, 0, begin);329 }330 }331 }332333 fclose(f);334 }335336 return f != NULL;337}338339// *************************************************************************************************340// Radiance RGBE HDR writer
492 unmodified lines