原理:将数据的二进制形式写入图像红色通道数据二进制的低位
只支持png格式的输出
写入数据
go run shadow.go -in="c.jpg" -data="hide me" -out="out.png"
读取数据
go run shadow.go -in="out.png"
复制代码 代码如下:
package main
import (
"errors"
"flag"
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"log"
"math"
"os"
)
var FLAG = [4]byte{0x13, 0x14, 0x52, 0x00} //shadow flag.
//byte to 8 bits
func Byte2bits(b byte) (a [8]byte) {
var c uint8 = 7
var i uint8
for i = 0; i < 8; i++ {
a[i] = b (c - i) & 1
}
return
}
//8 bits to byte.
func Bits2Byte(a [8]byte) (b byte) {
for i := 0; i < 8; i++ {
b += a[i] * uint8(math.Pow(2, float64(7-i)))
}
return
}
//uint32 to 4 bytes.
func Uint32ToBytes(i uint32) (b [4]byte) {
b[0] = uint8(i 24)
b[1] = uint8(i 16 & 0xffff)
b[2] = uint8(i 8 & 0xff)
b[3] = uint8(i & 0xff)
return
}
//4 bytes to uint32.
func Bytes2Uint32(b [4]byte) (i uint32) {
var j uint32
for ; j < 4; j++ {
i += uint32(b[j]) << (24 - j*8)
}
return
}
func BuildShadowHeader(length uint32) (b [8]byte) {
var i int
for ; i < 4; i++ {
b[i] = FLAG[i]
}
a := Uint32ToBytes(length)
for ; i < 8; i++ {
b[i] = a[i-4]
}
return
}
func WriteShadow(b []byte, im image.Image) (out image.Image, err error) {
max := im.Bounds().Max.X*im.Bounds().Max.Y/8 - 64
b_len := len(b)
if len(b) > max {
return nil, errors.New("image does not have enough space for shadow.")
}
head := BuildShadowHeader(uint32(b_len))
var bb byte
var bs [8]byte
var i int
out, err = SetImage(im, func(index, x, y int, in, out image.Image) {
rgba := readRGBAColor(im.At(x, y))
if index < b_len*8+64 {
if index < 64 {
bb = head[index/8]
} else {
bb = b[index/8-8]
}
bs = Byte2bits(bb)
i = index % 8
if bs[i] != rgba.R&1 {
if bs[i] == 0 {
rgba.R -= 1
} else {
rgba.R += 1
}
}
}
if v := out.(*image.RGBA); v != nil {
v.SetRGBA(x, y, rgba)
}
})
if err != nil {
return nil, err
}
return
}
func ReadShadowData(im image.Image) (b []byte, err error) {
head, err := ReadShadowHeader(im)
if err != nil {
return nil, err
}
length := int(ReadShadowLength(head))
var bk []byte = make([]byte, length*8)
b = make([]byte, length)
_, err = SetImage(im, func(index, x, y int, in, out image.Image) {
if index >= 64 && index < length*8+64 {
R := readRGBAColor(im.At(x, y)).R
bk[index-64] = uint8(R & 1)
}
})
var bb [8]byte
var bs []byte
for i := 0; i < length; i++ {
bs = bk[8*i : 8*(i+1)]
for j := 0; j < 8; j++ {
bb[j] = bs[j]
}
b[i] = Bits2Byte(bb)
}
return
}
func ReadShadowHeader(im image.Image) (b [8]byte, err error) {
var bm [64]byte
_, err = SetImage(im, func(index, x, y int, in, out image.Image) {
rgba := readRGBAColor(im.At(x, y))
if index < 64 {
bm[index] = uint8(rgba.R & 1)
}
})
if err != nil {
return
}
var bb [8]byte
var bs []byte
for i := 0; i < 8; i++ {
bs = bm[8*i : 8*(i+1)]
for j := 0; j < 8; j++ {
bb[j] = bs[j]
}
b[i] = Bits2Byte(bb)
}
return
}
func ReadShadowFlag(b [8]byte) (a [4]byte) {
for i := 0; i < 4; i++ {
a[i] = b[i]
}
return
}
func ReadShadowLength(b [8]byte) uint32 {
var bb [4]byte
for i := 4; i < 8; i++ {
bb[i-4] = b[i]
}
return Bytes2Uint32(bb)
}
func OpenImage(path string) (image.Image, error) {
im_read, err := os.Open(path)
defer im_read.Close()
if err != nil {
return nil, err
}
im, _, err := image.Decode(im_read)
if err != nil {
return nil, err
}
return im, nil
}
//modify image
func SetImage(im image.Image, f func(index, x, y int, in, out image.Image)) (out image.Image, err error) {
if f == nil {
return im, nil
}
index := 0
bounds := im.Bounds()
out = image.NewRGBA(bounds)
var m *image.RGBA = out.(*image.RGBA)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
m.Set(x, y, im.At(x, y))
f(index, x, y, im, out)
index += 1
}
}
return out, nil
}
//conert any color to RABGA color.
func readRGBAColor(from_color color.Color) color.RGBA {
return color.RGBAModel.Convert(from_color).(color.RGBA)
}
//only write to jpeg formats.
func WriteImage(path string, im image.Image) error {
out, err := os.OpenFile(path, os.O_CREATE, os.ModePerm)
defer out.Close()
if err != nil {
return err
}
err = png.Encode(out, im)
if err != nil {
return err
}
return nil
}
var read_in string
var write_out string
var data string
func init() {
flag.StringVar(&read_in, "in", "", "image path read in.")
flag.StringVar(&write_out, "out", "out.jpg", "image path write out.")
flag.StringVar(&data, "data", "", "data to shadow.")
}
func errHandle(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
if read_in == "" {
fmt.Println("Options:")
flag.PrintDefaults()
return
}
im, err := OpenImage(read_in)
errHandle(err)
if data != "" {
out, err := WriteShadow([]byte(data), im)
errHandle(err)
err = WriteImage(write_out, out)
errHandle(err)
} else {
head, err := ReadShadowHeader(im)
errHandle(err)
_flag := ReadShadowFlag(head)
if _flag != FLAG {
fmt.Println("image doesn't have shadow data.")
return
}
data, err := ReadShadowData(im)
errHandle(err)
fmt.Println("shadow:", string(data))
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]