Quick OK Image format

Everything else that doesn't fall into one of the other PB categories.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

I would expect any changes to the file format would require only minor code changes, which I'm sure the author would provide.

Right now it works. I'll wait until the format is finalized before using it in any projects.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Quick OK Image format

Post by BarryG »

So how do we view the images? I tried ebs's code but it results in a ".qoi" image format.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

You can't view QOI-format images directly, as there is no image viewer for that format (at least none that I know of).
The original author's C code (and my PureBasic translation) provides routines to encode raw bitmap data into a QOI-format file, and to decode a QOI-format file back into raw bitmap data.

The test stub I added shows how to convert between BMP image files and QOI-format files. The original author presented a similar program using PNG images. I used BMP images because they don't use compression. This made it easier to compare the original BMP image to one that had been converted to QOI-format and then back to BMP, to verify that my code was working properly. Using the test code, you can convert a BMP image to QOI-format and then back to BMP.

The code provides all the tools necessary to write your own QOI-format image viewer in PureBasic, if desired.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Quick OK Image format

Post by infratec »

@ebs

I also started a PB version, but you were faster :wink:

You use always PeekA(), which is slow, because it is a procedure call with the usual additional stuff of push and pop some registers.
Faster is to access the memory without a call:

Code: Select all

Procedure.l qoi_read_32(*bytes.Ascii)
  
  Protected.a a, b, c, d
  
  a = *bytes\a
  *bytes + 1
  b = *bytes\a
  *bytes + 1
  c = *bytes\a
  *bytes + 1
  d = *bytes\a
  *bytes + 1
  
  ProcedureReturn (a << 24) | (b << 16) | (c << 8) | d
	
EndProcedure
I also eliminated the pos pointer p, because I directly increase the pointer itself.

I'm just finished with my version, but it is untested. If I have some time over the weekend, I will test it and write a LoadQOImage()
and a SaveQOIImage() procedure which is compatible wth PB.


Maybe this will work too:

Code: Select all

Structure rgba_Structure
  r.a
  g.a
  b.a
  a.a
EndStructure

Structure qoi_rgba_t
  StructureUnion
    rgba.rgba_Structure
    v.l
  EndStructureUnion
EndStructure

Procedure.l qoi_read_32(*bytes.qoi_rgba_t)
  
  Protected Result.l
  
  Result = *bytes\v
  *bytes + 4
  
  ProcedureReturn Result
	
EndProcedure
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

@infratec,

Your direct access to the bytes is much better than my Peek/Poke method.

I noticed that the 'qoi_read32()' and 'qoi_write_32()' procedures aren't really that important,
since they're used only to read/write the qoi image header. The real work is done in the
encode/decode routines. Your code should make those routines much faster.

Please post your code when it's done!

Regards,
Eric
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Quick OK Image format

Post by wilbert »

ebs wrote: Thu Dec 09, 2021 8:16 pmI would expect any changes to the file format would require only minor code changes
I don't know.
The code from the experimental branch is quite a bit different in some places and certainly not compatible with the original code.
https://github.com/phoboslab/qoi/tree/experimental
It depends on what will end up in the final specification.
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Quick OK Image format

Post by infratec »

I tested now my version and it works. (only channel 3 stuff up to now)
Your listing gives not the same result.

I build the original qoiconv.exe withe Pelles C.
Then I converted a small PNG file to OUI.

I load this OUI file with my test and it is shown as it should.
If I use your version, I see a wrong picture :cry:

Here is my version:

CODE REMOVED SINCE FINAL SPECS ARE NOW AVAILABLE

The saved OUI file is identical to the loaded file.
In my case the original PNG file is 11kB and the OUI file is 44kB.
But I reduced the PNG colors to 16 and use compression level 9.
In this case is the size definately larger.
Last edited by infratec on Mon Dec 20, 2021 4:00 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Quick OK Image format

Post by wilbert »

infratec wrote: Sat Dec 11, 2021 2:27 pmThe saved OUI file is identical to the loaded file.
In my case the original PNG file is 11kB and the OUI file is 44kB.
But I reduced the PNG colors to 16 and use compression level 9.
In this case is the size definately larger.
It's not expected to beat PNG compression when it comes to the compressed size.
The advantage is mainly speed and the simplicity of the code.
For a 16 color PNG it can make quite a difference what colors are used. The color hash function from the original code is a simple xor of the red, green and blue values. Maybe there's a lot of collisions with your color palette.
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Quick OK Image format

Post by infratec »

LoadImageQOI()

CODE REMOVED SINCE FINAL SPECS ARE NOW AVAILABLE
Last edited by infratec on Mon Dec 20, 2021 4:00 pm, edited 3 times in total.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

@infratec,

Do you have somewhere that you can post the 1) EXE you built from the C code, 2) the original image you used, and 3) the QOI file you get from the EXE ?

I can encode/decode using my version, but that doesn't mean that it is working as the original code intended.

Thanks,
Eric
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

@infratec,

I found out why my image doesn't show correctly with your code. You mentioned it: "Unfortunately the internal format in windows is BGR."

Based on that, either of the two changes below should show a QOI image encoded with my code and decoded with your code correctly.

1. Replace your 'Plot()' calls with 'CopyMemory()':

Code: Select all

    CreateImage(0, desc\Width, desc\Height, 8 * desc\channels)
    If StartDrawing(ImageOutput(0))
        *pixel = *img
        Select desc\channels
          Case 3
            *ImageAddress = DrawingBuffer()
            CopyMemory(*pixel, *ImageAddress, desc\Width * desc\Height * desc\channels)
            
            ; For y = 0 To desc\Height - 1
              ; For x = 0 To desc\Width - 1
                ; Plot(x, y, RGB(*pixel\rgba\r, *pixel\rgba\g, *pixel\rgba\B))
                ; *pixel + 3
              ; Next x
            ; Next y
or
2. Change the order of the colors in your 'Plot()' calls:

Code: Select all

    CreateImage(0, desc\Width, desc\Height, 8 * desc\channels)
    If StartDrawing(ImageOutput(0))
        *pixel = *img
        Select desc\channels
          Case 3
            For y = 0 To desc\Height - 1
              For x = 0 To desc\Width - 1
                Plot(x, y, RGB(*pixel\rgba\B, *pixel\rgba\g, *pixel\rgba\r))
                ; Plot(x, y, RGB(*pixel\rgba\r, *pixel\rgba\g, *pixel\rgba\B))
                *pixel + 3
              Next x
            Next y
Since you did the comparison of your images with images generated from the original C code, your version must be the right one.
Last edited by ebs on Mon Dec 13, 2021 8:32 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Quick OK Image format

Post by infratec »

@ebs:
I think you did it the wrong way.
In the QOI file it is RGB.
So the Plot with R G B is correct, since I use the RGB macro.

Original (master) qoiconv:

https://www.von-der-salierburg.de/downl ... oiconv.exe

The original png file:

https://www.von-der-salierburg.de/downl ... IP_IMA.png

The qoi file:

https://www.von-der-salierburg.de/downl ... IP_IMA.qoi
Last edited by infratec on Mon Dec 13, 2021 8:40 pm, edited 1 time in total.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

Thanks - please see my message immediately above yours.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Quick OK Image format

Post by infratec »

I read it already.
Look my first lines above.

I uploaded the complete quiconv stuff.
Simply download and install Pelles C V11
Then you can compile it by yourself.

https://www.von-der-salierburg.de/downl ... OIConv.zip
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: Quick OK Image format

Post by ebs »

Our messages keep crossing on the internet! :) Yes, I agree that your code is the right way.
infratec wrote: Mon Dec 13, 2021 8:30 pm @ebs:
I think you did it the wrong way.
In the QOI file it is RGB.
So the Plot with R G B is correct, since I use the RGB macro.
I think that's the difference:
Your 'QOI_COLOR_HASH()' macro used the structure r/g/b/a elements, while mine used PB procedures 'Red()/Green()/Blue()/Alpha()', which probably accounts for the difference.

Thanks for working through this with me.

Regards,
Eric
Post Reply