// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcparam.c
// Function: jpeg_set_colorspace
GLOBAL(void)
jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
{
  jpeg_component_info * compptr;
  int ci;

#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
  (compptr = &cinfo->comp_info[index], \
   compptr->component_id = (id), \
   compptr->h_samp_factor = (hsamp), \
   compptr->v_samp_factor = (vsamp), \
   compptr->quant_tbl_no = (quant), \
   compptr->dc_tbl_no = (dctbl), \
   compptr->ac_tbl_no = (actbl) )

  /* Safety check to ensure start_compress not called yet. */
  if (cinfo->global_state != CSTATE_START)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
   * tables 1 for chrominance components.
   */

  cinfo->jpeg_color_space = colorspace;

  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */

  switch (colorspace) {
  case JCS_GRAYSCALE:
    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
    cinfo->num_components = 1;
    /* JFIF specifies component ID 1 */
    SET_COMP(0, 1, 1,1, 0, 0,0);
    break;
  case JCS_RGB:
    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
    cinfo->num_components = 3;
    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
    break;
  case JCS_YCbCr:
    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
    cinfo->num_components = 3;
    /* JFIF specifies component IDs 1,2,3 */
    /* We default to 2x2 subsamples of chrominance */
    SET_COMP(0, 1, 2,2, 0, 0,0);
    SET_COMP(1, 2, 1,1, 1, 1,1);
    SET_COMP(2, 3, 1,1, 1, 1,1);
    break;
  case JCS_CMYK:
    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
    cinfo->num_components = 4;
    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
    break;
  case JCS_YCCK:
    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
    cinfo->num_components = 4;
    SET_COMP(0, 1, 2,2, 0, 0,0);
    SET_COMP(1, 2, 1,1, 1, 1,1);
    SET_COMP(2, 3, 1,1, 1, 1,1);
    SET_COMP(3, 4, 2,2, 0, 0,0);
    break;
  case JCS_UNKNOWN:
    cinfo->num_components = cinfo->input_components;
    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
	       MAX_COMPONENTS);
    for (ci = 0; ci < cinfo->num_components; ci++) {
      SET_COMP(ci, ci, 1,1, 0, 0,0);
    }
    break;
  default:
    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  }
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movl %esi, 0x1c(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x64, 0x24(%rax)
je 0xd626c
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
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x50(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x120(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x12c(%rax)
movl 0x1c(%rsp), %eax
movq %rax, (%rsp)
subq $0x5, %rax
ja 0xd697a
movq (%rsp), %rax
leaq 0x606a5(%rip), %rcx     # 0x136958
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x20(%rsp), %rax
movl $0x1, 0x120(%rax)
movq 0x20(%rsp), %rax
movl $0x1, 0x4c(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd699b
movq 0x20(%rsp), %rax
movl $0x1, 0x12c(%rax)
movq 0x20(%rsp), %rax
movl $0x3, 0x4c(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x52, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x47, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0xc0, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x42, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd699b
movq 0x20(%rsp), %rax
movl $0x1, 0x120(%rax)
movq 0x20(%rsp), %rax
movl $0x3, 0x4c(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x2, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0xc0, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x3, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
jmp 0xd699b
movq 0x20(%rsp), %rax
movl $0x1, 0x12c(%rax)
movq 0x20(%rsp), %rax
movl $0x4, 0x4c(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x43, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x4d, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0xc0, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x59, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x4b, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd699b
movq 0x20(%rsp), %rax
movl $0x1, 0x12c(%rax)
movq 0x20(%rsp), %rax
movl $0x4, 0x4c(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x2, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0xc0, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x3, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl $0x4, (%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x2, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd699b
movq 0x20(%rsp), %rax
movl 0x38(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x4c(%rax)
movq 0x20(%rsp), %rax
cmpl $0x1, 0x4c(%rax)
jl 0xd68b1
movq 0x20(%rsp), %rax
cmpl $0xa, 0x4c(%rax)
jle 0xd68f4
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl $0x1a, 0x28(%rax)
movq 0x20(%rsp), %rax
movl 0x4c(%rax), %ecx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl $0xa, 0x30(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rdi
callq *%rax
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xd6978
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
movslq 0xc(%rsp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd68fc
jmp 0xd699b
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl $0xa, 0x28(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rdi
callq *%rax
addq $0x28, %rsp
retq