// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jne 0x16d32d
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x16d328
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x16d315
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x16b540
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x26(%rbp)
movzbl -0x26(%rbp), %eax
cmpl $0x0, %eax
je 0x16d305
movzbl -0x26(%rbp), %eax
cmpl $0xff, %eax
je 0x16d305
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x16d574
jmp 0x16d307
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x16d2b4
jmp 0x16d317
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x16d29e
jmp 0x16d574
movq -0x10(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x5(%rax)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x16d574
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x16d4f6
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x16b540
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, -0x25(%rbp)
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x26(%rbp)
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x27(%rbp)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
jne 0x16d433
movzbl -0x25(%rbp), %eax
cmpl $0x0, %eax
jne 0x16d433
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
je 0x16d3fc
movzbl -0x26(%rbp), %eax
cmpl $0x0, %eax
jne 0x16d3f2
movzbl -0x27(%rbp), %eax
cmpl $0x0, %eax
jne 0x16d3f2
movzbl -0x28(%rbp), %eax
cmpl $0x0, %eax
je 0x16d3fa
movq -0x10(%rbp), %rax
movb $0x0, 0x4(%rax)
jmp 0x16d3fc
movq -0x10(%rbp), %rax
cmpb $0x0, 0x5(%rax)
je 0x16d431
movzbl -0x26(%rbp), %eax
cmpl $0xff, %eax
jne 0x16d427
movzbl -0x27(%rbp), %eax
cmpl $0xff, %eax
jne 0x16d427
movzbl -0x28(%rbp), %eax
cmpl $0xff, %eax
je 0x16d42f
movq -0x10(%rbp), %rax
movb $0x0, 0x5(%rax)
jmp 0x16d431
jmp 0x16d433
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
jne 0x16d45e
movzbl -0x25(%rbp), %eax
cmpl $0x0, %eax
je 0x16d45e
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
jne 0x16d483
movzbl -0x25(%rbp), %eax
cmpl $0xff, %eax
jge 0x16d483
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x16d4bd
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jl 0x16d4bd
movzbl -0x27(%rbp), %eax
movzbl -0x26(%rbp), %ecx
cmpl %ecx, %eax
jne 0x16d4ae
movzbl -0x28(%rbp), %eax
movzbl -0x26(%rbp), %ecx
cmpl %ecx, %eax
je 0x16d4bd
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x3(%rax)
jne 0x16d4e3
movzbl -0x26(%rbp), %eax
cmpl $0x0, %eax
je 0x16d4e3
movzbl -0x26(%rbp), %eax
cmpl $0xff, %eax
je 0x16d4e3
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x16d4e5
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x16d35f
movq -0x8(%rbp), %rax
cmpl $0x2, 0x24(%rax)
jne 0x16d51e
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x16d51c
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x16d51c
jmp 0x16d574
jmp 0x16d561
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x16d535
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x16d533
jmp 0x16d574
jmp 0x16d55f
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
je 0x16d55d
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x16d55d
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x16d55d
jmp 0x16d574
jmp 0x16d55f
jmp 0x16d561
jmp 0x16d563
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x16d345
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)