Skip to content

Instantly share code, notes, and snippets.

NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, PrefixCodeGroup const& prefix_code_group)
{
// Image data.
for (ARGB32 pixel : bitmap) {
u8 a = pixel >> 24;
u8 r = pixel >> 16;
u8 g = pixel >> 8;
u8 b = pixel;
TRY(prefix_code_group[0].write_symbol(bit_stream, g));
NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, bool all_pixels_are_opaque)
{
for (ARGB32 pixel : bitmap) {
u8 a = pixel >> 24;
u8 r = pixel >> 16;
u8 g = pixel >> 8;
u8 b = pixel;
// We wrote a huffman table that gives every symbol 8 bits. That means we can write the image data
// out uncompressed –- but we do need to reverse the bit order of the bytes.
// A short demo program that uses libjpeg to write cmyk/ycck jpeg files.
// It's possible to write ycck jpegs using ImageMagick's convert and Photoshop's Save As.
// I haven't found a good way to write subsampled true cmyk jpegs using any of cjpeg, convert, Photoshop, or GIMP though.
// (GIMP 2.99.16 has in-progress cmyk saving support, and by picking 4:2:0 subsampling it writes a 2111 CMYK file.
// But 2111 CMYK files don't make any sense, and 4:2:0 is a YCC term and applying it to CMYK is fairly silly.
// Anyways, I wasn't able to create a 2112 CMYK file, which is what is sometimes seen in pratice -- not that
// having C have more resolution than MY makes a ton of sense either.)
//
// compile with:
// clang++ -std=c++11 write_cmyk_ycck_jpegs.cc -I ~/Downloads/libjpeg-turbo-3.0.1 -I ~/Downloads/libjpeg-turbo-3.0.1/build -ljpeg -L ~/Downloads/libjpeg-turbo-3.0.1/build
@nico
nico / kpc_demo.c
Created April 20, 2023 15:06 — forked from ibireme/kpc_demo.c
A demo shows how to read Intel or Apple M1 CPU performance counter in macOS.
// =============================================================================
// XNU kperf/kpc demo
// Available for 64-bit Intel/Apple Silicon, macOS/iOS, with root privileges
//
//
// Demo 1 (profile a function in current thread):
// 1. Open directory '/usr/share/kpep/', find your CPU PMC database.
// For M1 (Pro/Max), the database file is '/usr/share/kpep/a14.plist'.
// 2. Select a few events that you are interested in,
// add their names to the `profile_events` array below.
@nico
nico / .sh
Created September 30, 2022 15:41
minimal gengetopt example
% cat minimal.txt
package "example"
version "0.0.1"
% ~/Downloads/gengetopt-2.23/src/gengetopt < minimal.txt
% cat cmdline.h
/** @file cmdline.h
* @brief The header file for the command line option parser
* generated by GNU Gengetopt version 2.23
* http://www.gnu.org/software/gengetopt.
@nico
nico / quine.py
Created March 24, 2021 12:44
This produces itself as output when run (`python3 quine.py`). Original from https://twitter.com/relsqui/status/1136130759784259584 , but that profile is now hidden
File "quine.py", line 1
File "quine.py", line 1
^
IndentationError: unexpected indent
@nico
nico / c_inline.md
Last active July 25, 2023 18:43
C vs C++ inline function semantics, and ODR vs /arch:

The semantics of inline are one of the areas where C and C++ are pretty different. This post is about the C++ semantics, but the history is interesting, so here's a short summary of it.

The meaning of "inline" is intuitively easy to understand: It gives the compiler a hint that it'd be nice if a function could be inlined. It gets a bit complicated because of two issues:

  1. If the function ends up not being inlined, where should the definition of the function be emitted?
  2. If the inline function contains a static local variable, should that be inlined? Should there be several copies of the static local, or just one?
@nico
nico / blocks.txt
Last active April 27, 2024 01:13
unicode blocks for terminal UI drawing; lots new in unicode 13
General Punctuation
U+201x ‐ ‒ – — ― ‖ ‗ ‘ ’ ‚ ‛ “ ” „ ‟
U+202x † ‡ • ‣ ․ ‥ … ‧
U+203x ‰ ‱ ′ ″ ‴ ‵ ‶ ‷ ‸ ‹ › ※ ‼ ‽ ‾ ‿
U+204x ⁀ ⁁ ⁂ ⁃ ⁄ ⁅ ⁆ ⁇ ⁈ ⁉ ⁊ ⁋ ⁌ ⁍ ⁎ ⁏
U+205x ⁐ ⁑ ⁒ ⁓ ⁔ ⁕ ⁖ ⁗ ⁘ ⁙ ⁚ ⁛ ⁜ ⁝ ⁞
Arrows
U+219x ← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙ ↚ ↛ ↜ ↝ ↞ ↟
U+21Ax ↠ ↡ ↢ ↣ ↤ ↥ ↦ ↧ ↨ ↩ ↪ ↫ ↬ ↭ ↮ ↯
@ech off
REM Creates "myroot" which can be used with `clang-cl /winsysroot myroot`
setlocal
REM Adjust this one accordingly:
set VSInstallDir="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional"
set WinSdkDir="%ProgramFiles(x86)%\Windows Kits"
@nico
nico / gist:5bd81b9f887abd48e29f70cdb0978b0c
Last active December 4, 2020 15:02
inotifywait -m with event coalescing
#!/bin/bash
inotifywait -m -e create -r $1 | while read dir action file; do
# Coalese additional queued up events, if any
# If events keep arriving faster than we can read them off stdin here,
# we'll never run the script! If you're worried about that, set
# SECONDS to 0 before this loop and make the conditional also check that
# $SECONDS is smaller than some timeout to make sure that the script runs
# every now and then. The stdin pipe buffer size means there won't be an
# unbounded buildup on stdin -- instead inotifywait will block writing