github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/jxr/cgo.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package jxr
     6  
     7  /*
     8  #cgo windows,amd64 LDFLAGS: -L. -l"jxr-cgo-win64"
     9  #cgo windows,386 LDFLAGS: -L. -l"jxr-cgo-win32"
    10  #cgo linux,amd64 LDFLAGS: -L. -l"jxr-cgo-posix64"
    11  #cgo linux,386 LDFLAGS: -L. -l"jxr-cgo-posix32"
    12  
    13  #cgo windows CFLAGS: -I./jxrlib/include -fno-stack-check -fno-stack-protector -mno-stack-arg-probe
    14  #cgo linux   CFLAGS: -I./jxrlib/include
    15  
    16  #include "jxr.h"
    17  */
    18  import "C"
    19  import (
    20  	"fmt"
    21  	"unsafe"
    22  )
    23  
    24  type jxr_bool_t C.jxr_bool_t
    25  
    26  const (
    27  	jxr_true  = jxr_bool_t(C.jxr_true)
    28  	jxr_false = jxr_bool_t(C.jxr_false)
    29  )
    30  
    31  type jxr_data_type_t C.jxr_data_type_t
    32  
    33  const (
    34  	jxr_unsigned = jxr_data_type_t(C.jxr_unsigned)
    35  	jxr_signed   = jxr_data_type_t(C.jxr_signed)
    36  	jxr_float    = jxr_data_type_t(C.jxr_float)
    37  )
    38  
    39  func jxr_decode_config(data []byte) (
    40  	width, height, channels, depth C.int,
    41  	data_type jxr_data_type_t,
    42  	err error,
    43  ) {
    44  	if len(data) == 0 {
    45  		err = fmt.Errorf("jxr_decode_config: bad arguments")
    46  		return
    47  	}
    48  	rv := jxr_bool_t(C.jxr_decode_config(
    49  		(*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)),
    50  		(*C.int)(&width), (*C.int)(&height), (*C.int)(&channels), (*C.int)(&depth),
    51  		(*C.jxr_data_type_t)(&data_type),
    52  	))
    53  	if rv != jxr_true {
    54  		err = fmt.Errorf("jxr_decode_config: failed")
    55  		return
    56  	}
    57  	return
    58  }
    59  
    60  func jxr_decode(data, pix []byte, stride int) (
    61  	width, height, channels, depth C.int,
    62  	data_type jxr_data_type_t,
    63  	err error,
    64  ) {
    65  	if len(data) == 0 {
    66  		err = fmt.Errorf("jxr_decode: bad arguments")
    67  		return
    68  	}
    69  	rv := jxr_bool_t(C.jxr_decode(
    70  		(*C.char)(unsafe.Pointer(&pix[0])), C.int(len(pix)), C.int(stride),
    71  		(*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)),
    72  		(*C.int)(&width), (*C.int)(&height), (*C.int)(&channels), (*C.int)(&depth),
    73  		(*C.jxr_data_type_t)(&data_type),
    74  	))
    75  	if rv != jxr_true {
    76  		err = fmt.Errorf("jxr_decode: failed")
    77  		return
    78  	}
    79  	return
    80  }
    81  
    82  func jxr_encode_len(
    83  	pix []byte, stride int,
    84  	width, height, channels, depth, quality int,
    85  	data_type jxr_data_type_t,
    86  ) (size C.int, err error) {
    87  	if len(pix) == 0 {
    88  		err = fmt.Errorf("jxr_encode_len: bad arguments")
    89  		return
    90  	}
    91  	rv := jxr_bool_t(C.jxr_encode_len(
    92  		(*C.char)(unsafe.Pointer(&pix[0])), C.int(len(pix)), C.int(stride),
    93  		(C.int)(width), (C.int)(height), (C.int)(channels), (C.int)(depth), C.int(quality),
    94  		(C.jxr_data_type_t)(data_type),
    95  		&size,
    96  	))
    97  	if rv != jxr_true {
    98  		err = fmt.Errorf("jxr_encode_len: failed")
    99  		return
   100  	}
   101  	return
   102  }
   103  
   104  func jxr_encode(
   105  	buf, pix []byte, stride int,
   106  	width, height, channels, depth, quality int,
   107  	data_type jxr_data_type_t,
   108  ) (newSize C.int, err error) {
   109  	if len(buf) == 0 || len(pix) == 0 {
   110  		err = fmt.Errorf("jxr_encode: bad arguments")
   111  		return
   112  	}
   113  	rv := jxr_bool_t(C.jxr_encode(
   114  		(*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)),
   115  		(*C.char)(unsafe.Pointer(&pix[0])), C.int(len(pix)), C.int(stride),
   116  		C.int(width), C.int(height), C.int(channels), C.int(depth), C.int(quality),
   117  		C.jxr_data_type_t(data_type),
   118  		&newSize,
   119  	))
   120  	if rv != jxr_true {
   121  		err = fmt.Errorf("jxr_encode_len: failed")
   122  		return
   123  	}
   124  	return
   125  }