diff --git a/playground/cbor/2.png b/playground/cbor/2.png new file mode 100644 index 0000000..9131400 Binary files /dev/null and b/playground/cbor/2.png differ diff --git a/playground/cbor/cbor2_test.go b/playground/cbor/cbor2_test.go new file mode 100644 index 0000000..77f1975 --- /dev/null +++ b/playground/cbor/cbor2_test.go @@ -0,0 +1,53 @@ +package cbor + +import ( + "bytes" + "fmt" + "testing" + + "github.com/2tvenom/cbor" +) + +type Image struct { + Name string + Content []byte +} + +func TestCborEncode2(t *testing.T) { + // Read origin file + b := new(bytes.Buffer) + err := getImageBytes("./on.png", b) + if err != nil { + fmt.Println(err) + return + } + origin := b.Bytes() + + //image := &Image{Name:"on",Content:origin} + + // Encode + fmt.Println("--------- Encode ---------") + + var buf bytes.Buffer + encoder := cbor.NewEncoder(&buf) + ok, error := encoder.Marshal(origin) + + //check binary string + if !ok { + fmt.Errorf("Error decoding %s", error) + } else { + fmt.Println("Size: ", len(buf.Bytes())) + } + + // Decode + //fmt.Println("--------- Decode ---------",buf.Bytes()) + //var img []byte + //ok, err = encoder.Unmarshal(buf.Bytes(), &img) + //if !ok { + // fmt.Printf("Error Unmarshal %s", err) + // return + //} + ////output + //fmt.Printf("%v", img) + +} diff --git a/playground/cbor/cbor3_test.go b/playground/cbor/cbor3_test.go new file mode 100644 index 0000000..b71ac18 --- /dev/null +++ b/playground/cbor/cbor3_test.go @@ -0,0 +1,48 @@ +package cbor + +import ( + "bytes" + "fmt" + "testing" + + "go.mozilla.org/cose" +) + +func TestCborEncode3(t *testing.T) { + // Read origin file + b := new(bytes.Buffer) + err := getImageBytes("./on.png", b) + if err != nil { + fmt.Println(err) + return + } + origin := b.Bytes() + + //image := &Image{Name:"on",Content:origin} + fmt.Println("Size: ", len(origin)) + // Encode + fmt.Println("--------- Encode ---------") + + encoded, error := cose.Marshal(origin) + + if error != nil { + fmt.Errorf("Error decoding %s", error) + } else { + fmt.Println("Size: ", len(encoded)) + //fmt.Println("Content: ",string(encoded)) + } + + // Decode + fmt.Println("--------- Decode ---------") + //var img Image + unmarshal, err := cose.Unmarshal(encoded) + if err != nil { + fmt.Printf("Error Unmarshal %s", err) + return + } else { + fmt.Println("Size: ", len(unmarshal.([]byte))) + //fmt.Println("Content: ",unmarshal.([]byte)) + } + writeToFile("3", unmarshal.([]byte)) + +} diff --git a/playground/cbor/cbor_test.go b/playground/cbor/cbor_test.go new file mode 100644 index 0000000..cba4f8a --- /dev/null +++ b/playground/cbor/cbor_test.go @@ -0,0 +1,104 @@ +package cbor + +import ( + "bufio" + "bytes" + "fmt" + "image" + "image/jpeg" + "image/png" + "log" + "os" + "testing" + + "github.com/ugorji/go/codec" +) + +func TestCborEncode(t *testing.T) { + // Read origin file + buf := new(bytes.Buffer) + err := getImageBytes("./on.png", buf) + if err != nil { + fmt.Println(err) + return + } + origin := buf.Bytes() + + fmt.Println("size:", len(origin)) + writeToFile("1", origin) + + // Encode + encoded, err := encodeBinaryValue(buf.Bytes()) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("size:", len(encoded)) + writeToFile("2", encoded) + + // Decode + decoded, err := decodeBinaryValue(encoded) + fmt.Println("size:", len(decoded)) + writeToFile("3", decoded) +} + +func encodeBinaryValue(b []byte) ([]byte, error) { + var ch codec.CborHandle + buf := new(bytes.Buffer) + + enc := codec.NewEncoder(buf, &ch) + err := enc.Encode(&b) + + return buf.Bytes(), err +} + +func decodeBinaryValue(b []byte) ([]byte, error) { + var ch codec.CborHandle + var decoded []byte + var bufReader = bufio.NewReader(bytes.NewReader(b)) + var dec = codec.NewDecoder(bufReader, &ch) + var err = dec.Decode(&decoded) + return decoded, err +} + +func getImageBytes(imgFile string, buf *bytes.Buffer) error { + // Read existing image from file + img, err := os.Open(imgFile) + if err != nil { + return err + } + defer img.Close() + + // TODO: Attach MediaType property, determine if decoding + // early is required (to optimize edge processing) + + // Expect "png" or "jpeg" image type + imageData, imageType, err := image.Decode(img) + if err != nil { + return err + } + // Finished with file. Reset file pointer + img.Seek(0, 0) + if imageType == "jpeg" { + err = jpeg.Encode(buf, imageData, nil) + if err != nil { + return err + } + } else if imageType == "png" { + err = png.Encode(buf, imageData) + if err != nil { + return err + } + } + return nil +} + +func writeToFile(fileName string, b []byte) { + f, _ := os.Create(fileName) + n2, err := f.Write(b) + if err != nil { + log.Fatal(err) + } + fmt.Printf("wrote file %d bytes\n", n2) +} diff --git a/playground/cbor/on.png b/playground/cbor/on.png new file mode 100644 index 0000000..8849ff6 Binary files /dev/null and b/playground/cbor/on.png differ