Skip to content

Commit 51c7e39

Browse files
authored
Merge pull request #25 from lian/master
DarknetImage move float conversion into c code
2 parents a18a9cf + 7e34b45 commit 51c7e39

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

image.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@ void set_data_f32_val(float* data, int index, float value) {
1111
data[index] = value;
1212
}
1313

14+
void to_float_and_fill_image(image* im, int w, int h, uint8_t* data) {
15+
int x, y, idx_source;
16+
int pixel_count = w * h;
17+
int idx = 0;
18+
19+
for (y = 0; y < h; y++) {
20+
for (x = 0; x < w; x++) {
21+
idx_source = (y*w + x) * 4;
22+
im->data[(pixel_count*0) + idx] = (float)data[idx_source] / 255;
23+
im->data[(pixel_count*1) + idx] = (float)data[idx_source+1] / 255;
24+
im->data[(pixel_count*2) + idx] = (float)data[idx_source+2] / 255;
25+
idx++;
26+
}
27+
}
28+
}

image.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,33 @@ import (
1414
type DarknetImage struct {
1515
Width int
1616
Height int
17-
ans []float32
1817
image C.image
1918
}
2019

2120
// Close and release resources.
2221
func (img *DarknetImage) Close() error {
2322
C.free_image(img.image)
24-
img.ans = nil
2523
return nil
2624
}
2725

28-
// https://stackoverflow.com/questions/33186783/get-a-pixel-array-from-from-golang-image-image/59747737#59747737
29-
func imgTofloat32(src image.Image) []float32 {
26+
func Image2Float32(src image.Image) (*DarknetImage, error) {
3027
bounds := src.Bounds()
31-
width, height := bounds.Max.X, bounds.Max.Y
32-
srcRGBA := image.NewRGBA(src.Bounds())
33-
draw.Copy(srcRGBA, image.Point{}, src, src.Bounds(), draw.Src, nil)
28+
srcRGBA := image.NewRGBA(bounds)
29+
draw.Copy(srcRGBA, image.Point{}, src, bounds, draw.Src, nil)
3430

35-
red := make([]float32, 0, width*height)
36-
green := make([]float32, 0, width*height)
37-
blue := make([]float32, 0, width*height)
38-
for y := 0; y < height; y++ {
39-
for x := 0; x < width; x++ {
40-
idxSource := (y*width + x) * 4
41-
pix := srcRGBA.Pix[idxSource : idxSource+4]
42-
rpix, gpix, bpix := float32(pix[0])/257.0, float32(pix[1])/257.0, float32(pix[2])/257.0
43-
red = append(red, rpix)
44-
green = append(green, gpix)
45-
blue = append(blue, bpix)
46-
}
47-
}
48-
srcRGBA = nil
49-
50-
ans := make([]float32, len(red)+len(green)+len(blue))
51-
copy(ans[:len(red)], red)
52-
copy(ans[len(red):len(red)+len(green)], green)
53-
copy(ans[len(red)+len(green):], blue)
54-
red = nil
55-
green = nil
56-
blue = nil
57-
return ans
31+
return ImageRGBA2Float32(srcRGBA)
5832
}
5933

6034
// Image2Float32 Returns []float32 representation of image.Image
61-
func Image2Float32(img image.Image) (*DarknetImage, error) {
62-
// ans := imgTofloat32(img)
35+
func ImageRGBA2Float32(img *image.RGBA) (*DarknetImage, error) {
6336
width := img.Bounds().Dx()
6437
height := img.Bounds().Dy()
6538
imgDarknet := &DarknetImage{
6639
Width: width,
6740
Height: height,
68-
ans: imgTofloat32(img),
6941
image: C.make_image(C.int(width), C.int(height), 3),
7042
}
71-
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, (*C.float)(unsafe.Pointer(&imgDarknet.ans[0])))
43+
C.to_float_and_fill_image(&imgDarknet.image, C.int(width), C.int(height), (*C.uint8_t)(unsafe.Pointer(&img.Pix[0])))
7244
return imgDarknet, nil
7345
}
7446

image.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#include <darknet.h>
44

55
extern void fill_image_f32(image *im, int w, int h, int c, float* data);
6-
extern void set_data_f32_val(float* data, int index, float value);
6+
extern void set_data_f32_val(float* data, int index, float value);
7+
extern void to_float_and_fill_image(image *im, int w, int h, uint8_t* data);

0 commit comments

Comments
 (0)