Skip to content

Instantly share code, notes, and snippets.

View jjrv's full-sized avatar

Juha Järvi jjrv

View GitHub Profile
@jjrv
jjrv / sixel.ts
Last active March 19, 2025 20:34
Fast DEC sixel encoding optimized to compress 256-color images using heavy bit twiddling
// Fast sixel encoder for indexed color bitmap terminal graphics
// Copyright 2025- Juha Järvi
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// Copyright 2025- Juha Järvi
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
const std = @import("std");
pub const Sizes = @Vector(4, u6);
const debug = true;
pub const BitReader = struct {
word_buf: [*]const u32,
bit_count: u6,
bits: u64,
@jjrv
jjrv / hilbert.js
Last active September 25, 2024 18:57
Map index along 2D Hilbert to coordinates (x,y) and back, branchless using bit twiddling.
/*
Map index along 2D Hilbert curve to coordinates (x,y) and back,
branchless using bit twiddling.
Copyright 2012, 2024 Juha Järvi
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
@jjrv
jjrv / LICENSE
Last active September 25, 2024 06:24
Fragment shader for variable thickness line segment with gradient fill and rounded cap
Copyright 2024- Juha Järvi
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
@jjrv
jjrv / Histogram.ts
Created April 9, 2024 05:40
Histogram diff in TypeScript
import { LCS, Interned } from './lcs';
export class Histogram<Token> {
constructor(before: Token[], after: Token[]) {
let prefix = 0;
let beforeEnd = before.length;
let afterEnd = after.length;
// Skip common prefix.

We want to find a perspective camera lookat direction $\hat{d}$ so that a given direction $\hat{a}$ in world coordinates becomes $\hat{b}$ in camera coordinates. The camera rotation matrix $R$ is defined by:

World local up vector:

$$ \hat{y} = \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}^{\mkern-1.5mu\mathsf{T}} $$

Camera lookat vector:

@jjrv
jjrv / vtable.zig
Last active October 31, 2023 15:32
const std = @import("std");
/// Given an example struct with methods, create a function that produces vtables for similarly shaped structs.
/// A vtable contains function pointers to all the struct methods, with the struct type erased.
/// All structs should have an init function that takes an allocator as the first parameter and stores it in a field called "allocator".
/// At most 2 parameters are supported for methods (in addition to the initial self parameter).
pub fn VTableShape(comptime Template: type) type {
var fields = @typeInfo(struct { //
deinit: *const fn (*anyopaque) void,
}).Struct.fields;
@jjrv
jjrv / LICENSE
Last active March 4, 2025 22:45
SRLAB2 and OKLAB
Copyright (c) 2020- Juha Järvi
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
import { Widget } from '@phosphor/widgets';
import { ElementExt } from '@phosphor/domutils';
export class IframePanel extends Widget {
constructor(iframe?: HTMLIFrameElement | null) {
super({ node: IframePanel.createNode() });
if(!iframe) iframe = document.createElement('iframe');
this.iframe = iframe;