// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdmaster.c
// Function: jpeg_calc_output_dimensions
GLOBAL(void)
jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
/* Do computations that are needed before master selection phase */
{
#ifdef IDCT_SCALING_SUPPORTED
  int ci;
  jpeg_component_info *compptr;
#endif

  /* Prevent application from calling me at wrong times */
  if (cinfo->global_state != DSTATE_READY)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

#ifdef IDCT_SCALING_SUPPORTED

  /* Compute actual output image dimensions and DCT scaling choices. */
  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
    /* Provide 1/8 scaling */
    cinfo->output_width = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width, 8L);
    cinfo->output_height = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height, 8L);
    cinfo->min_DCT_scaled_size = 1;
  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
    /* Provide 1/4 scaling */
    cinfo->output_width = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width, 4L);
    cinfo->output_height = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height, 4L);
    cinfo->min_DCT_scaled_size = 2;
  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
    /* Provide 1/2 scaling */
    cinfo->output_width = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width, 2L);
    cinfo->output_height = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height, 2L);
    cinfo->min_DCT_scaled_size = 4;
  } else {
    /* Provide 1/1 scaling */
    cinfo->output_width = cinfo->image_width;
    cinfo->output_height = cinfo->image_height;
    cinfo->min_DCT_scaled_size = DCTSIZE;
  }
  /* In selecting the actual DCT scaling for each component, we try to
   * scale up the chroma components via IDCT scaling rather than upsampling.
   * This saves time if the upsampler gets to use 1:1 scaling.
   * Note this code assumes that the supported DCT scalings are powers of 2.
   */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    int ssize = cinfo->min_DCT_scaled_size;
    while (ssize < DCTSIZE &&
	   (compptr->h_samp_factor * ssize * 2 <=
	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
	   (compptr->v_samp_factor * ssize * 2 <=
	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
      ssize = ssize * 2;
    }
    compptr->DCT_scaled_size = ssize;
  }

  /* Recompute downsampled dimensions of components;
   * application needs to know these if using raw downsampled data.
   */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Size in samples, after IDCT scaling */
    compptr->downsampled_width = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width *
		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
    compptr->downsampled_height = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height *
		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  }

#else /* !IDCT_SCALING_SUPPORTED */

  /* Hardwire it to "no scaling" */
  cinfo->output_width = cinfo->image_width;
  cinfo->output_height = cinfo->image_height;
  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
   * and has computed unscaled downsampled_width and downsampled_height.
   */

#endif /* IDCT_SCALING_SUPPORTED */

  /* Report number of components in selected colorspace. */
  /* Probably this should be in the color conversion module... */
  switch (cinfo->out_color_space) {
  case JCS_GRAYSCALE:
    cinfo->out_color_components = 1;
    break;
  case JCS_RGB:
#if RGB_PIXELSIZE != 3
    cinfo->out_color_components = RGB_PIXELSIZE;
    break;
#endif /* else share code with YCbCr */
  case JCS_YCbCr:
    cinfo->out_color_components = 3;
    break;
  case JCS_CMYK:
  case JCS_YCCK:
    cinfo->out_color_components = 4;
    break;
  default:			/* else must be same colorspace as in file */
    cinfo->out_color_components = cinfo->num_components;
    break;
  }
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
			      cinfo->out_color_components);

  /* See if upsampler will want to emit more than one row at a time */
  if (use_merged_upsample(cinfo))
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  else
    cinfo->rec_outbuf_height = 1;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpl $0xca, 0x24(%rax)
je 0xe383b
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl $0x14, 0x28(%rax)
movq 0x20(%rsp), %rax
movl 0x24(%rax), %ecx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rdi
callq *%rax
movq 0x20(%rsp), %rax
movl 0x44(%rax), %eax
shll $0x3, %eax
movq 0x20(%rsp), %rcx
cmpl 0x48(%rcx), %eax
ja 0xe38a6
movq 0x20(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movl $0x8, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x20(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movl $0x8, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8c(%rax)
movq 0x20(%rsp), %rax
movl $0x1, 0x1a0(%rax)
jmp 0xe39b1
movq 0x20(%rsp), %rax
movl 0x44(%rax), %eax
shll $0x2, %eax
movq 0x20(%rsp), %rcx
cmpl 0x48(%rcx), %eax
ja 0xe3911
movq 0x20(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movl $0x4, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x20(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movl $0x4, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8c(%rax)
movq 0x20(%rsp), %rax
movl $0x2, 0x1a0(%rax)
jmp 0xe39af
movq 0x20(%rsp), %rax
movl 0x44(%rax), %eax
shll %eax
movq 0x20(%rsp), %rcx
cmpl 0x48(%rcx), %eax
ja 0xe3978
movq 0x20(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movl $0x2, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x20(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movl $0x2, %esi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8c(%rax)
movq 0x20(%rsp), %rax
movl $0x4, 0x1a0(%rax)
jmp 0xe39ad
movq 0x20(%rsp), %rax
movl 0x30(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x20(%rsp), %rax
movl 0x34(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8c(%rax)
movq 0x20(%rsp), %rax
movl $0x8, 0x1a0(%rax)
jmp 0xe39af
jmp 0xe39b1
movl $0x0, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq 0x130(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0xe3a9a
movq 0x20(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
cmpl $0x8, 0xc(%rsp)
movb %al, 0xb(%rsp)
jge 0xe3a57
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ecx
imull 0xc(%rsp), %ecx
shll %ecx
movq 0x20(%rsp), %rax
movl 0x198(%rax), %edx
movq 0x20(%rsp), %rax
imull 0x1a0(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0xb(%rsp)
jg 0xe3a57
movq 0x10(%rsp), %rax
movl 0xc(%rax), %eax
imull 0xc(%rsp), %eax
shll %eax
movq 0x20(%rsp), %rcx
movl 0x19c(%rcx), %ecx
movq 0x20(%rsp), %rdx
imull 0x1a0(%rdx), %ecx
cmpl %ecx, %eax
setle %al
movb %al, 0xb(%rsp)
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0xe3a61
jmp 0xe3a70
movl 0xc(%rsp), %eax
shll %eax
movl %eax, 0xc(%rsp)
jmp 0xe39eb
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x24(%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
jmp 0xe39ca
movl $0x0, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq 0x130(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0xe3b65
movq 0x20(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x10(%rsp), %rcx
imull 0x24(%rcx), %eax
cltq
imulq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x198(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x20(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movq 0x10(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x10(%rsp), %rcx
imull 0x24(%rcx), %eax
cltq
imulq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x19c(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x2c(%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
jmp 0xe3ab3
movq 0x20(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x4(%rsp)
subl $0x1, %eax
je 0xe3b94
jmp 0xe3b78
movl 0x4(%rsp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0xe3ba5
jmp 0xe3b86
movl 0x4(%rsp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0xe3bb6
jmp 0xe3bc7
movq 0x20(%rsp), %rax
movl $0x1, 0x90(%rax)
jmp 0xe3bda
movq 0x20(%rsp), %rax
movl $0x3, 0x90(%rax)
jmp 0xe3bda
movq 0x20(%rsp), %rax
movl $0x4, 0x90(%rax)
jmp 0xe3bda
movq 0x20(%rsp), %rax
movl 0x38(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x90(%rax)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
je 0xe3bef
movl $0x1, %eax
movl %eax, (%rsp)
jmp 0xe3bfd
movq 0x20(%rsp), %rax
movl 0x90(%rax), %eax
movl %eax, (%rsp)
movl (%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x94(%rax)
movq 0x20(%rsp), %rdi
callq 0xe3c50
cmpl $0x0, %eax
je 0xe3c32
movq 0x20(%rsp), %rax
movl 0x19c(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x98(%rax)
jmp 0xe3c41
movq 0x20(%rsp), %rax
movl $0x1, 0x98(%rax)
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)