thumbnail

package
v0.99.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package thumbnail implements expanding of stripped telegram thumbnails.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodePath added in v0.45.0

func DecodePath(data []byte) []byte

DecodePath decodes vector thumbnail from thumbnail bytes (e.g. tg.PhotoPathSize with type "j").

See DecodePathTo.

Example
package main

import (
	"os"

	"github.com/gotd/td/telegram/thumbnail"
)

func main() {
	thumb := []uint8{
		0x1a, 0x00, 0xb2, 0x04, 0xdc, 0x47, 0x03, 0x81, 0x73, 0x55, 0x48, 0x01, 0x46, 0x05, 0x44, 0x45,
		0x4f, 0x8b, 0x52, 0x8d, 0x4e, 0x8a, 0x5d, 0x8b, 0x6e, 0x8b, 0x71, 0x81, 0x4c, 0x4d, 0x07, 0x47,
		0x50, 0x02, 0x81, 0x46, 0x84, 0x4b, 0x83, 0x50, 0x80, 0x44, 0x67, 0x47, 0x01, 0x6b, 0x47, 0x08,
		0x43, 0x44, 0x4f, 0x53, 0x43, 0x56, 0x8e, 0x44, 0x97, 0x94, 0x9d, 0x9c, 0x82, 0x84, 0x89, 0x45,
		0x8b, 0x42, 0x88, 0x8a, 0xa6, 0xb3, 0xa8, 0xbc, 0x81, 0x8a, 0x81, 0xad, 0x82, 0xae, 0x87, 0x88,
		0xa5, 0x67, 0xab, 0x6a, 0x92, 0x49, 0xaa, 0x42, 0xb6, 0x4e, 0x8d, 0x4e, 0x5e, 0x80, 0x69, 0x43,
		0x45, 0x41, 0x4a, 0x46, 0x4d, 0x49, 0x52, 0x51, 0x8a, 0x46, 0x89, 0x47, 0x4b, 0x4f, 0x5f, 0x5b,
		0x6d, 0x67, 0x64, 0x5f, 0x46, 0x06, 0x48, 0x08, 0x63, 0x4d, 0x03, 0xa1, 0x70, 0x89, 0x06, 0x81,
		0x8b, 0x01, 0x48, 0x87, 0x44, 0x8a, 0x4c, 0x91, 0x51, 0x98, 0x51, 0x87, 0x03, 0x81, 0x89, 0x04,
		0x8f, 0x88, 0x86, 0x90, 0x8e, 0x95, 0x97, 0x81, 0x81, 0x82, 0x8b, 0x84, 0x8c, 0x98, 0x8d, 0xb2,
		0x93, 0x87, 0x00, 0xa9, 0xab, 0xad, 0x8e, 0x8a, 0x07, 0x69, 0x8c, 0x05, 0x5a, 0x89, 0x75, 0x84,
		0x48, 0x00, 0x87, 0x46, 0x80, 0x8c, 0x86, 0x92, 0x86, 0x90, 0x81, 0xa4, 0x4b, 0xb2, 0x81, 0x95,
		0x91, 0x9b, 0xab, 0xab, 0xbf, 0x95, 0x99, 0x87, 0x03, 0xb9, 0x87, 0x00, 0x89, 0x04, 0x42, 0xac,
		0x4e, 0x04, 0xb5, 0x51, 0x04, 0xb3, 0x4a, 0x81, 0x46, 0x86, 0x4a, 0x8b, 0x41, 0x81, 0x43, 0x44,
		0x45, 0x43, 0x51, 0x85, 0x5c, 0x8c, 0x6f, 0x8c,
	}

	r := thumbnail.DecodePath(thumb)
	w := os.Stdout
	w.WriteString(`<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
   viewBox="0 0 512 512" xml:space="preserve">
<path d="`)
	w.Write(r)
	w.WriteString(`"/>
</svg>`)

}
Output:

<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
   viewBox="0 0 512 512" xml:space="preserve">
<path d="M260,504c-73,1-51-21-81-65-4-5-15,11-18,13-14,10-29,11-46,11-49,1-12-137-7-162,1-6,4-11,3-16,0-4-39-71-43-78-3-4-15-19-3-22,14-4,23,20,29,28,2,4,9-5,11-2,8,10,38,51,40,60,1,10,1,45,2,46,7,8,37-39,43-42,18-9,42-2,54-14,13-14-30,0-41-3-5-1-10-6-13-9-18-17,10-6,9-7-11-15-31-27-45-39-36-31-66-88-35-133,33-48,96,1,111-8,7-4,10-12,17-17,24-17,73,1,94,15,8,6,16,14,21,23,1,1,2,11,4,12,24,13,50,19,70,41,43,45,14,107-41,125-26,9-53,4-80,7-6,0,12,6,18,6,16,1,36-11,50,1,21,17,27,43,43,63,21,25,73,57,70,94-2,44-144,53-174,51-10,1-6,6-10,11-1,1-3-4-5-3-17,5-28,12-47,12z"/>
</svg>

func DecodePathTo added in v0.45.0

func DecodePathTo(data, to []byte) []byte

DecodePathTo decodes vector thumbnail to given slice.

Returned path will contain the actual SVG path that can be directly inserted in the d attribute of an svg <path> element:

<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
   viewBox="0 0 512 512" xml:space="preserve">
<path d="{$path}"/>
</svg>

See https://core.telegram.org/api/files#vector-thumbnails.

func Expand

func Expand(data []byte) ([]byte, error)

Expand returns a JPG payload from stripped thumbnail bytes, like tg.UserProfilePhoto.StrippedThumb.

See ExpandTo.

Example
package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"image/jpeg"
	"image/png"

	"github.com/gotd/td/telegram/thumbnail"
	"github.com/gotd/td/tg"
)

func main() {
	userPhoto := tg.UserProfilePhoto{
		StrippedThumb: []uint8{
			0x01, 0x28, 0x28, 0x29, 0x40, 0x1d, 0xff, 0x00, 0x2a, 0x41, 0x8e, 0xe3, 0x22, 0xac, 0x42, 0x51,
			0xa0, 0x28, 0x03, 0x19, 0x1b, 0x9e, 0x07, 0x3f, 0xad, 0x69, 0x51, 0xbd, 0x91, 0xcf, 0x42, 0x29,
			0xea, 0xc8, 0x9a, 0x36, 0x0a, 0x1b, 0x6e, 0x01, 0xe0, 0x73, 0xd6, 0x99, 0xdf, 0xbf, 0xe2, 0x2a,
			0xdc, 0x71, 0xc8, 0xca, 0x11, 0x9d, 0x0e, 0xc3, 0xd7, 0x6f, 0x4f, 0xa5, 0x47, 0x2d, 0xb3, 0x2c,
			0x84, 0x20, 0x38, 0xec, 0x71, 0xd6, 0xa6, 0x12, 0xb3, 0xd4, 0xd2, 0xac, 0x14, 0x96, 0x8b, 0x52,
			0x0a, 0x2a, 0xc4, 0xb6, 0xa6, 0x38, 0x8b, 0x97, 0x04, 0x8e, 0xd8, 0xa2, 0xb6, 0x4d, 0x33, 0x95,
			0xc5, 0xad, 0x1a, 0x1b, 0x04, 0xcb, 0x11, 0xf9, 0xd0, 0x11, 0xeb, 0x8e, 0x45, 0x58, 0x8e, 0x68,
			0x93, 0x73, 0x96, 0x03, 0x79, 0xce, 0xd1, 0x55, 0x63, 0x50, 0x54, 0xb3, 0x64, 0xe3, 0xf0, 0x1f,
			0x89, 0xa4, 0x75, 0x20, 0xee, 0xe3, 0x6f, 0x6e, 0xd9, 0xa9, 0x71, 0x4d, 0x9a, 0x46, 0x72, 0x8a,
			0x2e, 0x79, 0xf2, 0x4a, 0x71, 0x02, 0x60, 0x7f, 0x79, 0xaa, 0x54, 0x46, 0x51, 0x97, 0x72, 0xcc,
			0x7f, 0x2a, 0x20, 0x74, 0x78, 0xc1, 0x40, 0x00, 0xf4, 0xf4, 0xa7, 0xb3, 0x05, 0x52, 0xc7, 0xa0,
			0xac, 0x9f, 0x63, 0x78, 0xae, 0xad, 0x94, 0xaf, 0xa4, 0xcb, 0x08, 0xc7, 0x41, 0xc9, 0xa2, 0xab,
			0xc8, 0xe6, 0x47, 0x66, 0x3d, 0xcd, 0x15, 0xbc, 0x55, 0x91, 0xc9, 0x39, 0x73, 0x3b, 0x88, 0x09,
			0x18, 0xf6, 0xe7, 0x14, 0x8c, 0xc5, 0x8e, 0x58, 0xe4, 0xd1, 0x45, 0x32, 0x6e, 0x3e, 0x19, 0x5a,
			0x27, 0xdc, 0x3a, 0x77, 0x1e, 0xb5, 0x62, 0xe2, 0xe5, 0x1e, 0x1d, 0xa9, 0xd4, 0xf5, 0xe3, 0xa5,
			0x14, 0x52, 0x71, 0x4d, 0xdc, 0xa5, 0x36, 0x95, 0x8a, 0x74, 0x51, 0x45, 0x32, 0x4f,
		},
	}

	result, err := thumbnail.Expand(userPhoto.StrippedThumb)
	if err != nil {
		panic(err)
	}

	img, err := jpeg.Decode(bytes.NewReader(result))
	if err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	if err := png.Encode(&buf, img); err != nil {
		panic(err)
	}
	fmt.Println("IMAGE:" + base64.StdEncoding.EncodeToString(buf.Bytes()))
}
Output:

func ExpandTo

func ExpandTo(data, to []byte) ([]byte, error)

ExpandTo appends a JPG payload to "to" byte slice, creating it from stripped thumbnail bytes (e.g. tg.UserProfilePhoto.StrippedThumb).

See https://core.telegram.org/api/files#stripped-thumbnails for reference.

Based on tdesktop implementation. See https://github.com/telegramdesktop/tdesktop/blob/v2.7.5/Telegram/SourceFiles/ui/image/image.cpp#L43.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL