Skip to content

Commit e496e5e

Browse files
committed
Fixed color encoding. Fixed initial textbox sizing with window.dispatch
1 parent 8837036 commit e496e5e

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

back/src/server.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ app.get('/gen.bmp', (req, res) => {
3838
const rayTracer = new RayTracer();
3939
rayTracer.renderToImage(defaultScene(), img);
4040

41-
ABGRtoRGBA(img.data);
41+
RGBAtoABGR(img.data);
4242

4343
res.contentType('image/bmp');
4444
res.send(bmp.encode(img).data);
@@ -59,7 +59,7 @@ app.post('/gen_xml', (req, res) => {
5959
const scene = xmlToScene(req.body.scene);
6060
rayTracer.renderToImage(scene, img);
6161

62-
ABGRtoRGBA(img.data);
62+
RGBAtoABGR(img.data);
6363

6464
res.contentType('image/bmp');
6565
res.send(bmp.encode(img).data);
@@ -68,7 +68,7 @@ app.post('/gen_xml', (req, res) => {
6868

6969
app.listen(PORT, () => console.log(`Listening on ${PORT}`))
7070

71-
function ABGRtoRGBA(imageData: Uint8ClampedArray) {
71+
function RGBAtoABGR(imageData: Uint8ClampedArray) {
7272
for (let i = 0; i < imageData.length; i += 4) {
7373
let r = imageData[i]; let g = imageData[i + 1]; let b = imageData[i + 2]; let a = imageData[i + 3];
7474
imageData[i] = a; imageData[i + 1] = b; imageData[i + 2] = g; imageData[i + 3] = r;

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ cd ..
4242

4343
# copy frontend bundles to public folder
4444
cp -r -v front/build/* back/public/
45-
cp -r -v react-client/build/* back/public/
45+
cp -r -v react-client/build/* back/public/react-client
4646

4747
echo 'Finished build'

front/src/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function onLoad() {
153153
height: size
154154
});
155155

156-
fetch("gen_xml", {
156+
fetch("http://localhost:1234/gen_xml", {
157157
"headers": {
158158
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
159159
"accept-language": "en-US,en;q=0.9",

raytrace/package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

raytrace/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@python36/raytrace",
2+
"name": "@python36/raytrace",
33
"version": "1.3.1",
44
"description": "Raytracer from typescript samples, broken up into modules",
55
"keywords": [],
@@ -10,7 +10,6 @@
1010
"build-cjs": "tsc",
1111
"build-es6": "tsc -p tsconfig.es6.json"
1212
},
13-
1413
"main": "lib/index.js",
1514
"types": "lib/index.d.ts",
1615
"module": "lib/esm/index.js",

react-client/src/App.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useRef } from 'react';
1+
import React, { useState, useEffect, useRef, useCallback, useLayoutEffect } from 'react';
22
import { samples } from '@python36/scene-xml';
33

44
import { sceneProcessors } from './processors';
@@ -13,7 +13,7 @@ const App: React.FC = () => {
1313
const renderingEl = useRef(null);
1414

1515
const setScene = (e: any) => setXmlInput(samples[e.target.value]);
16-
const setHeight = ():void => {
16+
const setHeight = useCallback(():void => {
1717
console.log('setHeight():', {renderingEl})
1818
if (renderingEl && renderingEl.current) {
1919
const elm = renderingEl.current as any;
@@ -22,7 +22,7 @@ const App: React.FC = () => {
2222
setTextareaHeight(elm.clientHeight);
2323
}
2424
}
25-
};
25+
}, [renderingEl, textareaHeight]);
2626

2727
const render = () => {
2828
const selectedProcessor = sceneProcessors[processor];
@@ -45,15 +45,16 @@ const App: React.FC = () => {
4545
return () => window.removeEventListener('resize', setHeight);
4646
}, [setHeight]);
4747

48-
useEffect(() => void setTimeout(setHeight, 1000));
49-
48+
useLayoutEffect(() => void setTimeout(() =>
49+
window.dispatchEvent(new Event('resize')), 1)
50+
, [imageURL]);
51+
5052
return (
5153
<div className="container">
5254
<hr />
5355

5456
<div className="row">
5557
<div className="col-md-6">
56-
{/* {imageURL && (<img src={imageURL} ref={renderingEl} className="rendering" />)} */}
5758
<img src={imageURL} ref={renderingEl} className="rendering" />
5859
</div>
5960
<div className="col-md-6 visible-md-inline visible-lg-inline">

react-client/src/processors.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export const sceneProcessors: SceneProcessors = {
4949
const imageData = new ImageData(size, size);
5050

5151
raytracer.renderToImage(scene, imageData);
52+
RGBAtoABGR(imageData.data);
5253
const { data: buffer } = bmp.encode(imageData);
5354
let newBlob = new Blob([buffer], {type: 'image/bmp'});
54-
console.log({newBlob})
5555
let url = URL.createObjectURL(newBlob);
5656
return Promise.resolve(url);
57-
5857
},
58+
5959
// 'golang-back': (xml, size) => {
6060
// fetch("http://localhost:5600/gen_xml.png?" + "xml-scene=" + encodeURI(xml), {
6161
// "headers": {
@@ -87,4 +87,11 @@ export const sceneProcessors: SceneProcessors = {
8787
// console.log({ err });
8888
// });
8989
// }
90-
};
90+
};
91+
92+
function RGBAtoABGR(imageData: Uint8ClampedArray) {
93+
for (let i = 0; i < imageData.length; i += 4) {
94+
let r = imageData[i]; let g = imageData[i + 1]; let b = imageData[i + 2]; let a = imageData[i + 3];
95+
imageData[i] = a; imageData[i + 1] = b; imageData[i + 2] = g; imageData[i + 3] = r;
96+
}
97+
}

0 commit comments

Comments
 (0)