duai0935 2016-10-19 21:55
浏览 42
已采纳

了解gometalinter取消转换不必要的转换警告

warning: unnecessary conversion (unconvert)

This comes from the following line:

offsetY += 60 + image.Image(*img.Bitmap).Bounds().Max.Y

It took me a while to understand how to convert this interface pointer to an interface yet I do not think that this is the right solution since gometalinter raises a warning.

I want to get the width of the img. img is of struct Image and has a Bitmap pointer to a real image.Image (image stdlib). If I want to call Bounds on the actual image.Image I need to transform the pointer to the interface into an interface.

How should that be done in a more go friendly way?

I have the following go code:

import (
    "image"
    "image/color"
    "image/draw"
)
type Image struct {
    Src    string
    Title  string
    Width  int
    Height int
    Index  int
    Bitmap *image.Image
}

type Images []Image

offsetY = 10
func ComposeImage(imgs Images) image.Image {
    masterRGBAImg := image.NewRGBA(image.Rect(0, 0, 300, 300))
    masterBounds := masterRGBAImg.Bounds()

    for _, img := range imgs {
        draw.Draw(masterRGBAImg, masterBounds,
            *img.Bitmap, image.Point{X: -10, Y: -offsetY + 10}, draw.Src)


        addLabel(masterRGBAImg, 10, offsetY-30, img.Title)

        // HERE ======
        offsetY += 60 + image.Image(*img.Bitmap).Bounds().Max.Y
        // END ======

    }
    return masterRGBAImg
}

// Draw label on image.
func addLabel(img *image.RGBA, x int, y int, label string) {
    col := color.RGBA{50, 50, 50, 255}
    point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}

    d := &font.Drawer{
        Dst:  img,
        Src:  image.NewUniform(col),
        Face: inconsolata.Bold8x16,
        Dot:  point,
    }
    d.DrawString(label)
}
  • 写回答

1条回答 默认 最新

  • dounanyin3179 2016-10-19 22:01
    关注

    First, the bitmap field is a *image.Image, so it's of the same type and you don't need to convert it when you could just dereference it.

    (*img.Bitmap).Bounds()
    

    However, image.Image is an interface. A pointer to an interface is almost always a programming error.

    Change your struct definition to

    type Image struct {
        Src    string
        Title  string
        Width  int
        Height int
        Index  int
        Bitmap image.Image
    }
    

    and you can then just call img.Bitmap.Bounds() directly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊