// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package jpeg

import (
	
	
	
	
	
)

// min returns the minimum of two integers.
func min(,  int) int {
	if  <  {
		return 
	}
	return 
}

// div returns a/b rounded to the nearest integer, instead of rounded to zero.
func div(,  int32) int32 {
	if  >= 0 {
		return ( + ( >> 1)) / 
	}
	return -((- + ( >> 1)) / )
}

// bitCount counts the number of bits needed to hold an integer.
var bitCount = [256]byte{
	0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
	5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
	6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
}

type quantIndex int

const (
	quantIndexLuminance quantIndex = iota
	quantIndexChrominance
	nQuantIndex
)

// unscaledQuant are the unscaled quantization tables in zig-zag order. Each
// encoder copies and scales the tables according to its quality parameter.
// The values are derived from section K.1 after converting from natural to
// zig-zag order.
var unscaledQuant = [nQuantIndex][blockSize]byte{
	// Luminance.
	{
		16, 11, 12, 14, 12, 10, 16, 14,
		13, 14, 18, 17, 16, 19, 24, 40,
		26, 24, 22, 22, 24, 49, 35, 37,
		29, 40, 58, 51, 61, 60, 57, 51,
		56, 55, 64, 72, 92, 78, 64, 68,
		87, 69, 55, 56, 80, 109, 81, 87,
		95, 98, 103, 104, 103, 62, 77, 113,
		121, 112, 100, 120, 92, 101, 103, 99,
	},
	// Chrominance.
	{
		17, 18, 18, 24, 21, 24, 47, 26,
		26, 47, 99, 66, 56, 66, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
	},
}

type huffIndex int

const (
	huffIndexLuminanceDC huffIndex = iota
	huffIndexLuminanceAC
	huffIndexChrominanceDC
	huffIndexChrominanceAC
	nHuffIndex
)

// huffmanSpec specifies a Huffman encoding.
type huffmanSpec struct {
	// count[i] is the number of codes of length i bits.
	count [16]byte
	// value[i] is the decoded value of the i'th codeword.
	value []byte
}

// theHuffmanSpec is the Huffman encoding specifications.
// This encoder uses the same Huffman encoding for all images.
var theHuffmanSpec = [nHuffIndex]huffmanSpec{
	// Luminance DC.
	{
		[16]byte{0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
	},
	// Luminance AC.
	{
		[16]byte{0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125},
		[]byte{
			0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
			0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
			0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
			0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
			0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
			0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
			0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
			0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
			0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
			0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
			0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
			0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
			0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
			0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
			0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
			0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
			0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
			0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
			0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
			0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
			0xf9, 0xfa,
		},
	},
	// Chrominance DC.
	{
		[16]byte{0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
	},
	// Chrominance AC.
	{
		[16]byte{0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119},
		[]byte{
			0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
			0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
			0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
			0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
			0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
			0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
			0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
			0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
			0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
			0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
			0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
			0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
			0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
			0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
			0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
			0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
			0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
			0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
			0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
			0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
			0xf9, 0xfa,
		},
	},
}

// huffmanLUT is a compiled look-up table representation of a huffmanSpec.
// Each value maps to a uint32 of which the 8 most significant bits hold the
// codeword size in bits and the 24 least significant bits hold the codeword.
// The maximum codeword size is 16 bits.
type huffmanLUT []uint32

func ( *huffmanLUT) ( huffmanSpec) {
	 := 0
	for ,  := range .value {
		if int() >  {
			 = int()
		}
	}
	* = make([]uint32, +1)
	,  := uint32(0), 0
	for  := 0;  < len(.count); ++ {
		 := uint32(+1) << 24
		for  := uint8(0);  < .count[]; ++ {
			(*)[.value[]] =  | 
			++
			++
		}
		 <<= 1
	}
}

// theHuffmanLUT are compiled representations of theHuffmanSpec.
var theHuffmanLUT [4]huffmanLUT

func init() {
	for ,  := range theHuffmanSpec {
		theHuffmanLUT[].init()
	}
}

// writer is a buffered writer.
type writer interface {
	Flush() error
	io.Writer
	io.ByteWriter
}

// encoder encodes an image to the JPEG format.
type encoder struct {
	// w is the writer to write to. err is the first error encountered during
	// writing. All attempted writes after the first error become no-ops.
	w   writer
	err error
	// buf is a scratch buffer.
	buf [16]byte
	// bits and nBits are accumulated bits to write to w.
	bits, nBits uint32
	// quant is the scaled quantization tables, in zig-zag order.
	quant [nQuantIndex][blockSize]byte
}

func ( *encoder) () {
	if .err != nil {
		return
	}
	.err = .w.Flush()
}

func ( *encoder) ( []byte) {
	if .err != nil {
		return
	}
	_, .err = .w.Write()
}

func ( *encoder) ( byte) {
	if .err != nil {
		return
	}
	.err = .w.WriteByte()
}

// emit emits the least significant nBits bits of bits to the bit-stream.
// The precondition is bits < 1<<nBits && nBits <= 16.
func ( *encoder) (,  uint32) {
	 += .nBits
	 <<= 32 - 
	 |= .bits
	for  >= 8 {
		 := uint8( >> 24)
		.writeByte()
		if  == 0xff {
			.writeByte(0x00)
		}
		 <<= 8
		 -= 8
	}
	.bits, .nBits = , 
}

// emitHuff emits the given value with the given Huffman encoder.
func ( *encoder) ( huffIndex,  int32) {
	 := theHuffmanLUT[][]
	.emit(&(1<<24-1), >>24)
}

// emitHuffRLE emits a run of runLength copies of value encoded with the given
// Huffman encoder.
func ( *encoder) ( huffIndex, ,  int32) {
	,  := , 
	if  < 0 {
		,  = -, -1
	}
	var  uint32
	if  < 0x100 {
		 = uint32(bitCount[])
	} else {
		 = 8 + uint32(bitCount[>>8])
	}
	.emitHuff(, <<4|int32())
	if  > 0 {
		.emit(uint32()&(1<<-1), )
	}
}

// writeMarkerHeader writes the header for a marker with the given length.
func ( *encoder) ( uint8,  int) {
	.buf[0] = 0xff
	.buf[1] = 
	.buf[2] = uint8( >> 8)
	.buf[3] = uint8( & 0xff)
	.write(.buf[:4])
}

// writeDQT writes the Define Quantization Table marker.
func ( *encoder) () {
	const  = 2 + int(nQuantIndex)*(1+blockSize)
	.writeMarkerHeader(dqtMarker, )
	for  := range .quant {
		.writeByte(uint8())
		.write(.quant[][:])
	}
}

// writeSOF0 writes the Start Of Frame (Baseline Sequential) marker.
func ( *encoder) ( image.Point,  int) {
	 := 8 + 3*
	.writeMarkerHeader(sof0Marker, )
	.buf[0] = 8 // 8-bit color.
	.buf[1] = uint8(.Y >> 8)
	.buf[2] = uint8(.Y & 0xff)
	.buf[3] = uint8(.X >> 8)
	.buf[4] = uint8(.X & 0xff)
	.buf[5] = uint8()
	if  == 1 {
		.buf[6] = 1
		// No subsampling for grayscale image.
		.buf[7] = 0x11
		.buf[8] = 0x00
	} else {
		for  := 0;  < ; ++ {
			.buf[3*+6] = uint8( + 1)
			// We use 4:2:0 chroma subsampling.
			.buf[3*+7] = "\x22\x11\x11"[]
			.buf[3*+8] = "\x00\x01\x01"[]
		}
	}
	.write(.buf[:3*(-1)+9])
}

// writeDHT writes the Define Huffman Table marker.
func ( *encoder) ( int) {
	 := 2
	 := theHuffmanSpec[:]
	if  == 1 {
		// Drop the Chrominance tables.
		 = [:2]
	}
	for ,  := range  {
		 += 1 + 16 + len(.value)
	}
	.writeMarkerHeader(dhtMarker, )
	for ,  := range  {
		.writeByte("\x00\x10\x01\x11"[])
		.write(.count[:])
		.write(.value)
	}
}

// writeBlock writes a block of pixel data using the given quantization table,
// returning the post-quantized DC value of the DCT-transformed block. b is in
// natural (not zig-zag) order.
func ( *encoder) ( *block,  quantIndex,  int32) int32 {
	fdct()
	// Emit the DC delta.
	 := div([0], 8*int32(.quant[][0]))
	.emitHuffRLE(huffIndex(2*+0), 0, -)
	// Emit the AC components.
	,  := huffIndex(2*+1), int32(0)
	for  := 1;  < blockSize; ++ {
		 := div([unzig[]], 8*int32(.quant[][]))
		if  == 0 {
			++
		} else {
			for  > 15 {
				.emitHuff(, 0xf0)
				 -= 16
			}
			.emitHuffRLE(, , )
			 = 0
		}
	}
	if  > 0 {
		.emitHuff(, 0x00)
	}
	return 
}

// toYCbCr converts the 8x8 region of m whose top-left corner is p to its
// YCbCr values.
func toYCbCr( image.Image,  image.Point, , ,  *block) {
	 := .Bounds()
	 := .Max.X - 1
	 := .Max.Y - 1
	for  := 0;  < 8; ++ {
		for  := 0;  < 8; ++ {
			, , ,  := .At(min(.X+, ), min(.Y+, )).RGBA()
			, ,  := color.RGBToYCbCr(uint8(>>8), uint8(>>8), uint8(>>8))
			[8*+] = int32()
			[8*+] = int32()
			[8*+] = int32()
		}
	}
}

// grayToY stores the 8x8 region of m whose top-left corner is p in yBlock.
func grayToY( *image.Gray,  image.Point,  *block) {
	 := .Bounds()
	 := .Max.X - 1
	 := .Max.Y - 1
	 := .Pix
	for  := 0;  < 8; ++ {
		for  := 0;  < 8; ++ {
			 := .PixOffset(min(.X+, ), min(.Y+, ))
			[8*+] = int32([])
		}
	}
}

// rgbaToYCbCr is a specialized version of toYCbCr for image.RGBA images.
func rgbaToYCbCr( *image.RGBA,  image.Point, , ,  *block) {
	 := .Bounds()
	 := .Max.X - 1
	 := .Max.Y - 1
	for  := 0;  < 8; ++ {
		 := .Y + 
		if  >  {
			 = 
		}
		 := (-.Min.Y)*.Stride - .Min.X*4
		for  := 0;  < 8; ++ {
			 := .X + 
			if  >  {
				 = 
			}
			 := .Pix[+*4:]
			, ,  := color.RGBToYCbCr([0], [1], [2])
			[8*+] = int32()
			[8*+] = int32()
			[8*+] = int32()
		}
	}
}

// yCbCrToYCbCr is a specialized version of toYCbCr for image.YCbCr images.
func yCbCrToYCbCr( *image.YCbCr,  image.Point, , ,  *block) {
	 := .Bounds()
	 := .Max.X - 1
	 := .Max.Y - 1
	for  := 0;  < 8; ++ {
		 := .Y + 
		if  >  {
			 = 
		}
		for  := 0;  < 8; ++ {
			 := .X + 
			if  >  {
				 = 
			}
			 := .YOffset(, )
			 := .COffset(, )
			[8*+] = int32(.Y[])
			[8*+] = int32(.Cb[])
			[8*+] = int32(.Cr[])
		}
	}
}

// scale scales the 16x16 region represented by the 4 src blocks to the 8x8
// dst block.
func scale( *block,  *[4]block) {
	for  := 0;  < 4; ++ {
		 := (&2)<<4 | (&1)<<2
		for  := 0;  < 4; ++ {
			for  := 0;  < 4; ++ {
				 := 16* + 2*
				 := [][] + [][+1] + [][+8] + [][+9]
				[8*++] = ( + 2) >> 2
			}
		}
	}
}

// sosHeaderY is the SOS marker "\xff\xda" followed by 8 bytes:
//	- the marker length "\x00\x08",
//	- the number of components "\x01",
//	- component 1 uses DC table 0 and AC table 0 "\x01\x00",
//	- the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for
//	  sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al)
//	  should be 0x00, 0x3f, 0x00<<4 | 0x00.
var sosHeaderY = []byte{
	0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00,
}

// sosHeaderYCbCr is the SOS marker "\xff\xda" followed by 12 bytes:
//	- the marker length "\x00\x0c",
//	- the number of components "\x03",
//	- component 1 uses DC table 0 and AC table 0 "\x01\x00",
//	- component 2 uses DC table 1 and AC table 1 "\x02\x11",
//	- component 3 uses DC table 1 and AC table 1 "\x03\x11",
//	- the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for
//	  sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al)
//	  should be 0x00, 0x3f, 0x00<<4 | 0x00.
var sosHeaderYCbCr = []byte{
	0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02,
	0x11, 0x03, 0x11, 0x00, 0x3f, 0x00,
}

// writeSOS writes the StartOfScan marker.
func ( *encoder) ( image.Image) {
	switch .(type) {
	case *image.Gray:
		.write(sosHeaderY)
	default:
		.write(sosHeaderYCbCr)
	}
	var (
		// Scratch buffers to hold the YCbCr values.
		// The blocks are in natural (not zig-zag) order.
		      block
		,  [4]block
		// DC components are delta-encoded.
		, ,  int32
	)
	 := .Bounds()
	switch m := .(type) {
	// TODO(wathiede): switch on m.ColorModel() instead of type.
	case *image.Gray:
		for  := .Min.Y;  < .Max.Y;  += 8 {
			for  := .Min.X;  < .Max.X;  += 8 {
				 := image.Pt(, )
				grayToY(, , &)
				 = .writeBlock(&, 0, )
			}
		}
	default:
		,  := .(*image.RGBA)
		,  := .(*image.YCbCr)
		for  := .Min.Y;  < .Max.Y;  += 16 {
			for  := .Min.X;  < .Max.X;  += 16 {
				for  := 0;  < 4; ++ {
					 := ( & 1) * 8
					 := ( & 2) * 4
					 := image.Pt(+, +)
					if  != nil {
						rgbaToYCbCr(, , &, &[], &[])
					} else if  != nil {
						yCbCrToYCbCr(, , &, &[], &[])
					} else {
						toYCbCr(, , &, &[], &[])
					}
					 = .writeBlock(&, 0, )
				}
				scale(&, &)
				 = .writeBlock(&, 1, )
				scale(&, &)
				 = .writeBlock(&, 1, )
			}
		}
	}
	// Pad the last byte with 1's.
	.emit(0x7f, 7)
}

// DefaultQuality is the default quality encoding parameter.
const DefaultQuality = 75

// Options are the encoding parameters.
// Quality ranges from 1 to 100 inclusive, higher is better.
type Options struct {
	Quality int
}

// Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given
// options. Default parameters are used if a nil *Options is passed.
func ( io.Writer,  image.Image,  *Options) error {
	 := .Bounds()
	if .Dx() >= 1<<16 || .Dy() >= 1<<16 {
		return errors.New("jpeg: image is too large to encode")
	}
	var  encoder
	if ,  := .(writer);  {
		.w = 
	} else {
		.w = bufio.NewWriter()
	}
	// Clip quality to [1, 100].
	 := DefaultQuality
	if  != nil {
		 = .Quality
		if  < 1 {
			 = 1
		} else if  > 100 {
			 = 100
		}
	}
	// Convert from a quality rating to a scaling factor.
	var  int
	if  < 50 {
		 = 5000 / 
	} else {
		 = 200 - *2
	}
	// Initialize the quantization tables.
	for  := range .quant {
		for  := range .quant[] {
			 := int(unscaledQuant[][])
			 = (* + 50) / 100
			if  < 1 {
				 = 1
			} else if  > 255 {
				 = 255
			}
			.quant[][] = uint8()
		}
	}
	// Compute number of components based on input image type.
	 := 3
	switch .(type) {
	// TODO(wathiede): switch on m.ColorModel() instead of type.
	case *image.Gray:
		 = 1
	}
	// Write the Start Of Image marker.
	.buf[0] = 0xff
	.buf[1] = 0xd8
	.write(.buf[:2])
	// Write the quantization tables.
	.writeDQT()
	// Write the image dimensions.
	.writeSOF0(.Size(), )
	// Write the Huffman tables.
	.writeDHT()
	// Write the image data.
	.writeSOS()
	// Write the End Of Image marker.
	.buf[0] = 0xff
	.buf[1] = 0xd9
	.write(.buf[:2])
	.flush()
	return .err
}