// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %rbp
movl 0x24(%rdi), %r14d
cmpl $0x1, %r14d
jne 0xf402d
movb $0x1, 0x2(%rbx)
movq 0x10(%rbp), %r14
testq %r14, %r14
jle 0xf41e5
movq 0x8(%rbp), %r13
xorl %r15d, %r15d
testq %r13, %r13
jle 0xf4020
xorl %r12d, %r12d
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xf2bd3
shrl $0x10, %eax
testb %al, %al
je 0xf4018
movzbl %al, %eax
cmpl $0xff, %eax
jne 0xf41e1
incq %r12
cmpq %r12, %r13
jne 0xf3ff5
incq %r15
cmpq %r14, %r15
jne 0xf3fed
jmp 0xf41e5
movw $0x101, 0x4(%rbx)       # imm = 0x101
movq 0x10(%rbp), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
jle 0xf41e5
movq 0x8(%rbp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
cmpq $0x0, 0x10(%rsp)
jle 0xf41a3
movb 0x2(%rbx), %r13b
movb (%rbx), %al
movb %al, 0x7(%rsp)
movb 0x1(%rbx), %bpl
movb 0x3(%rbx), %r15b
xorl %r12d, %r12d
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0xf2bd3
movl %eax, %ecx
shrl $0x10, %ecx
testb %r13b, %r13b
sete %dl
cmpl $0x1000000, %eax        # imm = 0x1000000
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf40f2
cmpb $0x0, 0x4(%rbx)
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
jne 0xf40c2
movb $0x0, 0x4(%rbx)
cmpb $0x0, 0x5(%rbx)
je 0xf40f2
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
cmpb $0x1, %sil
jne 0xf40f2
movb $0x0, 0x5(%rbx)
testb %r13b, %r13b
sete %dl
cmpl $0x1000000, %eax        # imm = 0x1000000
setae %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf4117
movb $0x1, 0x2(%rbx)
movw $0x0, 0x4(%rbx)
movb $0x1, %r13b
testb %bpl, %bpl
sete %dl
cmpl $0xff000000, %eax       # imm = 0xFF000000
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xf413d
movb $0x1, %r15b
movb %r15b, 0x1(%rbx)
movb %r15b, 0x3(%rbx)
movb $0x1, %bpl
cmpl $0x3, %r14d
jl 0xf4177
cmpb $0x0, 0x7(%rsp)
jne 0xf4177
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
je 0xf4177
movb $0x1, %al
movb %al, (%rbx)
movb $0x1, 0x7(%rsp)
movb %al, 0x3(%rbx)
movb $0x1, %r15b
testb %r15b, %r15b
je 0xf418c
incq %r12
cmpq %r12, 0x10(%rsp)
jne 0xf407d
jmp 0xf41a3
testb %cl, %cl
je 0xf417c
movzbl %cl, %eax
cmpl $0xff, %eax
je 0xf417c
movb $0x1, 0x3(%rbx)
movb $0x1, %r15b
jmp 0xf417c
cmpl $0x3, %r14d
je 0xf41c2
cmpl $0x2, %r14d
je 0xf41b4
cmpb $0x0, (%rbx)
je 0xf41c7
cmpb $0x0, 0x1(%rbx)
je 0xf41c7
cmpb $0x0, 0x2(%rbx)
je 0xf41c7
jmp 0xf41e5
cmpb $0x0, (%rbx)
jne 0xf41e5
movq 0x8(%rsp), %rcx
incq %rcx
movq %rcx, 0x8(%rsp)
cmpq 0x18(%rsp), %rcx
jne 0xf405c
jmp 0xf41e5
movb $0x1, 0x3(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq