// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-bitmap.c
// Function: scan_image
static void scan_image(de_bitmap *img, struct image_scan_opt_data *optctx)
{
	i64 i, j;
	de_color clr;
	de_colorsample a, r, g, b;

	if(img->bytes_per_pixel==1) { // Special case
		optctx->has_visible_pixels = 1;

		for(j=0; j<img->height; j++) {
			for(i=0; i<img->width; i++) {
				clr = de_bitmap_getpixel(img, i, j);
				r = DE_COLOR_K(clr);
				if(r!=0 && r!=255) {
					optctx->is_nonbilevel = 1;
					return;
				}
			}
		}
		return;
	}

	optctx->has_only_invis_black_pixels = 1;
	optctx->has_only_invis_white_pixels = 1;

	for(j=0; j<img->height; j++) {
		for(i=0; i<img->width; i++) {
			clr = de_bitmap_getpixel(img, i, j);
			// TODO: Optimize these tests. We check for too many things we
			// already know the answer to.
			a = DE_COLOR_A(clr);
			r = DE_COLOR_R(clr);
			g = DE_COLOR_G(clr);
			b = DE_COLOR_B(clr);

			if(!optctx->has_visible_pixels && a==0) {
				if(optctx->has_only_invis_black_pixels) {
					if(r || g || b) {
						optctx->has_only_invis_black_pixels = 0;
					}
				}
				if(optctx->has_only_invis_white_pixels) {
					if((r!=0xff) || (g!=0xff) || (b!=0xff)) {
						optctx->has_only_invis_white_pixels = 0;
					}
				}
			}

			if(!optctx->has_visible_pixels && a!=0) {
				optctx->has_visible_pixels = 1;
				optctx->has_only_invis_black_pixels = 0;
				optctx->has_only_invis_white_pixels = 0;
			}
			if(!optctx->has_trns && a<255) {
				optctx->has_trns = 1;
				optctx->is_nonbilevel = 1;
			}
			if(!optctx->has_color && img->bytes_per_pixel>=3 &&
				(g!=r || b!=r))
			{
				// This doesn't test for "&& a!=0". It *could*, but our (undocumented)
				// behavior is to try not to change the underlying color of invisible
				// pixels.
				// If we *were* going to do that, it would be more logical, and better
				// for compression, to change the underlying color to black, instead
				// of ultimately writing what amounts to garbage (probably just the
				// red channel would survive).
				optctx->has_color = 1;
				optctx->is_nonbilevel = 1;
			}

			if(!optctx->is_nonbilevel && (r!=0 && r!=255)) {
				// Only need to test one of the color samples. The has_color and
				// has_trns tests will take care of the rest.
				optctx->is_nonbilevel = 1;
			}
		}

		// After each row, test whether we've learned everything we can learn
		// about this image.
		if(img->bytes_per_pixel==2) {
			if(optctx->has_trns && optctx->has_visible_pixels) return;
		}
		else if(img->bytes_per_pixel==3) {
			if(optctx->has_color) return;
		}
		else { // bypp==4
			if(optctx->has_color && optctx->has_trns && optctx->has_visible_pixels) return;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl 0x24(%rdi), %r13d
cmpl $0x1, %r13d
movq %rdi, 0x8(%rsp)
jne 0xe8600
movb $0x1, 0x2(%rbx)
movq 0x10(%rdi), %r14
xorl %r13d, %r13d
testq %r14, %r14
cmovleq %r13, %r14
xorl %r15d, %r15d
cmpq %r14, %r15
je 0xe8799
movq 0x8(%rdi), %rbp
testq %rbp, %rbp
cmovleq %r13, %rbp
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0xe85fb
movq %r12, %rsi
movq %r15, %rdx
callq 0xe735f
shrl $0x10, %eax
testb %al, %al
je 0xe85f1
movzbl %al, %eax
cmpl $0xff, %eax
jne 0xe8795
incq %r12
movq 0x8(%rsp), %rdi
jmp 0xe85cc
incq %r15
jmp 0xe85b5
movw $0x101, 0x4(%rbx)       # imm = 0x101
movq 0x10(%rdi), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
movq %rax, 0x10(%rsp)
movb $0x1, %r14b
movb $0x1, %al
movl %eax, 0x4(%rsp)
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r15
je 0xe8799
movq 0x8(%rdi), %rbp
testq %rbp, %rbp
movl $0x0, %eax
cmovleq %rax, %rbp
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0xe8769
movq %r12, %rsi
movq %r15, %rdx
callq 0xe735f
movl %eax, %ecx
shrl $0x10, %ecx
movb 0x2(%rbx), %dl
testb %dl, %dl
sete %sil
cmpl $0x1000000, %eax        # imm = 0x1000000
setb %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0xe86c3
cmpb $0x0, 0x4(%rsp)
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
jne 0xe8695
movb $0x0, 0x4(%rbx)
movl $0x0, 0x4(%rsp)
testb %r14b, %r14b
movq 0x8(%rsp), %rdi
je 0xe86f3
movl %ecx, %edx
xorl $0xff, %edx
movl %eax, %esi
notl %esi
andl $0xff00, %esi           # imm = 0xFF00
orl %edx, %esi
setne %dl
cmpb $-0x1, %al
setne %sil
orb %dl, %sil
jne 0xe86ef
jmp 0xe86f6
testb %dl, %dl
sete %dl
cmpl $0x1000000, %eax        # imm = 0x1000000
setae %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0x8(%rsp), %rdi
jne 0xe86f6
movb $0x1, 0x2(%rbx)
movb $0x0, 0x4(%rbx)
movl $0x0, 0x4(%rsp)
movb $0x0, 0x5(%rbx)
xorl %r14d, %r14d
cmpb $0x0, 0x1(%rbx)
sete %dl
cmpl $0xff000000, %eax       # imm = 0xFF000000
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xe8717
movb $0x1, %dl
movb %dl, 0x1(%rbx)
movb %dl, 0x3(%rbx)
cmpl $0x3, %r13d
jl 0xe8747
cmpb $0x0, (%rbx)
jne 0xe8747
movl %eax, %edx
shrl $0x8, %edx
movl $0xff, %r9d
andl %r9d, %edx
movl %ecx, %esi
andl %r9d, %esi
xorl %esi, %edx
andl %r9d, %eax
xorl %esi, %eax
orl %edx, %eax
je 0xe8747
movb $0x1, %al
movb %al, (%rbx)
movb %al, 0x3(%rbx)
cmpb $0x0, 0x3(%rbx)
je 0xe8755
incq %r12
jmp 0xe8642
testb %cl, %cl
je 0xe874d
movzbl %cl, %eax
cmpl $0xff, %eax
je 0xe874d
movb $0x1, 0x3(%rbx)
jmp 0xe874d
cmpl $0x3, %r13d
je 0xe8788
cmpl $0x2, %r13d
je 0xe877a
cmpb $0x0, (%rbx)
je 0xe878d
cmpb $0x0, 0x1(%rbx)
je 0xe878d
cmpb $0x0, 0x2(%rbx)
je 0xe878d
jmp 0xe8799
cmpb $0x0, (%rbx)
jne 0xe8799
incq %r15
jmp 0xe8624
movb $0x1, 0x3(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq