Skip to content

Commit

Permalink
add invert func
Browse files Browse the repository at this point in the history
  • Loading branch information
octu0 committed Mar 26, 2021
1 parent 19449cd commit 450fd51
Show file tree
Hide file tree
Showing 41 changed files with 363 additions and 27 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ img, err := blurry.Grayscale(input)

![example](testdata/grayscale.png)

### Invert

```go
img, err := blurry.Invert(input)
```

![example](testdata/invert.png)

### Brightness

```go
Expand Down Expand Up @@ -123,7 +131,7 @@ USAGE:
blurry [global options] command [command options] [arguments...]
VERSION:
1.2.0
1.3.0
COMMANDS:
blockmozaic
Expand All @@ -135,6 +143,7 @@ COMMANDS:
gamma
gaussianblur
grayscale
invert
sobel
help, h Shows a list of commands or help for one command
Expand All @@ -160,25 +169,27 @@ darwin/amd64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
realize benchmark...
w/ src 320x240
benchmarking cloneimg...
cloneimg: 0.0195515ms
cloneimg: 0.0185758ms
benchmarking grayscale...
grayscale: 0.119545ms
grayscale: 0.118637ms
benchmarking invert...
invert: 0.060455ms
benchmarking brightness...
brightness: 0.0781598ms
brightness: 0.0845308ms
benchmarking gammacorrection...
gammacorrection: 0.18429ms
gammacorrection: 0.125641ms
benchmarking contrast...
contrast: 0.139594ms
contrast: 0.0912185ms
benchmarking boxblur...
boxblur: 0.345168ms
boxblur: 0.333382ms
benchmarking gaussianblur...
gaussianblur: 0.351843ms
gaussianblur: 0.325328ms
benchmarking edge...
edge: 0.105334ms
edge: 0.105413ms
benchmarking sobel...
sobel: 0.122694ms
sobel: 0.157796ms
benchmarking blockmozaic...
blockmozaic: 0.376637ms
blockmozaic: 0.365575ms
```

### Blur
Expand Down
71 changes: 71 additions & 0 deletions blurry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ Func grayscale_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
return grayscale;
}

Func invert_fn(Func input, Param<int32_t> width, Param<int32_t> height) {
Region src_bounds = {{0, width},{0, height},{0, 4}};
Func in = read(BoundaryConditions::repeat_edge(input, src_bounds), "in");

Var x("x"), y("y"), ch("ch");
Var xo("xo"), xi("xi");
Var yo("yo"), yi("yi");
Var ti("ti");

Func invert = Func("invert");
Expr value = select(
ch == 3, in(x, y, ch), // alpha
255 - in(x, y, ch) // r g b
);
invert(x, y, ch) = cast<uint8_t>(value);

invert.compute_root()
.tile(x, y, xo, yo, xi, yi, 32, 32)
.fuse(xo, yo, ti)
.parallel(ch)
.parallel(ti)
.vectorize(xi, 32);

return invert;
}

Func brightness_fn(Func input, Param<int32_t> width, Param<int32_t> height, Param<float> factor) {
Region src_bounds = {{0, width},{0, height},{0, 4}};
Func in = read(BoundaryConditions::repeat_edge(input, src_bounds), "in");
Expand Down Expand Up @@ -557,6 +583,26 @@ void generate_grayscale(std::vector<Target::Feature> features) {
}, fn.name());
}

void generate_invert(std::vector<Target::Feature> features) {
ImageParam src(type_of<uint8_t>(), 3);

Param<int32_t> width{"width", 1920};
Param<int32_t> height{"height", 1080};

init_input_rgba(src);

Func fn = invert_fn(
src.in(), width, height
);

init_output_rgba(fn.output_buffer());

printf("generate %s\n", fn.name().c_str());
generate_static_link(features, fn, {
src, width, height,
}, fn.name());
}

void generate_brightness(std::vector<Target::Feature> features) {
ImageParam src(type_of<uint8_t>(), 3);

Expand Down Expand Up @@ -727,6 +773,7 @@ void generate(std::vector<Target::Feature> features){
generate_runtime(features);
generate_cloneimg(features);
generate_grayscale(features);
generate_invert(features);
generate_brightness(features);
generate_gamma(features);
generate_contrast(features);
Expand Down Expand Up @@ -804,6 +851,25 @@ int main(int argc, char **argv) {
save_image(out, argv[3]);
return 0;
}
if(strcmp(argv[1], "invert") == 0) {
Buffer<uint8_t> buf_src = load_and_convert_image(argv[2]);

Param<int32_t> width{"width", buf_src.get()->width()};
Param<int32_t> height{"height", buf_src.get()->height()};

Func fn = invert_fn(
wrapFunc(buf_src, "buf_src"), width, height
);
fn.compile_jit(get_jit_target_from_environment());

printf("realize %s...\n", fn.name().c_str());

Buffer<uint8_t> out = fn.realize({buf_src.get()->width(), buf_src.get()->height(), 3});

printf("save to %s\n", argv[3]);
save_image(out, argv[3]);
return 0;
}
if(strcmp(argv[1], "brightness") == 0) {
Buffer<uint8_t> buf_src = load_and_convert_image(argv[2]);

Expand Down Expand Up @@ -986,6 +1052,11 @@ int main(int argc, char **argv) {
wrapFunc(buf_src, "buf_src"), width, height
), buf_src);
}
{
benchmark(invert_fn(
wrapFunc(buf_src, "buf_src"), width, height
), buf_src);
}
{
Param<float> factor{"factor", 1.5f};
benchmark(brightness_fn(
Expand Down
42 changes: 42 additions & 0 deletions cli/bridge/invert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bridge

import (
"log"

"gopkg.in/urfave/cli.v1"

"github.com/octu0/blurry"
)

func invertAction(c *cli.Context) error {
in, err := loadImage(c.String("input"))
if err != nil {
return err
}

out, err := blurry.Invert(in)
if err != nil {
return err
}

path, err := saveImage(out)
if err != nil {
return err
}
log.Printf("info: open %s", path)
return nil
}

func init() {
addCommand(cli.Command{
Name: "invert",
Action: invertAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "i,input",
Usage: "/path/to/input image",
Value: "./testdata/src.png",
},
},
})
}
43 changes: 43 additions & 0 deletions cli/genrun/invert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package genrun

import (
"gopkg.in/urfave/cli.v1"
)

func invertAction(c *cli.Context) error {
runtimePath := c.String("runtime")
generateOutFilePath, err := generate(runtimePath, c.String("file"))
if err != nil {
return err
}

if err := runLocal(runtimePath, generateOutFilePath, c.String("input"), "invert", nil); err != nil {
return err
}

return nil
}

func init() {
addCommand(cli.Command{
Name: "invert",
Action: invertAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "i,input",
Usage: "/path/to/input image",
Value: "./testdata/src.png",
},
cli.StringFlag{
Name: "r,runtime",
Usage: "halide runtime path",
Value: "./Halide-Runtime",
},
cli.StringFlag{
Name: "f,file",
Usage: "/path/to/blurry.cpp path",
Value: "./blurry.cpp",
},
},
})
}
57 changes: 57 additions & 0 deletions invert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package blurry

/*
#cgo CFLAGS: -I${SRCDIR}
#cgo darwin LDFLAGS: -L${SRCDIR} -lruntime_osx -linvert_osx -ldl -lm
#cgo linux LDFLAGS: -L${SRCDIR} -lruntime_linux -linvert_linux -ldl -lm
#include <stdlib.h>
#include <string.h>
#include "bridge.h"
#ifdef __APPLE__
#include "libinvert_osx.h"
#elif __linux__
#include "libinvert_linux.h"
#endif
int libinvert(unsigned char *src, int32_t width, int32_t height, unsigned char *out) {
halide_buffer_t *in_rgba_buf = create_rgba_buffer(src, width, height);
if(in_rgba_buf == NULL){
return 1;
}
halide_buffer_t *out_rgba_buf = create_rgba_buffer(out, width, height);
if(out_rgba_buf == NULL){
return 1;
}
int ret = invert(in_rgba_buf, width, height, out_rgba_buf);
free_buf(in_rgba_buf);
free_buf(out_rgba_buf);
return ret;
}
*/
import "C"
import (
"errors"
"image"
)

var (
ErrInvert = errors.New("invert cgo call error")
)

func Invert(img *image.RGBA) (*image.RGBA, error) {
width, height := wh(img)
out := GetRGBA(width, height)

ret := C.libinvert(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
(*C.uchar)(&out.Pix[0]),
)
if int(ret) != 0 {
return nil, ErrInvert
}
return out, nil
}
Binary file modified libblockmozaic_linux.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libblockmozaic_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int blockmozaic(struct halide_buffer_t *_p9_buffer, int32_t _width, int32_t _height, int32_t _block, struct halide_buffer_t *_blockmozaic_buffer);
int blockmozaic(struct halide_buffer_t *_p10_buffer, int32_t _width, int32_t _height, int32_t _block, struct halide_buffer_t *_blockmozaic_buffer);

HALIDE_FUNCTION_ATTRS
int blockmozaic_argv(void **args);
Expand Down
Binary file modified libblockmozaic_osx.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libblockmozaic_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int blockmozaic(struct halide_buffer_t *_p9_buffer, int32_t _width, int32_t _height, int32_t _block, struct halide_buffer_t *_blockmozaic_buffer);
int blockmozaic(struct halide_buffer_t *_p10_buffer, int32_t _width, int32_t _height, int32_t _block, struct halide_buffer_t *_blockmozaic_buffer);

HALIDE_FUNCTION_ATTRS
int blockmozaic_argv(void **args);
Expand Down
Binary file modified libboxblur_linux.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libboxblur_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int boxblur(struct halide_buffer_t *_p5_buffer, int32_t _width, int32_t _height, uint8_t _size, struct halide_buffer_t *_boxblur_buffer);
int boxblur(struct halide_buffer_t *_p6_buffer, int32_t _width, int32_t _height, uint8_t _size, struct halide_buffer_t *_boxblur_buffer);

HALIDE_FUNCTION_ATTRS
int boxblur_argv(void **args);
Expand Down
Binary file modified libboxblur_osx.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libboxblur_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int boxblur(struct halide_buffer_t *_p5_buffer, int32_t _width, int32_t _height, uint8_t _size, struct halide_buffer_t *_boxblur_buffer);
int boxblur(struct halide_buffer_t *_p6_buffer, int32_t _width, int32_t _height, uint8_t _size, struct halide_buffer_t *_boxblur_buffer);

HALIDE_FUNCTION_ATTRS
int boxblur_argv(void **args);
Expand Down
Binary file modified libbrightness_linux.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libbrightness_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int brightness(struct halide_buffer_t *_p2_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_brightness_buffer);
int brightness(struct halide_buffer_t *_p3_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_brightness_buffer);

HALIDE_FUNCTION_ATTRS
int brightness_argv(void **args);
Expand Down
Binary file modified libbrightness_osx.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libbrightness_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int brightness(struct halide_buffer_t *_p2_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_brightness_buffer);
int brightness(struct halide_buffer_t *_p3_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_brightness_buffer);

HALIDE_FUNCTION_ATTRS
int brightness_argv(void **args);
Expand Down
Binary file modified libcontrast_linux.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libcontrast_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int contrast(struct halide_buffer_t *_p4_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_contrast_buffer);
int contrast(struct halide_buffer_t *_p5_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_contrast_buffer);

HALIDE_FUNCTION_ATTRS
int contrast_argv(void **args);
Expand Down
Binary file modified libcontrast_osx.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libcontrast_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#endif

HALIDE_FUNCTION_ATTRS
int contrast(struct halide_buffer_t *_p4_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_contrast_buffer);
int contrast(struct halide_buffer_t *_p5_buffer, int32_t _width, int32_t _height, float _factor, struct halide_buffer_t *_contrast_buffer);

HALIDE_FUNCTION_ATTRS
int contrast_argv(void **args);
Expand Down
Binary file modified libedge_linux.a
Binary file not shown.
Loading

0 comments on commit 450fd51

Please sign in to comment.