// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %rbp
movl 0x24(%rdi), %r14d
cmpl $0x1, %r14d
jne 0xf39e0
movb $0x1, 0x2(%rbx)
movq 0x10(%rbp), %r14
testq %r14, %r14
jle 0xf3bcc
movq 0x8(%rbp), %r13
xorl %r15d, %r15d
testq %r13, %r13
jle 0xf39d3
xorl %r12d, %r12d
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xf2592
shrl $0x10, %eax
testb %al, %al
je 0xf39cb
movzbl %al, %eax
cmpl $0xff, %eax
jne 0xf3bc8
incq %r12
cmpq %r12, %r13
jne 0xf39a8
incq %r15
cmpq %r14, %r15
jne 0xf39a0
jmp 0xf3bcc
movw $0x101, 0x4(%rbx)       # imm = 0x101
movq 0x10(%rbp), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
jle 0xf3bcc
movq 0x8(%rbp), %rax
movq %rax, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq %rbp, 0x30(%rsp)
cmpq $0x0, 0x20(%rsp)
jle 0xf3b8a
movb 0x2(%rbx), %r15b
movb (%rbx), %al
movl %eax, 0x14(%rsp)
movb 0x1(%rbx), %r13b
movb 0x3(%rbx), %bpl
xorl %r12d, %r12d
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
callq 0xf2592
movl %eax, %ecx
shrl $0x10, %ecx
testb %r15b, %r15b
sete %dl
cmpl $0x1000000, %eax        # imm = 0x1000000
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf3ab8
cmpb $0x0, 0xc(%rsp)
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
jne 0xf3a8a
movb $0x0, 0x4(%rbx)
movl $0x0, 0xc(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0xf3af1
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
xorl %r15d, %r15d
orb %dl, %sil
jne 0xf3ae3
jmp 0xf3afc
testb %r15b, %r15b
sete %dl
cmpl $0x1000000, %eax        # imm = 0x1000000
setae %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf3afc
movb $0x1, 0x2(%rbx)
movb $0x0, 0x4(%rbx)
movb $0x1, %r15b
movl $0x0, 0xc(%rsp)
movb $0x0, 0x5(%rbx)
movl $0x0, 0x10(%rsp)
jmp 0xf3afc
movl $0x0, 0x10(%rsp)
xorl %r15d, %r15d
testb %r13b, %r13b
sete %dl
cmpl $0xff000000, %eax       # imm = 0xFF000000
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf3b22
movb $0x1, %bpl
movb %bpl, 0x1(%rbx)
movb %bpl, 0x3(%rbx)
movb $0x1, %r13b
cmpl $0x3, %r14d
jl 0xf3b55
cmpb $0x0, 0x14(%rsp)
jne 0xf3b55
movl %eax, %edx
shrl $0x8, %edx
movl $0xff, %r8d
andl %r8d, %edx
movl %ecx, %esi
andl %r8d, %esi
xorl %esi, %edx
andl %r8d, %eax
xorl %esi, %eax
orl %edx, %eax
jne 0xf3b6c
movl $0x0, 0x14(%rsp)
testb %bpl, %bpl
jne 0xf3b7c
xorl %ebp, %ebp
testb %cl, %cl
je 0xf3b7c
movzbl %cl, %eax
cmpl $0xff, %eax
je 0xf3b7c
jmp 0xf3b75
movb $0x1, (%rbx)
movb $0x1, %al
movl %eax, 0x14(%rsp)
movb $0x1, 0x3(%rbx)
movb $0x1, %bpl
incq %r12
cmpq %r12, 0x20(%rsp)
jne 0xf3a3c
cmpl $0x3, %r14d
je 0xf3ba9
cmpl $0x2, %r14d
je 0xf3b9b
cmpb $0x0, (%rbx)
je 0xf3bae
cmpb $0x0, 0x1(%rbx)
je 0xf3bae
cmpb $0x0, 0x2(%rbx)
je 0xf3bae
jmp 0xf3bcc
cmpb $0x0, (%rbx)
jne 0xf3bcc
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x28(%rsp), %rcx
jne 0xf3a1b
jmp 0xf3bcc
movb $0x1, 0x3(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq