mirror of
https://github.com/Danile71/go-gpujpeg.git
synced 2025-09-26 19:41:18 +08:00
Init
This commit is contained in:
58
decoder.go
Normal file
58
decoder.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package gpujpeg
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lgpujpeg
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
#include "wrapper.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
decoder *C.gpujpeg_decoder
|
||||
}
|
||||
|
||||
func CreateDecoder() (*Decoder, error) {
|
||||
d := &Decoder{decoder: C.create_decoder()}
|
||||
if d.decoder == nil {
|
||||
return nil, errors.New("Can't create decoder")
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Init(param *Param, paramImage *ParamImage) {
|
||||
C.gpujpeg_decoder_init(d.decoder, ¶m.param, ¶mImage.param)
|
||||
}
|
||||
|
||||
func (d *Decoder) SetOutput() {
|
||||
C.gpujpeg_decoder_set_output_format(d.decoder, C.GPUJPEG_RGB,
|
||||
C.GPUJPEG_444_U8_P012)
|
||||
// or eg. GPUJPEG_YCBCR_JPEG and GPUJPEG_422_U8_P1020
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode(image []byte) (data []byte, err error) {
|
||||
var decoder_output C.gpujpeg_decoder_output
|
||||
C.gpujpeg_decoder_output_set_default(&decoder_output)
|
||||
|
||||
if C.gpujpeg_decoder_decode(d.decoder, (*C.uchar)(unsafe.Pointer(&image[0])), C.int(len(image)), &decoder_output) != C.int(0) {
|
||||
return nil, errors.New("Can't decode")
|
||||
}
|
||||
|
||||
data = make([]byte, int(decoder_output.data_size))
|
||||
|
||||
copy(data, *(*[]byte)(unsafe.Pointer(&decoder_output.data)))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Decoder) Free() {
|
||||
C.gpujpeg_decoder_destroy(d.decoder)
|
||||
}
|
23
gpujpeg.go
Normal file
23
gpujpeg.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package gpujpeg
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lgpujpeg
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
#include "wrapper.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func InitDevice(deviceId int) error {
|
||||
if C.gpujpeg_init_device(C.int(deviceId), 0) != C.int(0) {
|
||||
return errors.New("Can't init device")
|
||||
}
|
||||
return nil
|
||||
}
|
50
param.go
Normal file
50
param.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package gpujpeg
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lgpujpeg
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
#include "wrapper.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type Param struct {
|
||||
param C.gpujpeg_parameters
|
||||
}
|
||||
|
||||
func SetParam() *Param {
|
||||
p := &Param{}
|
||||
C.gpujpeg_set_default_parameters(&p.param)
|
||||
p.param.restart_interval = 1
|
||||
p.param.interleaved = 1
|
||||
p.param.color_space_internal = 82
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Param) RestartInterval() int {
|
||||
return int(p.param.restart_interval)
|
||||
}
|
||||
|
||||
func (p *Param) Interleaved() int {
|
||||
return int(p.param.interleaved)
|
||||
}
|
||||
|
||||
func (p *Param) ColorSpaceInternal() int {
|
||||
return int(p.param.color_space_internal)
|
||||
}
|
||||
|
||||
func (p *Param) SetRestartInterval(i int) {
|
||||
p.param.restart_interval = C.int(i)
|
||||
}
|
||||
|
||||
func (p *Param) SetInterleaved(i int) {
|
||||
p.param.interleaved = C.int(i)
|
||||
}
|
||||
|
||||
func (p *Param) SetColorSpaceInternal(c uint32) {
|
||||
p.param.color_space_internal = c
|
||||
}
|
126
paramimage.go
Normal file
126
paramimage.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package gpujpeg
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lgpujpeg
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
#include "wrapper.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type PixelFormat int
|
||||
|
||||
const (
|
||||
GPUJPEG_PIXFMT_NONE PixelFormat = -1
|
||||
|
||||
/// 8bit unsigned samples, 1 component
|
||||
GPUJPEG_U8
|
||||
|
||||
/// 8bit unsigned samples, 3 components, 4:4:4 sampling,
|
||||
/// sample order: comp#0 comp#1 comp#2, interleaved
|
||||
GPUJPEG_444_U8_P012
|
||||
|
||||
/// 8bit unsigned samples, 3 components, 4:4:4, planar
|
||||
GPUJPEG_444_U8_P0P1P2
|
||||
|
||||
/// 8bit unsigned samples, 3 components, 4:2:2,
|
||||
/// order of samples: comp#1 comp#0 comp#2 comp#0, interleaved
|
||||
GPUJPEG_422_U8_P1020
|
||||
|
||||
/// 8bit unsigned samples, planar, 3 components, 4:2:2, planar
|
||||
GPUJPEG_422_U8_P0P1P2
|
||||
|
||||
/// 8bit unsigned samples, planar, 3 components, 4:2:0, planar
|
||||
GPUJPEG_420_U8_P0P1P2
|
||||
|
||||
/// 8bit unsigned samples, 3 components, each pixel padded to 32bits
|
||||
/// with zero byte, 4:4:4 sampling, interleaved
|
||||
GPUJPEG_444_U8_P012Z
|
||||
|
||||
/// 8bit unsigned samples, 3 components, each pixel padded to 32bits
|
||||
/// with all-one bits, 4:4:4 sampling, interleaved
|
||||
GPUJPEG_444_U8_P012A
|
||||
)
|
||||
|
||||
func (d PixelFormat) String() string {
|
||||
return [...]string{"None", "U8", "444_U8_P012", "444_U8_P0P1P2", "422_U8_P1020", "422_U8_P0P1P2", "420_U8_P0P1P2", "444_U8_P012Z", "444_U8_P012A"}[d]
|
||||
}
|
||||
|
||||
type ParamImage struct {
|
||||
param C.gpujpeg_image_parameters
|
||||
}
|
||||
|
||||
func (p *ParamImage) Width() int {
|
||||
return int(p.param.width)
|
||||
}
|
||||
|
||||
func (p *ParamImage) Height() int {
|
||||
return int(p.param.height)
|
||||
}
|
||||
|
||||
func (p *ParamImage) CompCount() int {
|
||||
return int(p.param.comp_count)
|
||||
}
|
||||
|
||||
func (p *ParamImage) ColorSpace() int {
|
||||
return int(p.param.color_space)
|
||||
}
|
||||
|
||||
func (p *ParamImage) PixelFormat() int {
|
||||
return int(p.param.pixel_format)
|
||||
}
|
||||
|
||||
func (p *ParamImage) SetWidth(w int) {
|
||||
p.param.width = C.int(w)
|
||||
}
|
||||
|
||||
func (p *ParamImage) SetHeight(h int) {
|
||||
p.param.height = C.int(h)
|
||||
}
|
||||
|
||||
func (p *ParamImage) SetCompCount(c int) {
|
||||
p.param.comp_count = C.int(c)
|
||||
}
|
||||
|
||||
func (p *ParamImage) SetColorSpace(c uint32) {
|
||||
p.param.color_space = uint32(c)
|
||||
}
|
||||
|
||||
func (p *ParamImage) SetPixelFormat(pf int) {
|
||||
p.param.pixel_format = int32(pf)
|
||||
}
|
||||
|
||||
func SetImageParam() *ParamImage {
|
||||
p := &ParamImage{}
|
||||
C.gpujpeg_image_set_default_parameters(&p.param)
|
||||
return p
|
||||
}
|
||||
|
||||
func ReadImageInfo(image []byte) (*ParamImage, int, error) {
|
||||
p := &ParamImage{}
|
||||
|
||||
segment_count := C.int(0)
|
||||
|
||||
if C.gpujpeg_decoder_get_image_info((*C.uchar)(unsafe.Pointer(&image[0])), C.int(len(image)), &p.param, &segment_count) != C.int(0) {
|
||||
return nil, 0, errors.New("Can't decode image info")
|
||||
}
|
||||
|
||||
return p, int(segment_count), nil
|
||||
}
|
||||
|
||||
func SetCustomParam(w, h, c int) *ParamImage {
|
||||
p := &ParamImage{}
|
||||
C.gpujpeg_image_set_default_parameters(&p.param)
|
||||
p.param.width = C.int(w)
|
||||
p.param.height = C.int(h)
|
||||
p.param.comp_count = C.int(c)
|
||||
return p
|
||||
}
|
11
wrapper.c
Normal file
11
wrapper.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
#include "wrapper.h"
|
||||
|
||||
gpujpeg_decoder *create_decoder() {
|
||||
return gpujpeg_decoder_create(0);
|
||||
}
|
19
wrapper.h
Normal file
19
wrapper.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#ifndef WRAPPER_H
|
||||
#define WRAPPER_H
|
||||
|
||||
#include <libgpujpeg/gpujpeg.h>
|
||||
#include <libgpujpeg/gpujpeg_common.h>
|
||||
#include <libgpujpeg/gpujpeg_encoder.h>
|
||||
#include <libgpujpeg/gpujpeg_decoder.h>
|
||||
#include <libgpujpeg/gpujpeg_type.h>
|
||||
#include <libgpujpeg/gpujpeg_version.h>
|
||||
|
||||
typedef struct gpujpeg_decoder gpujpeg_decoder;
|
||||
typedef struct gpujpeg_parameters gpujpeg_parameters;
|
||||
typedef struct gpujpeg_image_parameters gpujpeg_image_parameters;
|
||||
typedef struct gpujpeg_decoder_output gpujpeg_decoder_output;
|
||||
|
||||
gpujpeg_decoder *create_decoder();
|
||||
|
||||
#endif /* WRAPPER_H */
|
Reference in New Issue
Block a user