如何在 Go 中從圖像中提取像素數組以獲取'texImage2D”?
Dec 06, 2024 pm 06:45 PM從Go 中的影像擷取像素陣列
問題:
從Go 中的影像中擷取像素數組位元組數組的形式對於將其傳遞給gl.Context 的texImage2D 方法是必要的。所需的像素數組由從左到右、從上到下的順序排列的 RGBA 值組成。
解決方案:
要取得像素數組,請執行以下操作可以採取以下步驟:
-
取得影像:
使用image.Decode(a) 從io.Reader(如os.File 或http.Request.Body)載入圖像。 -
取得單一像素:
使用img.At(x, y).RGBA() 擷取每個的RGBA 值Pixel. -
建構像素數組:
建構一個二維數組pixels來表示像素排列。迭代影像中的每個像素,將其 RGBA 值新增至對應的像素行。 -
轉換為8 位元表示:
由於RGBA 值是32 位元整數,將它們除以257 以獲得texImage2D 的8 位元表示
package main import ( "fmt" "image" "image/png" "os" "io" "net/http" ) func main() { // Register other formats as needed image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig) file, err := os.Open("./image.png") if err != nil { fmt.Println("Error: File could not be opened") os.Exit(1) } defer file.Close() pixels, err := getPixels(file) if err != nil { fmt.Println("Error: Image could not be decoded") os.Exit(1) } fmt.Println(pixels) } func getPixels(file io.Reader) ([][]Pixel, error) { img, _, err := image.Decode(file) if err != nil { return nil, err } bounds := img.Bounds() width, height := bounds.Max.X, bounds.Max.Y var pixels [][]Pixel for y := 0; y < height; y++ { var row []Pixel for x := 0; x < width; x++ { row = append(row, rgbaToPixel(img.At(x, y).RGBA())) } pixels = append(pixels, row) } return pixels, nil } func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) Pixel { return Pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)} } type Pixel struct { R int G int B int A int }
登入後複製
透過以下步驟,您可以有效地從影像中取得像素數組,以便與Go中的 texImage2D 方法一起使用。
以上是如何在 Go 中從圖像中提取像素數組以獲取'texImage2D”?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
兩個點博物館:邦格荒地地點指南
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
兩個點博物館:邦格荒地地點指南
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)