예제 #1
0
파일: main.go 프로젝트: xrstf/rct
func main() {
	if len(os.Args) < 2 {
		log.Fatal("No filename given.")
	}

	file, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}

	decoder := tp4.NewDecoder()
	img, err := decoder.DecodeFile(file)
	if err != nil {
		log.Fatal(err)
	}

	out, err := os.Create("test.png")
	if err != nil {
		log.Fatal(err)
	}

	encoder := png.Encoder{png.BestCompression}
	err = encoder.Encode(out, img)
	if err != nil {
		log.Fatal(err)
	}
}
예제 #2
0
파일: encoder.go 프로젝트: hcninja/malpic
// Encodes data to monochromatic-scale PNG file
func EncodeColor(file []byte, secDat [][]int) (png.Encoder, *image.RGBA) {
	fSize := len(file)
	min := 0
	max := int(math.Sqrt(float64(fSize)))
	binIndex := 0

	fmt.Printf("[+] File size: %d\n", fSize)
	fmt.Printf("[+] Max vector size: %d\n", max)
	fmt.Printf("[+] Total vectors: %d\n", fSize/max)

	binImage := image.NewRGBA(
		image.Rect(min, min, max, max),
	)

	var c color.Color
	sectionNumber := 0

	// Fill the image with the file bytes
	for y := min; y < max; y++ {
		for x := min; x < max; x++ {
			// Set section color delimiters
			idxA := binIndex > secDat[sectionNumber][0]
			idxB := binIndex < secDat[sectionNumber][1]
			lim := sectionNumber < len(secDat)-1

			// Increase section number
			if binIndex > secDat[sectionNumber][1] && lim {
				sectionNumber++
			}

			// If the same section map with color
			if idxA && idxB && lim {
				// Get a color for every different section
				// NOTE: "sectionNumber%12" is for color table security, only 12 colors
				// available at the moment.
				c = GetColor(sectionNumber%12, file[binIndex])

			} else {
				c = color.RGBA{
					uint8(file[binIndex]),
					uint8(file[binIndex]),
					uint8(file[binIndex]),
					uint8(255),
				}
			}

			binIndex++
			binImage.Set(x, y, c)
		}
	}

	var enc png.Encoder
	enc.CompressionLevel = 0

	return enc, binImage
}
예제 #3
0
func (b basicController) Encode(target io.Writer) error {
	switch b.imageFormat {
	case "jpeg":
		jpeg.Encode(target, b.data, &jpeg.Options{jpeg.DefaultQuality})
	case "png":
		encoder := png.Encoder{CompressionLevel: png.BestCompression}
		encoder.Encode(target, b.data)
	case "gif":
		gif.Encode(target, b.data, &gif.Options{256, nil, nil})
	default:
		return fmt.Errorf("invalid imageFormat given")
	}

	return nil
}
예제 #4
0
func writeMandelbrotImageToFile() {
	m := image.NewRGBA(image.Rect(0, 0, int(w), int(h)))
	for i := 0; i < int(w); i += 1 {
		for j := 0; j < int(h); j += 1 {
			c := colorOf(i, j)
			m.Set(i, j, color.RGBA{c, c, c, 255})
		}
	}
	file, err := os.Create("pure-go.png")
	if err != nil {
		fmt.Printf("%v\n", err)
	}
	defer file.Close()
	enc := png.Encoder{CompressionLevel: -1}
	enc.Encode(file, m)
}
예제 #5
0
// Calculate the DiffMetrics for the provided digests.
func (fs *FileDiffStore) diff(d1, d2 string) (*diff.DiffMetrics, error) {
	img1, err := fs.getDigestImage(d1)
	if err != nil {
		return nil, err
	}

	img2, err := fs.getDigestImage(d2)
	if err != nil {
		return nil, err
	}
	dm, resultImg := diff.Diff(img1, img2)

	baseName := getDiffBasename(d1, d2)

	// Write the diff image to a temporary file.
	tempOut, err := ioutil.TempFile(fs.localTempFileDir, fmt.Sprintf("tempfile-%s", baseName))
	if err != nil {
		return nil, fmt.Errorf("Unable to create temp file: %s", err)
	}

	encoder := png.Encoder{CompressionLevel: png.BestSpeed}
	if err := encoder.Encode(tempOut, resultImg); err != nil {
		return nil, err
	}

	err = tempOut.Close()
	if err != nil {
		return nil, fmt.Errorf("Error closing temp file: %s", err)
	}

	diffImageFilename := fmt.Sprintf("%s.%s", baseName, IMG_EXTENSION)
	outputFileName, err := fs.createRadixPath(fs.localDiffDir, diffImageFilename)
	if err != nil {
		return nil, err
	}

	fs.diffDirLock.Lock()
	defer fs.diffDirLock.Unlock()
	if err := os.Rename(tempOut.Name(), outputFileName); err != nil {
		return nil, fmt.Errorf("Unable to move file: %s", err)
	}

	// This sets a logical path for this file.
	dm.PixelDiffFilePath = diffImageFilename
	return dm, nil
}
예제 #6
0
파일: encoder.go 프로젝트: hcninja/malpic
// Encodes data to black and white PNG file
func EncodeBW(file []byte) (png.Encoder, *image.RGBA) {
	fSize := len(file)
	min := 0
	max := int(math.Sqrt(float64(fSize)))
	binIndex := 0

	fmt.Printf("[+] File size: %d\n", fSize)
	fmt.Printf("[+] Max vector size: %d\n", max)
	fmt.Printf("[+] Total vectors: %d\n", fSize/max)

	binImage := image.NewRGBA(
		image.Rect(min, min, max, max),
	)

	var c color.Color

	// Fill the image with the file bytes
	for y := min; y < max; y++ {
		for x := min; x < max; x++ {
			c = color.RGBA{
				uint8(file[binIndex]),
				uint8(file[binIndex]),
				uint8(file[binIndex]),
				uint8(255),
			}

			binIndex++
			binImage.Set(x, y, c)
		}
	}

	var enc png.Encoder
	enc.CompressionLevel = 0

	return enc, binImage
}
예제 #7
0
// Process はリサイズ処理を行い、エンコードしたデータを返します。
func (self *Processor) Process(i image.Image, w io.Writer, f storage.Image) (*image.Point, error) {
	var ir image.Image
	switch f.ValidatedMethod {
	default:
		return nil, fmt.Errorf("Unsupported method: %s", f.ValidatedMethod)
	case storage.MethodNormal:
		ir = resize.Resize(uint(f.DestWidth), uint(f.DestHeight), i, resize.Lanczos3)
	case storage.MethodThumbnail:
		cr := image.Rect(0, 0, f.CanvasWidth, f.CanvasHeight)
		src := resize.Resize(uint(f.DestWidth), uint(f.DestHeight), i, resize.Lanczos3)
		dst := image.NewRGBA(cr)
		draw.Draw(dst, cr, src, image.Point{int((f.DestWidth - f.CanvasWidth) / 2), int((f.DestHeight - f.CanvasHeight) / 2)}, draw.Src)
		ir = dst
	}

	switch f.ValidatedFormat {
	default:
		return nil, fmt.Errorf("Unsupported format: %s", f.ValidatedFormat)
	case storage.FormatJpeg:
		if err := jpeg.Encode(w, ir, &jpeg.Options{int(f.ValidatedQuality)}); err != nil {
			return nil, err
		}
	case storage.FormatPng:
		e := png.Encoder{CompressionLevel: png.DefaultCompression}
		if err := e.Encode(w, ir); err != nil {
			return nil, err
		}
	case storage.FormatGif:
		if err := gif.Encode(w, ir, &gif.Options{NumColors: 256}); err != nil {
			return nil, err
		}
	}

	size := ir.Bounds().Size()
	return &size, nil
}
예제 #8
0
func Render(letter string, bgColor color.Color, width int, out io.Writer) error {
	fg := pickForegroundColor(bgColor)

	rgba := image.NewRGBA(image.Rect(0, 0, width, width))
	draw.Draw(rgba, rgba.Bounds(), &image.Uniform{bgColor}, image.ZP, draw.Src)

	fontSize := fontSizeFactor * float64(width)
	d := &font.Drawer{
		Dst: rgba,
		Src: &image.Uniform{fg},
		Face: truetype.NewFace(fnt, &truetype.Options{
			Size:    fontSize,
			DPI:     dpi,
			Hinting: font.HintingNone,
		}),
	}

	y := int(yOffsetFactor*float64(width)) + int(math.Ceil(fontSize*dpi/72))
	d.Dot = fixed.Point26_6{
		X: (fixed.I(width) - d.MeasureString(letter)) / 2,
		Y: fixed.I(y),
	}
	d.DrawString(letter)

	b := bufio.NewWriter(out)
	encoder := png.Encoder{CompressionLevel: png.DefaultCompression}
	err := encoder.Encode(b, rgba)
	if err != nil {
		return err
	}
	err = b.Flush()
	if err != nil {
		return err
	}
	return nil
}
예제 #9
0
func main() {
	const charFullWidth = 12
	const charHalfWidth = 6
	const charHeight = 16

	files := []string{
		"latin.png",
		"bmp-0.png",
		"bmp-2.png",
		"bmp-3.png",
		"bmp-4.png",
		"bmp-5.png",
		"bmp-6.png",
		"bmp-7.png",
		"bmp-8.png",
		"bmp-9.png",
		"bmp-15.png",
	}
	palette := color.Palette([]color.Color{
		color.Transparent, color.Opaque,
	})
	result := image.NewPaletted(image.Rect(0, 0, 256*charFullWidth, 256*charHeight), palette)
	for _, file := range files {
		f, err := os.Open(file)
		if err != nil {
			panic(err)
		}
		defer f.Close()
		img, _, err := image.Decode(f)
		if err != nil {
			panic(err)
		}
		if file == "latin.png" {
			for i := 0; i < 256; i++ {
				dx := i * charFullWidth
				dy := 0
				dr := image.Rect(dx, dy, dx+charHalfWidth, dy+charHeight)
				sp := image.Point{
					(i % 32) * charHalfWidth,
					(i / 32) * charHeight,
				}
				draw.Draw(result, dr, img, sp, draw.Src)
			}
			continue
		}
		id, err := strconv.Atoi(regexp.MustCompile(`\d+`).FindString(file))
		if err != nil {
			panic(err)
		}
		for i := 0; i < 4096; i++ {
			if id == 0 && i < 256 {
				continue
			}
			dx := (i % 256) * charFullWidth
			dy := id*256 + (i/256)*charHeight
			dr := image.Rect(dx, dy, dx+charFullWidth, dy+charHeight)
			sp := image.Point{
				(i % 64) * charFullWidth,
				(i / 64) * charHeight,
			}
			draw.Draw(result, dr, img, sp, draw.Src)
		}
	}
	e := png.Encoder{CompressionLevel: png.BestCompression}
	if err := e.Encode(os.Stdout, result); err != nil {
		panic(err)
	}
}
예제 #10
0
파일: malPic.go 프로젝트: hcninja/malpic
func main() {
	flag.Parse()

	if infoFlag {
		fmt.Println("")
		fmt.Println("malPic " + VERSION)
		fmt.Println("\t Malware visual analysis tool")
		fmt.Println("\t Licensed under GPLv2 – 2015")
		fmt.Println("\t https://github.com/hcninja/malpic/LICENSE")
		fmt.Println("\t By @bitsniper")
		fmt.Println("")
		os.Exit(0)
	}

	// Flag sanity checks
	if inputFlag == "" || outputFlag == "" && !noPictFlag {
		fmt.Println("[!] Please give me an input and output, try with -h")
		os.Exit(1)
	}

	if colorizeFlag && !execInfoFlag {
		fmt.Println("[!] Colorized output needs -execinfo")
		os.Exit(1)
	}

	if noPictFlag && !execInfoFlag {
		fmt.Println("[!] Setting -execinfo flag")
		execInfoFlag = true
	}

	if noPictFlag && inputFlag != "" {
		fmt.Println("[!] Ignoring -out flag")
	}

	if symbolsDumpFlag && !execInfoFlag {
		fmt.Println("[!] For symbols dump the -execinfo is needed")
		fmt.Println("[!] Symbols will not be dumped")
	}

	// Open file
	file, err := ioutil.ReadFile(inputFlag)
	if err != nil {
		fmt.Printf("[!] %s\n", err)
		os.Exit(1)
	}

	var sectionData [][]int
	// Extract executable info
	if execInfoFlag {
		if err := bin.IsPE(inputFlag); err == nil {
			fmt.Println("[+] File is PE")
			sectionData, err = bin.PEAnal(inputFlag, symbolsDumpFlag)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		} else if err := bin.IsELF(inputFlag); err == nil {
			fmt.Println("[+] File is ELF")
			sectionData, err = bin.ELFAnal(inputFlag, symbolsDumpFlag)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		} else if err := bin.IsMACHO(inputFlag); err == nil {
			fmt.Println("[+] File is MACH-O")
			sectionData, err = bin.MACHOAnal(inputFlag, symbolsDumpFlag)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		} else {
			fmt.Println("[+] File is not an executable")
		}
	}

	var encoder png.Encoder
	var binImage *image.RGBA

	// Encode binary to colorized image or B/W
	if !noPictFlag {
		if colorizeFlag {
			encoder, binImage = img.EncodeColor(file, sectionData)
		} else {
			encoder, binImage = img.EncodeBW(file)
		}

		// Write image to file
		malPict, _ := os.Create(outputFlag)
		encoder.Encode(malPict, binImage)
		fmt.Println("[+] Picture saved as: " + outputFlag)
	}
}
		return image.Decode(fp)
	}

	saveImage := func(path string, format string, i image.Image) error {
		fp, err := os.Create(path)
		if err != nil {
			return err
		}

		defer fp.Close()
		switch format {
		case "jpeg":
			jpeg.Encode(fp, i, &jpeg.Options{jpeg.DefaultQuality})
		case "png":
			encoder := png.Encoder{CompressionLevel: png.BestCompression}
			encoder.Encode(fp, i)
		case "gif":
			gif.Encode(fp, i, &gif.Options{256, nil, nil})
		default:
			return fmt.Errorf("invalid imageFormat given")
		}

		return nil
	}

	formats := []format{
		{Width: 220, Height: 480},
		{Width: 500, Height: 500},
		{Width: 400, Height: 200},
		{Width: 50, Height: 50},
예제 #12
0
파일: main.go 프로젝트: ttyv0/simple-render
func (img *Img) Save() error {
	encoder := png.Encoder{png.DefaultCompression}
	err := encoder.Encode(img.f, img.i)
	img.f.Close()
	return err
}