Advertisement
Guest User

imgproc.go

a guest
Dec 11th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.42 KB | None | 0 0
  1. package imgproc
  2.  
  3. //#cgo pkg-config: GraphicsMagickWand
  4. //#cgo linux pkg-config: opencv
  5. ////XXX it is possible that some of cgo Warning flags are in conflict with cgo generated code.
  6. //// If that is the case, then remove appropriate -W... flags here.
  7. //// You can use -Wno-... format if problematic flag is implied with other flags (e.g. -Wall or -Wextra).
  8. //#cgo CPPFLAGS: -Wall -Wextra -Wpedantic -Wno-unused-parameter -Wno-unused-variable -Wstrict-overflow=5 -Wshadow -Wcast-qual -Wpointer-arith -Werror
  9. //#include <wand/magick_wand.h>
  10. //#include <opencv/cv.h>
  11. //#include <opencv2/imgproc/imgproc_c.h>
  12. //#include <opencv2/core/core_c.h>
  13. //
  14. //#include "imgproc-encode.h"
  15. //#include "fix_c_signals.h"
  16. //
  17. //
  18. // void gm_init() {
  19. //  InitializeMagick(NULL);
  20. //      cgo_fix_signals();
  21. //}
  22. import "C"
  23. import (
  24.     "errors"
  25.     "fmt"
  26.     "image"
  27.     "log"
  28.     "log/syslog"
  29.     "logger"
  30.     "math"
  31.     "os"
  32.     "unsafe"
  33.     //"image/draw"
  34.  
  35.     "common"
  36.     "proto"
  37. )
  38. .
  39. .
  40. .
  41. func checkGoImage(inImage image.Image) error {
  42.     inRect := inImage.Bounds()
  43.     if inRect.Min.X != 0 || inRect.Min.Y != 0 || inRect.Max.X <= 0 || inRect.Max.Y <= 0 {
  44.         return fmt.Errorf("cvFromGoImage: input Go image dimensions problem")
  45.     }
  46.     switch tmpIn := inImage.(type) {
  47.     case *image.RGBA:
  48.         if tmpIn.Stride < 4*inRect.Max.X {
  49.             return fmt.Errorf("checkGoImage: inImage.Stride size problem.")
  50.         }
  51.         if tmpIn.Pix == nil || tmpIn.Stride*inRect.Max.Y != len(tmpIn.Pix) {
  52.             return fmt.Errorf("checkGoImage: inImage.Pix buffer size problem.")
  53.         }
  54.     case *image.Gray:
  55.         if tmpIn.Stride < inRect.Max.X {
  56.             return fmt.Errorf("checkGoImage: inImage.Stride size problem.")
  57.         }
  58.         if tmpIn.Pix == nil || tmpIn.Stride*inRect.Max.Y != len(tmpIn.Pix) {
  59.             return fmt.Errorf("checkGoImage: inImage.Pix buffer size problem.")
  60.         }
  61.     }
  62.     return nil
  63. }
  64. .
  65. .
  66. .
  67. func cvFromGoImageNoCopy(inImage image.Image) (cvImage *C.IplImage, err error) {
  68.     inRect := inImage.Bounds()
  69.     if err := checkGoImage(inImage); err != nil {
  70.         return nil, err
  71.     }
  72.     w := inRect.Max.X
  73.     h := inRect.Max.Y
  74.     switch tmpIn := inImage.(type) {
  75.     case *image.RGBA:
  76.         cvImage = C.cvCreateImageHeader(C.cvSize(C.int(w), C.int(h)), C.IPL_DEPTH_8U, 4)
  77.         if cvImage == nil {
  78.             return nil, fmt.Errorf("cvFromGoImageNoCopy: OpenCv header creation fail")
  79.         }
  80.         C.cvSetData(unsafe.Pointer(cvImage), unsafe.Pointer(&tmpIn.Pix[0]), C.int(tmpIn.Stride))
  81.     case *image.Gray:
  82.         cvImage = C.cvCreateImageHeader(C.cvSize(C.int(w), C.int(h)), C.IPL_DEPTH_8U, 1)
  83.         if cvImage == nil {
  84.             return nil, fmt.Errorf("cvFromGoImageNoCopy: OpenCv header creation fail")
  85.         }
  86.         C.cvSetData(unsafe.Pointer(cvImage), unsafe.Pointer(&tmpIn.Pix[0]), C.int(tmpIn.Stride))
  87.     default:
  88.         err = fmt.Errorf("cvFromGoImageNoCopy: Unsupported format %T", tmpIn)
  89.     }
  90.     return
  91. }
  92. .
  93. .
  94. .
  95. type CvOrtoRotation int
  96.  
  97. const (
  98.     CvOrtoRotation0 CvOrtoRotation = iota
  99.     CvOrtoRotation90
  100.     CvOrtoRotation180
  101.     CvOrtoRotation270
  102. )
  103.  
  104. func CvRotateOrtoghonal(inImage image.Image, rotation CvOrtoRotation) (image.Image, error) {
  105.     var (
  106.         outImage   image.Image
  107.         cvIn       *C.IplImage
  108.         cvOutImage *C.IplImage
  109.         err        error
  110.     )
  111.  
  112.     cvIn, err = cvFromGoImageNoCopy(inImage)
  113.     defer C.cvReleaseImageHeader(&cvIn)
  114.     if err != nil {
  115.         return nil, err
  116.     }
  117.  
  118.     outW := inImage.Bounds().Max.X
  119.     outH := inImage.Bounds().Max.Y
  120.     if rotation == CvOrtoRotation90 || rotation == CvOrtoRotation270 {
  121.         outW, outH = outH, outW
  122.     }
  123.     outRect := image.Rect(0, 0, outW, outH)
  124.     switch tmpIn := inImage.(type) {
  125.     case *image.RGBA:
  126.         outImage = image.NewRGBA(outRect)
  127.     case *image.Gray:
  128.         outImage = image.NewGray(outRect)
  129.     default:
  130.         return nil, fmt.Errorf("CvRotateOrtoghonal: Unsupported format %T", tmpIn)
  131.     }
  132.     cvOutImage, err = cvFromGoImageNoCopy(outImage)
  133.     defer C.cvReleaseImageHeader(&cvOutImage)
  134.     if err != nil {
  135.         return nil, err
  136.     }
  137.  
  138.     if rotation == CvOrtoRotation90 || rotation == CvOrtoRotation270 {
  139.         C.cvTranspose(unsafe.Pointer(cvIn), unsafe.Pointer(cvOutImage))
  140.     }
  141.  
  142.     switch rotation {
  143.     case CvOrtoRotation0:
  144.         C.cvCopy(unsafe.Pointer(cvIn), unsafe.Pointer(cvOutImage), nil)
  145.     case CvOrtoRotation90:
  146.         C.cvFlip(unsafe.Pointer(cvOutImage), nil, C.int(CvFlipY))
  147.     case CvOrtoRotation180:
  148.         C.cvFlip(unsafe.Pointer(cvIn),
  149.             unsafe.Pointer(cvOutImage), C.int(CvFlipXY))
  150.     case CvOrtoRotation270:
  151.         C.cvFlip(unsafe.Pointer(cvOutImage), nil, C.int(CvFlipX))
  152.     default:
  153.         return nil, fmt.Errorf("CvRotateOrtoghonal unknown rotation %d", rotation)
  154.     }
  155.  
  156.     return outImage, nil
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement