| 385 | #endif //!STBI_WRITE_NO_STDIO |
| 386 | |
| 387 | static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) |
| 388 | { |
| 389 | int has_alpha = (comp == 2 || comp == 4); |
| 390 | int colorbytes = has_alpha ? comp-1 : comp; |
| 391 | int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 |
| 392 | |
| 393 | if (y < 0 || x < 0) |
| 394 | return 0; |
| 395 | |
| 396 | if (!stbi_write_tga_with_rle) { |
| 397 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, |
| 398 | "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); |
| 399 | } else { |
| 400 | int i,j,k; |
| 401 | |
| 402 | stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); |
| 403 | |
| 404 | for (j = y - 1; j >= 0; --j) { |
| 405 | unsigned char *row = (unsigned char *) data + j * x * comp; |
| 406 | int len; |
| 407 | |
| 408 | for (i = 0; i < x; i += len) { |
| 409 | unsigned char *begin = row + i * comp; |
| 410 | int diff = 1; |
| 411 | len = 1; |
| 412 | |
| 413 | if (i < x - 1) { |
| 414 | ++len; |
| 415 | diff = memcmp(begin, row + (i + 1) * comp, comp); |
| 416 | if (diff) { |
| 417 | const unsigned char *prev = begin; |
| 418 | for (k = i + 2; k < x && len < 128; ++k) { |
| 419 | if (memcmp(prev, row + k * comp, comp)) { |
| 420 | prev += comp; |
| 421 | ++len; |
| 422 | } else { |
| 423 | --len; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } else { |
| 428 | for (k = i + 2; k < x && len < 128; ++k) { |
| 429 | if (!memcmp(begin, row + k * comp, comp)) { |
| 430 | ++len; |
| 431 | } else { |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (diff) { |
| 439 | unsigned char header = STBIW_UCHAR(len - 1); |
| 440 | s->func(s->context, &header, 1); |
| 441 | for (k = 0; k < len; ++k) { |
| 442 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); |
| 443 | } |
| 444 | } else { |
no test coverage detected