Zooming the image in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Developed by Google, the simple, efficient, open-source go language is becoming a favorite language. It is optimized for programming of multiprocessor system applications, making go-compiled programs comparable to the speed of C or C + + code, and more secure and support parallel processes. The go language supports Windows, Apple Mac OS X, Linux and FreeBSD operating systems on the GO1 version. Go supports object-oriented, with features such as true encapsulation (closures) and reflection (reflection).

In terms of learning curve, Parker thinks go is similar to Java, and it should be easy for Java developers to learn go. Similarly, it is easy for C # developers to learn the go language.

The author is interested in the research of graphic image, let us look at the solution of zooming the image in the go language:


Package Resize


Import (
"Image"
"Image/color"
)


Average convert the sums to averages and returns the result.
Func average (sum []uint64, W, h int, n UInt64) *image. RGBA {
RET: = image. Newrgba (image. Rect (0, 0, W, h))
For y: = 0; Y < H; y++ {
For x: = 0; x < W; X + + {
Index: = 4 * (y*w + x)
PIX: = Ret. Pix[y*ret. Stride+x*4:]
Pix[0] = uint8 (sum[index+0]/N)
PIX[1] = uint8 (sum[index+1]/N)
PIX[2] = uint8 (sum[index+2]/N)
PIX[3] = uint8 (sum[index+3]/N)
}
}
return ret
}


Resizergba returns a scaled copy of the RGBA image slice R of M.
The returned image has width W and height H.
Func Resizergba (M *image. RGBA, R image. Rectangle, W, h int) *image. RGBA {
WW, hh: = UInt64 (W), UInt64 (h)
DX, dy: = UInt64 (R.DX ()), UInt64 (R.dy ())
See comment in Resize.
N, Sum: = Dx*dy, make ([]uint64, 4*w*h)
For y: = R.MIN.Y; Y < r.max.y; y++ {
PIX: = m.pix[(y-r. MIN.Y) *m.stride:]
For x: = r.min.x; x < r.max.x; X + + {
Get the source pixel.
P: = pix[(x-r. min.x) * 4:]
R64: = UInt64 (P[0])
G64: = UInt64 (p[1])
B64: = UInt64 (p[2])
A64: = UInt64 (P[3])
Spread the source pixel over 1 or more destination rows.
PY: = UInt64 (y) * hh
For remy: = HH; Remy > 0; {
QY: = dy-(py% dy)
If qy > Remy {
QY = Remy
}
Spread the source pixel over 1 or more destination columns.
PX: = UInt64 (x) * WW
Index: = 4 * ((Py/dy) *WW + (PX/DX))
For remx: = WW; Remx > 0; {
QX: = DX-(px% dx)
If QX > Remx {
QX = Remx
}
Qxy: = QX * QY
Sum[index+0] + = R64 * Qxy
Sum[index+1] + = g64 * Qxy
SUM[INDEX+2] + = b64 * Qxy
SUM[INDEX+3] + = A64 * Qxy
Index + = 4
px + = QX
REMX-= QX
}
PY + = QY
Remy-= qy
}
}
}
return average (SUM, W, h, N)
}


Resizenrgba returns a scaled copy of the RGBA image slice R of M.
The returned image has width W and height H.
Func Resizenrgba (M *image. Nrgba, R image. Rectangle, W, h int) *image. RGBA {
WW, hh: = UInt64 (W), UInt64 (h)
DX, dy: = UInt64 (R.DX ()), UInt64 (R.dy ())
See comment in Resize.
N, Sum: = Dx*dy, make ([]uint64, 4*w*h)
For y: = R.MIN.Y; Y < r.max.y; y++ {
PIX: = m.pix[(y-r. MIN.Y) *m.stride:]
For x: = r.min.x; x < r.max.x; X + + {
Get the source pixel.
P: = pix[(x-r. min.x) * 4:]
R64: = UInt64 (P[0])
G64: = UInt64 (p[1])
B64: = UInt64 (p[2])
A64: = UInt64 (P[3])
R64 = (R64 * a64)/255
g64 = (G64 * a64)/255
b64 = (B64 * a64)/255
Spread the source pixel over 1 or more destination rows.
PY: = UInt64 (y) * hh
For remy: = HH; Remy > 0; {
QY: = dy-(py% dy)
If qy > Remy {
QY = Remy
}
Spread the source pixel over 1 or more destination columns.
PX: = UInt64 (x) * WW
Index: = 4 * ((Py/dy) *WW + (PX/DX))
For remx: = WW; Remx > 0; {
QX: = DX-(px% dx)
If QX > Remx {
QX = Remx
}
Qxy: = QX * QY
Sum[index+0] + = R64 * Qxy
Sum[index+1] + = g64 * Qxy
SUM[INDEX+2] + = b64 * Qxy
SUM[INDEX+3] + = A64 * Qxy
Index + = 4
px + = QX
REMX-= QX
}
PY + = QY
Remy-= qy
}
}
}
return average (SUM, W, h, N)
}


Resample returns a resampled copy of the image slice R of M.
The returned image has width W and height H.
Func resample (M image. Image, R image. Rectangle, W, h int) *image. RGBA {
If w < 0 | | H < 0 {
return Nil
}
if w = = 0 | | H = = 0 | | R.DX () <= 0 | | R.dy () <= 0 {
return image. Newrgba (image. Rect (0, 0, W, h))
}
CURW, Curh: = R.dx (), R.dy ()
IMG: = image. Newrgba (image. Rect (0, 0, W, h))
For y: = 0; Y < H; y++ {
For x: = 0; x < W; X + + {
Get a source pixel.
SUBX: = x * curw/w
Suby: = y * curh/h
R32, G32, B32, A32: = m.at (SUBX, Suby). RGBA ()
r: = uint8 (R32 >> 8)
G: = Uint8 (g32 >> 8)
B: = uint8 (B32 >> 8)
A: = Uint8 (A32 >> 8)
Img. Setrgba (x, y, color. Rgba{r, G, B, a})
}
}
return img

}

(Longgang Minsheng Network http://www.lgms.net)

With the basics above, the following explains the use of the go language to upload images and generate thumbnails.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.