xbrscaler

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 6 Imported by: 0

README

GoLang xBR Scaler

GoLang xBR Scaler is a Go implementation of the xBR scaling algorithm, originally developed in JavaScript (xBRjs by joseprio). This project aims to provide an efficient and easy-to-use solution for scaling pixel art images in GoLang applications, preserving the artistic style and enhancing the quality of low-resolution images.

Features

  • 2x, 3x, and 4x scaling using the xBR algorithm.
  • Options for color blending and alpha scaling.
  • Original and alternative xBR algorithm implementations.

Installation

To use the GoLang xBR Scaler in your project, you can install it using go get:

Usage

PNG Scaler Util
    pngScaler := NewPngScaler(true, true, false) // blend colors, scale alpha, do not use original 3x impl
    pngScaler.ScalePng("input.png", "output.png", 4) // read input.png and write 4x output to output.png
XBR Scaler
package main

import (
    "github.com/virtualparadox/xbrscaler"
)

func main() {
    // Load your pixel data into pixelArray (slice of uint32)
    pixelArray := []uint32{/* your pixel data */}
    originalWidth, originalHeight := /* your image dimensions */

    // Create a new Xbr scaler instance
    scaler := xbr.NewXbrScaler(false) // do not use original 3x scaling implementation

    // Scale the image using 2x scaling
    scaledPixels, scaledWidth, scaledHeight := scaler.Xbr3x(&pixelArray, originalWidth, originalHeight, true, true)

    // scaledPixels now contains the scaled image data
    // scaledWidth and scaledHeight are the dimensions of the scaled image
}

Examples

Original 3x Zoom
Original Image Zoomed Image
Description or details for original image Description or details for zoomed image

Contributing

Contributions to the GoLang xBR Scaler are welcome! If you have improvements or bug fixes:

  • Fork the repository.
  • Create a new branch for your feature or fix.
  • Implement your changes.
  • Submit a pull request.
  • Please ensure your code adheres to Go best practices and includes comments for GoDoc.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	// Masks for ARGB
	RedMask   uint32 = 0x000000FF
	GreenMask uint32 = 0x0000FF00
	BlueMask  uint32 = 0x00FF0000
	AlphaMask uint32 = 0xFF000000

	// Thresholds
	ThresholdY float64 = 48
	ThresholdU float64 = 7
	ThresholdV float64 = 6
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PngUtil

type PngUtil struct {
	// contains filtered or unexported fields
}

func NewPngScaler

func NewPngScaler(blendColors, scaleAlpha, use3XOriginalImplementation bool) *PngUtil

NewPngScaler creates and returns a new instance of PngUtil, a utility for scaling PNG images.

This function initializes a PngUtil struct with the specified configuration for image scaling. The PngUtil struct provides methods for scaling PNG images using the xBR algorithm, allowing customization through various options.

Parameters:

blendColors bool: Determines whether color blending should be applied during scaling.
                  When set to true, the algorithm blends colors to create smoother transitions.
scaleAlpha bool: Indicates whether alpha values should be scaled, affecting transparency handling.
use3XOriginalImplementation bool: Specifies which version of the xBR algorithm to use.
                                  If set to true, the original 3X implementation of the algorithm is used.
                                  If false, an alternative or updated version is used.

Returns:

*PngUtil: A pointer to a new PngUtil instance, configured with the provided scaling options.

The returned PngUtil instance can be used to perform PNG image scaling operations. The provided options allow for control over color blending, alpha scaling, and the choice of xBR algorithm implementation, affecting the visual outcome and performance of the scaling process.

func (*PngUtil) ScalePng

func (scl *PngUtil) ScalePng(inputFilename string, outputFilename string, zoom byte) error

ScalePng scales a PNG image using the xBR algorithm and saves the result to a file.

This method applies the xBR scaling algorithm to a PNG image specified by `inputFilename`. The scaled image is saved to the file specified by `outputFilename`. The scaling factor is determined by the `zoom` parameter, which can be set to 2, 3, or 4, corresponding to 2x, 3x, or 4x scaling respectively.

Parameters:

inputFilename string: The file path of the input PNG image to be scaled.
outputFilename string: The file path where the scaled PNG image will be saved.
zoom byte: The scaling factor (2, 3, or 4) to apply to the image.

Returns:

error: An error if the scaling process fails or if invalid arguments are provided.
       It returns an error if `zoom` is not in the range of 2 to 4, if the input file
       cannot be loaded, or if there's an issue saving the scaled image.

The method utilizes an `Xbr` scaler instance (created via `NewXbrScaler`) to perform the image scaling. It also uses internal methods `loadPngImage`, `extractPixels`, `createImageFromPixels`, and `savePngImage` for handling image data. The choice of xBR scaling (2x, 3x, or 4x) depends on the `zoom` parameter. The method ensures that only valid zoom levels are used and returns an error for invalid inputs.

type Xbr

type Xbr struct {
	// contains filtered or unexported fields
}

func NewXbrScaler

func NewXbrScaler(use3XOriginalImplementation bool) *Xbr

NewXbrScaler creates and returns a new instance of the Xbr scaler.

This function initializes an Xbr struct, which implements the xBR scaling algorithm for pixel art images. The xBR algorithm is used for upscaling low-resolution pixel art while preserving its original artistic style.

Parameter:

use3XOriginalImplementation bool: A flag that determines which version of the xBR algorithm to use.
                                  If set to true, the original 3X implementation of the algorithm is used.
                                  If false, an alternative or updated version of the algorithm is used.

Returns:

*Xbr: A pointer to a new instance of the Xbr struct configured with the specified algorithm version.

The returned Xbr instance can be used to perform pixel art scaling operations. The choice of algorithm version (original 3X implementation or an alternative) can affect the visual results and performance of the scaling process.

func (*Xbr) Xbr2x

func (xbr *Xbr) Xbr2x(pixelArray *[]uint32, width, height int, blendColors, scaleAlpha bool) (*[]uint32, int, int)

Xbr2x applies the 2x xBR scaling algorithm to a given pixel array.

This method takes a slice of uint32 representing the original pixel data in ARGB format, along with the width and height of the original image. It then scales the image by a factor of 2 using the xBR algorithm, which is designed for pixel art upscaling.

Parameters:

pixelArray *[]uint32: Pointer to a slice of uint32 representing the original pixel data in ARGB format.
width, height int: The width and height of the original image.
blendColors: A boolean flag indicating whether color blending should be used during the scaling process.
             When set to true, the algorithm blends colors to create smoother transitions.
scaleAlpha: A boolean flag indicating whether alpha values should be scaled, affecting the handling of transparency.

Returns:

*[]uint32: Pointer to a slice of uint32 containing the scaled pixel data.
int: The width of the scaled image, which is twice the width of the original image.
int: The height of the scaled image, which is twice the height of the original image.

The method uses the computeXbr2x function internally to process each pixel and write the results to a new slice that represents the scaled image. The returned slice contains the pixel data of the scaled image, and its dimensions are double those of the input image.

func (*Xbr) Xbr3x

func (xbr *Xbr) Xbr3x(pixelArray *[]uint32, width, height int, blendColors, scaleAlpha bool) (*[]uint32, int, int)

Xbr3x applies the 3x xBR scaling algorithm to a given pixel array.

This method takes a slice of uint32 representing the original pixel data in ARGB format, along with the width and height of the original image. It then scales the image by a factor of 2 using the xBR algorithm, which is designed for pixel art upscaling.

Parameters:

pixelArray *[]uint32: Pointer to a slice of uint32 representing the original pixel data in ARGB format.
width, height int: The width and height of the original image.
blendColors: A boolean flag indicating whether color blending should be used during the scaling process.
             When set to true, the algorithm blends colors to create smoother transitions.
scaleAlpha: A boolean flag indicating whether alpha values should be scaled, affecting the handling of transparency.

Returns:

*[]uint32: Pointer to a slice of uint32 containing the scaled pixel data.
int: The width of the scaled image, which is twice the width of the original image.
int: The height of the scaled image, which is twice the height of the original image.

The method uses the computeXbr2x function internally to process each pixel and write the results to a new slice that represents the scaled image. The returned slice contains the pixel data of the scaled image, and its dimensions are double those of the input image.

func (*Xbr) Xbr4x

func (xbr *Xbr) Xbr4x(pixelArray *[]uint32, width, height int, blendColors, scaleAlpha bool) (*[]uint32, int, int)

Xbr4x applies the 4x xBR scaling algorithm to a given pixel array.

This method takes a slice of uint32 representing the original pixel data in ARGB format, along with the width and height of the original image. It then scales the image by a factor of 2 using the xBR algorithm, which is designed for pixel art upscaling.

Parameters:

pixelArray *[]uint32: Pointer to a slice of uint32 representing the original pixel data in ARGB format.
width, height int: The width and height of the original image.
blendColors: A boolean flag indicating whether color blending should be used during the scaling process.
             When set to true, the algorithm blends colors to create smoother transitions.
scaleAlpha: A boolean flag indicating whether alpha values should be scaled, affecting the handling of transparency.

Returns:

*[]uint32: Pointer to a slice of uint32 containing the scaled pixel data.
int: The width of the scaled image, which is twice the width of the original image.
int: The height of the scaled image, which is twice the height of the original image.

The method uses the computeXbr2x function internally to process each pixel and write the results to a new slice that represents the scaled image. The returned slice contains the pixel data of the scaled image, and its dimensions are double those of the input image.

Jump to

Keyboard shortcuts

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