Compressing a Flag to 11 Bits

(read.vantezzen.io)

62 points | by bennett_dev 2 days ago

15 comments

  • kjgkjhfkjf 2 hours ago
    In case anyone else was curious about using a lookup table: there are there are fewer than 256 recognized countries or territories [1], so 8 bits should be enough for a key.

    This would be superior to the 11-bit encoding presented here because it handles complexities such as coats of arms. It would also handle bizarre situations such as two countries having almost identical flags [2].

    [1] https://en.wikipedia.org/wiki/List_of_countries_and_territor...

    [2] https://www.worldatlas.com/articles/country-flags-that-resem...

    • eventualcomp 1 hour ago
      Maybe there's an allegory to the sciences. But I rather a model be suboptimal in encoding that can be used to generate hypothetical reasonable flags in its unpopulated states, than having an optimal encoding which doesn't answer questions or reveal possibilities in its unpopulated states. Like what does flag #0xFE look like in an 8-bit encoding? I really like the idea of driving compressed trees in reverse with random data to generate new fascinating (valid) sequences.

      Also, for compressing into 8 bits - is this not like saying "I can compress all of Shakespeare's plays in 6 bits" because there are ~39 plays, but then the size of the "shakespearezip" is just the sum of the works?

    • NathanaelRea 39 minutes ago
      I don't understand why this is the top comment. The article isn't about encoding a bag of unique items, it's about drawing the flag. So of course you can do it in 8 bits, because you're just storing the "id", not how to draw it.
      • selcuka 13 minutes ago
        Yeah. I had to read the comment a few times to make sure that I'm not missing anything, but you are right. Those are two distinct things.

        As a side note, the title is not accurate either because the method in the article can compress only one flag to 11 bits:

        > Using this format, the average flag can be represented in 76 bits, with a median of 55 bits.

        One can use Huffman encoding (the same encoding the article uses) to compress one of the flags to a single bit.

    • dhosek 2 hours ago
      I’m surprised that the near-identical flags list omits Slovenia and Slovakia which both have white-blue-red horizontal stripes and a coat of arms (albeit with slightly different placement and different background colors on the coats of arms). A part of me feels like it’s part of a long-time joke the two countries are playing to confuse non-Slavs (the Slovenian word for their language is Slovenščina while the Slovak word for their language is Slovenčina and this is just the beginning of the confusion between the two countries/nationalities).
    • opiotrek 2 hours ago
      but then you have to have the svg built into decoder. will they be smaller than 5KB?

      also then it's no different from a svg sprite

      • MadnessASAP 16 minutes ago
        I feel like it would be unfair to make them include the SVG decoder, the artical uses TypeScript and I believe that is a feature most runtimes (browsers) provide. However they do have to include the SVG files themselves. The Canadian flag from Wikimedia comes in at 699 bytes and France at 262 bytes. So I'm fairly confident that even with compression they'll be over 5kB
    • porphyra 2 hours ago
      you could also exploit the fact that some flags may appear more frequently than others and use huffman encoding or something to encode the commonly used flags in a shorter sequence than rarely-mentioned countries, and save some bits on average
  • mabster 2 hours ago
    Loved the title. I was like "that's 10 bits too many for a flag!" Haha
    • xp84 1 hour ago
      Slightly reminds me of how we used to use TINYINT in MySQL to store boolean flags (which can store a range from -128 to 127 or 0-255 if unsigned).

      I'm the kind of nerd who secretly wished to store many such bools in one integer and use the bitwise operators to query them, but restrained myself from ever doing this, since if I managed to sneak that through code review, the number of curses of my name would no doubt become so large (and so loud) over time that it would affect my employability.

  • gerdesj 2 hours ago
    A jack is a flag flown from a jackstaff, wot is found at the back of a ship or boat.

    The UK flag is called the Union Flag (it's a flag and it is a union of various flags and possibly nations too!) but let's face it, given how many people think its called the Union Jack, it doesn't really matter.

    If your flag pole at home is somewhat canted then I think you can call it a jackstaff, with a similar justification to the Royal Navy managing to designate their shore bases as ships. For example just up the road is HMS Heron (His Majesty's Ship: Heron).

    The Union Flag/Jack is only the flag of the UK by convention and not law. It's a bit wooley, just like our Constitution but it still all works.

    My point is that whilst I do enjoy this encoding scheme and it is jolly clever, reality is way more complicated. The UK's flag is pretty complicated but not alone. I'm pretty sure several flags have tassels, which I suppose strays into the coat of arms territory.

    How hard do you want to squint!

    • xp84 1 hour ago
      I enjoyed that the Union Flag just gets its own 'preset' - the fact that it's still presiding over a large handful of others makes it a very efficient decision compared to trying to tease out all the necessary shapes to reconstruct it.

      I'll admit, I was hoping for some geometric approximations for countries like Brazil, though.

      • gerdesj 1 hour ago
        This scheme is OK but not OK. You note the Brazil flag and that's a great example along with the likes of the UK and the US too.

        The Brazilian flag is nominally "quite" simple: Green with a yellow/gold parallelogram/rhombus with a blue circle ... then there is the white band and stars. Absolute nightmare to classify.

        I thought the UK flag was a bit of a challenge and I remember an art class 50 odd years ago, trying to paint the bloody thing. Little did I know at the time about how Aussies and Kiwi worriers and co would fare! Won't someone think of Hawaiians: their state flag is not something you depict for a quick laugh!

        I suppose the US flag is arguably the hardest to depict: Those stars would take ages to draw/paint/dribble. I imagine kids get a box of stars to stick on their depictions in art classes in the US.

        • xp84 1 hour ago
          What's really hard, together with the quantity of them is that they're white! I haven't actually seen many star stickers used, but agree that's a great idea.

          Often, when the Flag's depicted in kids' art, paints or paint pens are used, since 'negative space' stars would take forever to do with a blue marker or pencil but is quicker to paint on top of blue. And there's a great deal of artistic license used in terms of their size and color. Or on a small flag, just a lot of tiny white dots!

  • ComputerGuru 1 hour ago
    If you're on Windows (where Microsoft famously chose they would not get involved with flags and show country codes instead), execute this in dev tools or save it as a javascript bookmarklet to make the emoji flags show up:

        javascript:(function(){if(!document.getElementById('twemoji-styles')){const s=document.createElement('style');s.id='twemoji-styles';s.textContent='img.emoji{height:1em!important;width:1em!important;margin:0 .05em 0 .1em!important;vertical-align:-0.1em!important;display:inline!important;}';document.head.appendChild(s)}const r=()=>{const o={folder:'svg',ext:'.svg'};twemoji.parse(document.body,o);if(!window.__twemojiObserver){let t;const ob=new MutationObserver(()=>{clearTimeout(t);t=setTimeout(()=>{ob.disconnect();twemoji.parse(document.body,o);ob.observe(document.body,{childList:true,subtree:true});},300);});ob.observe(document.body,{childList:true,subtree:true});window.__twemojiObserver=ob;}};if(window.twemoji){r();}else{const sc=document.createElement('script');sc.src='https://cdn.jsdelivr.net/npm/@twemoji/api@latest/dist/twemoji.min.js';sc.crossOrigin='anonymous';sc.onload=r;document.head.appendChild(sc);}})();
  • pimlottc 2 hours ago
    Surprised this didn’t mention heraldry, which has what’s basically a DSL for coats-of-arms:

    https://en.wikipedia.org/wiki/Blazon

  • ComputerGuru 1 hour ago
    Nice work! I wonder if you could (or did?) choose a more median shade for each color, especially blue? It seems, going by the comparison page, that most flags actually call for a less royal, more sky shade of blue, so you could use the median (on a linear RGB scale, or XYZ) shade instead as the designated representative for that color?

    I noticed Rwanda seems to have the wrong aspect ratio, though you are able to encode that arbitrarily, no?

  • emptybits 1 hour ago
    Cool idea. The author's AI failed to encode flags for approximately 35% of the world's population. Approximately 3 billion people. At least that's why my AI tells me. India obviously contributes a lot to this. I'm probably salty because my own country's simple (?) flag is also in the "unencodable" group. Lol.

    I applaud the author holding aspect ratio as a priority to encode. It's jarring to see an otherwise correct and familiar flag stretched into an incorrect shape when flown or shown.

  • loloquwowndueo 57 minutes ago
    Spoiler: after all this work only 128 flags were encoded to some degree, 67 left out. Dunno that I’d call an encoding scheme that can only represent 66% of its target set (and even so, with self admitted limitations) successful in any way.
  • Smalltalker-80 3 hours ago
    Nobody? Okay then: "Fun with flags!". There, I said it.
  • harpiaharpyja 2 hours ago
    With all the attention on the link between compression and intelligence lately it is neat to have this example of how creating a encoding scheme naturally allows you to create new (likely) never before seen flags.
  • blacklion 2 hours ago
    Fun fact: Constitution of Nepal has chapter about format of The Flag and it reads as geometry textbook problem (starred one, for advanced students).
  • ano-ther 2 hours ago
    Cool idea. Interesting that Indonesia, Poland and Monaco with very similar two-stripe designs have different compression rates (11, 14 and 17 respectively). Probably the aspect ratio that also gets encoded.
  • jtxt 2 hours ago
    Cool project and results! https://vantezzen.github.io/miniflags/
  • AdieuToLogic 1 hour ago
    This reminds me of the old joke[0]:

      There are 10 kinds of people in the world.
      Those who understand binary and those who don't.
    
    0 - https://www.gnu.org/fun/jokes/10-kinds-of-people.html.en
  • fercircularbuf 1 hour ago
    you couldn't dedicate one of those bit patterns to nepal?

    fun stuff

    • fercircularbuf 1 hour ago
      or treat the triangles as if they were part of a larger white flag?