<commit_before>//
// Copyright (c) 2011-2014 <NAME> <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import math
use ooc-base
use ooc-math
use ooc-geometry
import RasterPacked
import RasterImage
import StbImage
import Image, FloatImage
import Color
import Canvas, RasterCanvas

RasterMonochromeCanvas: class extends RasterPackedCanvas {
	target ::= this _target as RasterMonochrome
	init: func (image: RasterMonochrome) { super(image) }
	_drawPoint: override func (x, y: Int) {
		position := this _map(IntPoint2D new(x, y))
		if (this target isValidIn(position x, position y))
			this target[position x, position y] = this target[position x, position y] blend(this pen alphaAsFloat, this pen color toMonochrome())
	}
}

RasterMonochrome: class extends RasterPacked {
	bytesPerPixel ::= 1
	init: func ~allocate (size: IntVector2D) { super~allocate(size) }
	init: func ~allocateStride (size: IntVector2D, stride: UInt) { super(size, stride) }
	init: func ~fromByteBufferStride (buffer: ByteBuffer, size: IntVector2D, stride: UInt) { super(buffer, size, stride) }
	init: func ~fromByteBuffer (buffer: ByteBuffer, size: IntVector2D) { this init(buffer, size, this bytesPerPixel * size x) }
	init: func ~fromRasterMonochrome (original: This) { super(original) }
	init: func ~fromRasterImage (original: RasterImage) { super(original) }
	create: func (size: IntVector2D) -> Image { This new(size) }
	copy: func -> This { This new(this) }
	apply: func ~bgr (action: Func(ColorBgr)) {
		this apply(ColorConvert fromMonochrome(action))
	}
	apply: func ~yuv (action: Func(ColorYuv)) {
		this apply(ColorConvert fromMonochrome(action))
	}
	apply: func ~monochrome (action: Func(ColorMonochrome)) {
		pointer := this buffer pointer as ColorMonochrome*
		for (row in 0 .. this size y)
			for (column in 0 .. this size x) {
				pixel := pointer + row * this stride + column
				action(pixel@)
			}
	}
	resizeTo: override func (size: IntVector2D) -> This {
		this resizeTo(size, InterpolationMode Smooth) as This
	}
	resizeTo: override func ~withMethod (size: IntVector2D, method: InterpolationMode) -> This {
		result: This
		if (this size == size)
			result = this copy()
		else {
			result = This new(size)
			match (method) {
				case InterpolationMode Smooth => RasterPackedCanvas _resizeBilinear(this buffer pointer as ColorMonochrome*, result buffer pointer as ColorMonochrome*, this, result, IntBox2D new(this size), IntBox2D new(result size))
				case => RasterPackedCanvas _resizeNearestNeighbour(this buffer pointer as ColorMonochrome*, result buffer pointer as ColorMonochrome*, this, result, IntBox2D new(this size), IntBox2D new(result size))
			}
		}
		result
	}
	distance: override func (other: Image) -> Float {
		result := 0.0f<commit_msg>implemented draw in RasterMonochromeCanvas, simplified resizeTo<commit_after>//
// Copyright (c) 2011-2014 <NAME>ika <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import math
use ooc-base
use ooc-math
use ooc-geometry
import RasterPacked
import RasterImage
import StbImage
import Image, FloatImage
import Color
import Canvas, RasterCanvas

RasterMonochromeCanvas: class extends RasterPackedCanvas {
	target ::= this _target as RasterMonochrome
	init: func (image: RasterMonochrome) { super(image) }
	_drawPoint: override func (x, y: Int) {
		position := this _map(IntPoint2D new(x, y))
		if (this target isValidIn(position x, position y))
			this target[position x, position y] = this target[position x, position y] blend(this pen alphaAsFloat, this pen color toMonochrome())
	}
	draw: override func ~ImageSourceDestination (image: Image, source, destination: IntBox2D) {
		monochrome: RasterMonochrome = null
		if (image instanceOf?(RasterMonochrome))
			monochrome = image as RasterMonochrome
		else if (image instanceOf?(RasterImage))
			monochrome = RasterMonochrome convertFrom(image as RasterImage)
		else
			Debug raise("Unsupported image type in RasterMonochromeCanvas draw")
		this _resizePacked(monochrome buffer pointer as ColorMonochrome*, monochrome, source, destination)
		if (monochrome != image)
			monochrome referenceCount decrease()
	}
}

RasterMonochrome: class extends RasterPacked {
	bytesPerPixel ::= 1
	init: func ~allocate (size: IntVector2D) { super~allocate(size) }
	init: func ~allocateStride (size: IntVector2D, stride: UInt) { super(size, stride) }
	init: func ~fromByteBufferStride (buffer: ByteBuffer, size: IntVector2D, stride: UInt) { super(buffer, size, stride) }
	init: func ~fromByteBuffer (buffer: ByteBuffer, size: IntVector2D) { this init(buffer, size, this bytesPerPixel * size x) }
	init: func ~fromRasterMonochrome (original: This) { super(original) }
	init: func ~fromRasterImage (original: RasterImage) { super(original) }
	create: func (size: IntVector2D) -> Image { This new(size) }
	copy: func -> This { This new(this) }
	apply: func ~bgr (action: Func(ColorBgr)) {
		this apply(ColorConvert fromMonochrome(action))
	}
	apply: func ~yuv (action: Func(ColorYuv)) {
		this apply(ColorConvert fromMonochrome(action))
	}
	apply: func ~monochrome (action: Func(ColorMonochrome)) {
		pointer := this buffer pointer as ColorMonochrome*
		for (row in 0 .. this size y)
			for (column in 0 .. this size x) {
				pixel := pointer + row * this stride + column
				action(pixel@)
			}
	}
	resizeTo: override func (size: IntVector2D) -> This {
		this resizeTo(size, InterpolationMode Smooth) as This
	}
	resizeTo: override func ~withMethod (size: IntVector2D, method: InterpolationMode) -> This {
		result := This new(size)
		result canvas interpolationMode = method
		result canvas draw(this, IntBox2D new(this size), IntBox2D new(size))
		//TODO will be fixed by subcanvas
		result canvas interpolationMode = InterpolationMode Fast
		result
	}
	distance: override func (other: Image) -> Float {
		result := 0.0f
{'fraction_non_alphanumeric': 0.09841179807146909, 'fraction_numerical': 0.007231990924560408, 'mean_word_length': 4.6424, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5374029', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2137, 'n_words': 794}