Discuss Scratch

JackK211424
Scratcher
500+ posts

Resize images in the editor in a better way

fdreerf wrote:

your
what

Broken images in projects (even my own) are really annoying.
I have been enlightened about Supersampling.

Edit: King of da page >:D

Last edited by JackK211424 (Aug. 11, 2021 01:00:10)


6d66yh
Scratcher
100+ posts

Resize images in the editor in a better way

I recommend the use of four times four oversampling, which is gamma-correct and it is fast enough.

#include <stdint.h>
#include <fstream>
#include <math.h>
std::fstream input;
std::fstream output;
	const int_fast32_t sRGBtolinear[256] = {0, 20, 40, 60, 80, 99, 119, 139, 159, 179, 199,
219, 241, 264, 288, 313, 340, 367, 396, 427, 458, 491, 526, 562, 599, 637, 677,
718, 761, 805, 851, 898, 947, 997, 1048, 1101, 1156, 1212, 1270, 1330, 1391,
1453, 1517, 1583, 1651, 1720, 1791, 1863, 1937, 2013, 2090, 2170, 2250, 2333,
2418, 2504, 2592, 2681, 2773, 2866, 2961, 3058, 3157, 3258, 3360, 3464, 3570,
3678, 3788, 3900, 4014, 4129, 4247, 4366, 4488, 4611, 4736, 4864, 4993, 5124,
5257, 5392, 5530, 5669, 5810, 5953, 6099, 6246, 6395, 6547, 6701, 6856, 7014,
7174, 7336, 7500, 7666, 7834, 8004, 8177, 8352, 8529, 8708, 8889, 9072, 9258,
9446, 9636, 9828, 10022, 10219, 10418, 10619, 10822, 11028, 11236, 11446, 11658,
11873, 12090, 12309, 12531, 12754, 12981, 13209, 13440, 13673, 13909, 14147,
14387, 14629, 14874, 15122, 15372, 15624, 15878, 16135, 16394, 16656, 16920,
17187, 17456, 17727, 18001, 18278, 18556, 18838, 19121, 19408, 19696, 19988,
20281, 20578, 20876, 21178, 21481, 21788, 22096, 22408, 22722, 23038, 23357,
23679, 24003, 24329, 24659, 24991, 25325, 25662, 26002, 26344, 26689, 27036,
27387, 27739, 28095, 28453, 28813, 29177, 29543, 29911, 30283, 30657, 31033,
31413, 31795, 32180, 32567, 32957, 33350, 33746, 34144, 34545, 34949, 35355,
35765, 36177, 36591, 37009, 37429, 37852, 38278, 38707, 39138, 39572, 40009,
40449, 40892, 41337, 41786, 42237, 42691, 43147, 43607, 44069, 44534, 45003,
45474, 45947, 46424, 46904, 47386, 47871, 48360, 48851, 49345, 49842, 50342,
50844, 51350, 51859, 52370, 52884, 53402, 53922, 54445, 54972, 55501, 56033,
56568, 57106, 57647, 58191, 58738, 59288, 59841, 60397, 60956, 61518, 62083,
62651, 63222, 63796, 64373, 64953, 65536};
	const int_fast32_t lineartosRGBthr[256] = {0, 10, 30, 50, 70, 90, 110, 130, 150, 170, 189, 209, 230, 253, 276, 301, 327, 354,
382, 412, 443, 475, 509, 544, 580, 618, 657, 698, 740, 783, 828, 875, 923, 972,
1023, 1075, 1129, 1185, 1242, 1300, 1360, 1422, 1486, 1551, 1617, 1685, 1755, 1827
  , 1900, 1975, 2052, 2130, 2210, 2292, 2376, 2461, 2548, 2637, 2727, 2820, 2914
, 3010, 3108, 3208, 3309, 3412, 3518, 3625, 3734, 3844, 3957, 4072, 4188, 4307,
4427, 4550, 4674, 4800, 4929, 5059, 5191, 5325, 5461, 5600, 5740, 5882, 6026, 6172
  , 6321, 6471, 6624, 6779, 6935, 7094, 7255, 7418, 7583, 7750, 7920, 8091, 8265
, 8440, 8618, 8798, 8981, 9165, 9352, 9541, 9732, 9925, 10121, 10318, 10518, 10721
  , 10925, 11132, 11341, 11552, 11766, 11981, 12200, 12420, 12643, 12868, 13095,
 13325, 13557, 13791, 14028, 14267, 14508, 14752, 14998, 15247, 15498, 15751, 16007
   , 16265, 16525, 16788, 17054, 17322, 17592, 17864, 18140, 18417, 18697, 18980
, 19265, 19552, 19842, 20135, 20430, 20727, 21027, 21330, 21635, 21942, 22252, 22565
    , 22880, 23198, 23518, 23841, 24166, 24494, 24825, 25158, 25494, 25832, 26173
 , 26517, 26863, 27212, 27563, 27917, 28274, 28633, 28995, 29360, 29727, 30097,
30470, 30845, 31223, 31604, 31987, 32373, 32762, 33154, 33548, 33945, 34345, 34747
  , 35152, 35560, 35971, 36384, 36800, 37219, 37641, 38065, 38493, 38923, 39355,
 39791, 40229, 40671, 41115, 41562, 42011, 42464, 42919, 43377, 43838, 44302, 44769
   , 45238, 45711, 46186, 46664, 47145, 47629, 48116, 48605, 49098, 49593, 50092
, 50593, 51097, 51604, 52114, 52627, 53143, 53662, 54184, 54709, 55236, 55767, 56300
    , 56837, 57377, 57919, 58465, 59013, 59564, 60119, 60676, 61237, 61800, 62367
 , 62936, 63509, 64084, 64663, 65245};
 uint_least8_t lineartosRGB(int32_t value){
	uint_least8_t a = 0;
	if(lineartosRGBthr[a+128] <= value) a+=128;
	if(lineartosRGBthr[a+ 64] <= value) a+= 64;
	if(lineartosRGBthr[a+ 32] <= value) a+= 32;
	if(lineartosRGBthr[a+ 16] <= value) a+= 16;
	if(lineartosRGBthr[a+  8] <= value) a+=  8;
	if(lineartosRGBthr[a+  4] <= value) a+=  4;
	if(lineartosRGBthr[a+  2] <= value) a+=  2;
	if(lineartosRGBthr[a+  1] <= value) a+=  1;
	return a;
 }
 uint32_t RGBavg(const uint32_t* pixel, const int_fast16_t amount){
	int_fast32_t red = 0;
 	int_fast32_t green = 0;
 	int_fast32_t blue = 0;
 	for(int_fast16_t i=0; i<amount; i++){
		red   += sRGBtolinear[(pixel[i]/65536)%256];
		green += sRGBtolinear[(pixel[i]/  256)%256];
		blue  += sRGBtolinear[(pixel[i]/    1)%256];
 	}
 	return lineartosRGB((red+(amount/2))/amount)*65536+lineartosRGB((green+(amount/2))/amount)*256+lineartosRGB((blue+(amount/2))/amount)*1;
 }
uint32_t findpixel(uint64_t* coord, uint32_t* originalimage, uint64_t width, uint64_t* numerator, uint64_t* denominator){
	uint64_t x[4];
	uint64_t y[4];
	x[0] = ((coord[0]*8+1)*denominator[0])/(8*numerator[0]);
	x[1] = ((coord[0]*8+3)*denominator[0])/(8*numerator[0]);
	x[2] = ((coord[0]*8+5)*denominator[0])/(8*numerator[0]);
	x[3] = ((coord[0]*8+7)*denominator[0])/(8*numerator[0]);
	y[0] = ((coord[1]*8+1)*denominator[1])/(8*numerator[1]);
	y[1] = ((coord[1]*8+3)*denominator[1])/(8*numerator[1]);
	y[2] = ((coord[1]*8+5)*denominator[1])/(8*numerator[1]);
	y[3] = ((coord[1]*8+7)*denominator[1])/(8*numerator[1]);
	uint32_t pixel[16];
	for(int_fast8_t i=0; i<16; i++){
		pixel[i] = originalimage[x[i%4]+y[i/4]*width];
	}
	return RGBavg(pixel, 16);
}
#include <iostream>
int main(){
	uint_fast32_t inputwidth; uint_fast32_t inputheight; uint_fast16_t inputpadding; uint_fast32_t outputwidth; uint_fast32_t outputheight;
	std::cout << "Input width, input height, input padding, output width, output height\n";
	std::cin >> inputwidth; std::cin >> inputheight; std::cin >> inputpadding; std::cin >> outputwidth; std::cin >> outputheight;
	uint32_t* image = (uint32_t*)malloc(inputwidth*inputheight*sizeof(uint32_t));
	input.open("input", std::ios::in|std::ios::binary);
	input.seekg (0, input.beg);
	for(uint64_t i=0; i<inputwidth*inputheight; i++){
		if(!(i%inputwidth) && i>0){
			char garbage;
			for(uint_fast8_t j=0; j<inputpadding; j++){
				input.read(&garbage, 1);
			}
		}
		input.read((char*)&image[i], 3);
	}
	input.close();
	uint32_t color = 0;
	output.open("output", std::ios::out|std::ios::binary);
	for(uint64_t i=0; i<outputwidth*outputheight; i++){
		uint64_t coord[2] = {i%outputwidth, i/outputwidth};
		uint64_t numerator[2] = {outputwidth, outputheight};
		uint64_t denominator[2] = {inputwidth, inputheight};
		color = findpixel(coord, image, inputwidth, numerator, denominator);
		output.write((char*)&color, 3);
	}
	output.close();
	free(image);
}

Integer arithmetic suggestion: https://scratch.mit.edu/discuss/post/5163608/
lm1996
Scratcher
1000+ posts

Resize images in the editor in a better way

gosoccerboy5 wrote:

Anything that increases image quality gets a support in my book.
True. Sometimes I want to draw refs that are bigger than the stage.

Lm / She/her / D&D DM / April 3rd /
dertermenter
Scratcher
1000+ posts

Resize images in the editor in a better way

The 2nd shrunk image looks far better quality than the first, it isn’t as blurry. I don’t have much to add, but this will just be great for importing images.


The2000 wrote:

All suggestions are unnecessary. If a suggestion is necessary then it's a bug report.

dertermenter wrote:

April Fools Day on the forums has been a repeated privilege, not an expectation
han614698
Scratcher
1000+ posts

Resize images in the editor in a better way

Bump

<Scratch Wikian | Forum Helper | 6000+ Posts>

Credit to -gge for the icons in my signature | I condensed all this code into five lines using [p] tags, idk why




Powered by DjangoBB