// decodeConfigPPM reads and parses a PPM header, either "raw" (binary) or // "plain" (ASCII). func decodeConfigPPM(r io.Reader) (image.Config, error) { // We really want a bufio.Reader. If we were given one, use it. If // not, create a new one. br, ok := r.(*bufio.Reader) if !ok { br = bufio.NewReader(r) } nr := newNetpbmReader(br) // Parse the PPM header. header, ok := nr.GetNetpbmHeader() if !ok { err := nr.Err() if err == nil { err = errors.New("Invalid PPM header") } return image.Config{}, err } // Store and return the image configuration. var cfg image.Config cfg.Width = header.Width cfg.Height = header.Height if header.Maxval < 256 { cfg.ColorModel = npcolor.RGBMModel{M: uint8(header.Maxval)} } else { cfg.ColorModel = npcolor.RGBM64Model{M: uint16(header.Maxval)} } return cfg, nil }
// decodeConfigPBM reads and parses a PBM header, either "raw" (binary) or // "plain" (ASCII). func decodeConfigPBM(r io.Reader) (image.Config, error) { // We really want a bufio.Reader. If we were given one, use it. If // not, create a new one. br, ok := r.(*bufio.Reader) if !ok { br = bufio.NewReader(r) } nr := newNetpbmReader(br) // Parse the PBM header. header, ok := nr.GetNetpbmHeader() if !ok { err := nr.Err() if err == nil { err = errors.New("Invalid PBM header") } return image.Config{}, err } // Store the image configuration. var cfg image.Config cfg.Width = header.Width cfg.Height = header.Height // A PBM file's color map is 0=white, 1=black. colorMap := make(color.Palette, 2) colorMap[0] = color.RGBA{255, 255, 255, 255} colorMap[1] = color.RGBA{0, 0, 0, 255} cfg.ColorModel = colorMap return cfg, nil }
// DecodeConfig returns dimensions of imagefile image. func DecodeConfig(r io.Reader) (image.Config, error) { var cfg image.Config buff := make([]uint8, 17) _, err := io.ReadFull(r, buff) cfg.ColorModel = color.NRGBAModel be := binary.BigEndian cfg.Width = int(be.Uint32(buff[9:13])) cfg.Height = int(be.Uint32(buff[13:17])) return cfg, err }