func (l LabelMap) DecodeImageFromImageSegment(bmpImage image.Image, transparentBackground bool) (image.Image, error) {
	// Map from the color code to the Label, with all of its attached
	// information
	colorLabels := make(map[uint32]Label)

	outputImage := image.NewRGBA(bmpImage.Bounds())

	for y := 0; y < bmpImage.Bounds().Max.Y; y++ {
		for x := 0; x < bmpImage.Bounds().Max.X; x++ {

			// Find the color at this pixel
			c := bmpImage.At(x, y)

			// Find the color channel values for this pixel
			pr, pg, pb, a := c.RGBA()

			// Confirm that we're mapping ID 1 => #010101, etc
			if pr != pg || pg != pb || pr != pb {
				return nil, fmt.Errorf("Encoding expected to have equal values for R, G, and B. Instead, found %d, %d, %d", pr, pg, pb)
			}

			// Create the hex string representation. Since each color channel is
			// "alpha-premultiplied" (https://golang.org/pkg/image/color/#RGBA),
			// we need to divide by alpha (scaling 0-1), then multiplying by
			// 255, to get what we're actually looking for
			pixelID := uint32(math.Round(255 * float64(pr) / float64(a)))

			// If we haven't yet mapped this point's color to a Label
			// identifier, do so now:
			if _, exists := colorLabels[pixelID]; !exists {

				// Look up the ID
				for _, v := range l.Sorted() {
					if uint32(v.ID) == pixelID {
						colorLabels[pixelID] = v
						break
					} else if pixelID == 0 {
						// Background, special case
						colorLabels[pixelID] = v
						break
					}
				}
			}

			// Make sure that all labels are known
			lab, exists := colorLabels[pixelID]
			if !exists {
				return nil, pfx.Err(fmt.Errorf("Saw color %v (ID %v) but could not find this color in the label map %+v", c, pixelID, l))
			}

			// For human vision, we want the background to be special-cased to
			// transparent, and the pixels otherwise to be fully opaque.
			// However, if we are running in overlay-only mode, then the special
			// cased color will be set to full opacity rather than transparent
			// (to facilitate gif creation)
			var opacity uint8 = 255
			if transparentBackground && (lab.ID == 0 || lab.Color == SpecialTransparentColor) {
				opacity = 0
			}
			humanColor, err := rgbaFromColorCode(lab.Color)
			if err != nil {
				return nil, err
			}

			hr, hg, hb, _ := humanColor.RGBA()

			outputImage.Set(x, y, color.RGBA{
				R: uint8(hr),
				G: uint8(hg),
				B: uint8(hb),
				A: opacity,
			})
		}
	}

	return outputImage, nil
}
// DecodeImageFromImageSegment consumes an ID-encoded image (where each pixel is
// #010101 for ID 1, #020202 for ID 2 etc), and transforms it into a
// human-visible value based on the colors for those IDs assigned in the config
// file. It special-cases ID 0 (the background) and color value #000001 to be
// transparent. However, if transparentBackground is false, then the background
// is set to black.
DecodeImageFromImageSegment consumes an ID-encoded image (where each pixel is
#010101 for ID 1, #020202 for ID 2 etc), and transforms it into a
human-visible value based on the colors for those IDs assigned in the config
file. It special-cases ID 0 (the background) and color value #000001 to be
transparent. However, if transparentBackground is false, then the background
is set to black.
// DecodeImageFromImageSegment consumes an ID - encoded image ( where each pixel is // #010101 for ID 1 #020202 for ID 2 etc ) and transforms it into a // human - visible value based on the colors for those IDs assigned in the config // file . It special - cases ID 0 ( the background ) and color value #000001 to be // transparent . However if transparentBackground is false then the background // is set to black .
func (l LabelMap) DecodeImageFromImageSegment(bmpImage image.Image, transparentBackground bool) (image.Image, error) {
	colorLabels := make(map[uint32]Label)
	outputImage := image.NewRGBA(bmpImage.Bounds())
	for y := 0; y < bmpImage.Bounds().Max.Y; y++ {
		for x := 0; x < bmpImage.Bounds().Max.X; x++ {
			c := bmpImage.At(x, y)
			pr, pg, pb, a := c.RGBA()
			if pr != pg || pg != pb || pr != pb {
				return nil, fmt.Errorf("Encoding expected to have equal values for R, G, and B. Instead, found %d, %d, %d", pr, pg, pb)
			}
			pixelID := uint32(math.Round(255 * float64(pr) / float64(a)))
			if _, exists := colorLabels[pixelID]; !exists {
				for _, v := range l.Sorted() {
					if uint32(v.ID) == pixelID {
						colorLabels[pixelID] = v
						break
					} else if pixelID == 0 {
						colorLabels[pixelID] = v
						break
					}
				}
			}
			lab, exists := colorLabels[pixelID]
			if !exists {
				return nil, pfx.Err(fmt.Errorf("Saw color %v (ID %v) but could not find this color in the label map %+v", c, pixelID, l))
			}
			var opacity uint8 = 255
			if transparentBackground && (lab.ID == 0 || lab.Color == SpecialTransparentColor) {
				opacity = 0
			}
			humanColor, err := rgbaFromColorCode(lab.Color)
			if err != nil {
				return nil, err
			}
			hr, hg, hb, _ := humanColor.RGBA()
			outputImage.Set(x, y, color.RGBA{
				R: uint8(hr),
				G: uint8(hg),
				B: uint8(hb),
				A: opacity,
			})
		}
	}
	return outputImage, nil
}
func ( l LabelMap ) DecodeImageFromImageSegment ( bmpImage image . Image , transparentBackground bool ) ( image . Image , error ) { colorLabels := make ( map [ uint32 ] Label ) outputImage := image . NewRGBA ( bmpImage . Bounds ( ) ) for y := 0 ; y < bmpImage . Bounds ( ) . Max . Y ; y ++ { for x := 0 ; x < bmpImage . Bounds ( ) . Max . X ; x ++ { c := bmpImage . At ( x , y ) pr , pg , pb , a := c . RGBA ( ) if pr != pg || pg != pb || pr != pb { return nil , fmt . Errorf ( " " , pr , pg , pb ) } pixelID := uint32 ( math . Round ( 255 * float64 ( pr ) / float64 ( a ) ) ) if _ , exists := colorLabels [ pixelID ] ; ! exists { for _ , v := range l . Sorted ( ) { if uint32 ( v . ID ) == pixelID { colorLabels [ pixelID ] = v break } else if pixelID == 0 { colorLabels [ pixelID ] = v break } } } lab , exists := colorLabels [ pixelID ] if ! exists { return nil , pfx . Err ( fmt . Errorf ( " " , c , pixelID , l ) ) } var opacity uint8 = 255 if transparentBackground && ( lab . ID == 0 || lab . Color == SpecialTransparentColor ) { opacity = 0 } humanColor , err := rgbaFromColorCode ( lab . Color ) if err != nil { return nil , err } hr , hg , hb , _ := humanColor . RGBA ( ) outputImage . Set ( x , y , color . RGBA { R : uint8 ( hr ) , G : uint8 ( hg ) , B : uint8 ( hb ) , A : opacity , } ) } } return outputImage , nil }
DecodeImageFromImageSegment consumes an ID-encoded image (where each pixel is
010101 for ID 1, #020202 for ID 2 etc), and transforms it into a
human-visible value based on the colors for those IDs assigned in the config
file.
DecodeImageFromImageSegment consumes an ID - encoded image ( where each pixel is 010101 for ID 1 #020202 for ID 2 etc ) and transforms it into a human - visible value based on the colors for those IDs assigned in the config file .
// Map from the color code to the Label, with all of its attached // information // Find the color at this pixel // Find the color channel values for this pixel // Confirm that we're mapping ID 1 => #010101, etc // Create the hex string representation. Since each color channel is // "alpha-premultiplied" (https://golang.org/pkg/image/color/#RGBA), // we need to divide by alpha (scaling 0-1), then multiplying by // 255, to get what we're actually looking for // If we haven't yet mapped this point's color to a Label // identifier, do so now: // Look up the ID // Background, special case // Make sure that all labels are known // For human vision, we want the background to be special-cased to // transparent, and the pixels otherwise to be fully opaque. // However, if we are running in overlay-only mode, then the special // cased color will be set to full opacity rather than transparent // (to facilitate gif creation)