### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Convert from Mono12 pixel format to RGBA  (Read 935 times)

#### BlueIcaro

• Hero Member
• Posts: 647
##### Convert from Mono12 pixel format to RGBA
« on: March 26, 2021, 03:47:06 pm »
Hello, I have a buffer from a camera with a image in Mono12 or in Mono8 pixel format. I want to save it in jpeg.
For convert from Mono8 to RGBA, I wrote:
Code: [Select]
`Imagen := TFPMemoryImage.Create(g_Frames.Width, g_Frames.Height);    fpCol.Alpha := \$FFFF;    if PixelFormat[0] = 'Mono8' then    begin      src := pInt8(g_Frames.buffer);// pInt8 is a pointer to shortInt      for Y := 0 to g_Frames.Height - 1 do      begin        for X := 0 to g_Frames.Width - 1 do        begin          fpCol.Red := Abs(src^ * \$101);          fpCol.Green := Abs(src^ * \$101);          fpCol.Blue := Abs(src^ * \$101);          Imagen[x, y] := fpCol;          Inc(src);        end;      end;    end                                        `After thar I write the image to file, like this:
Code: [Select]
` jpeg := TFPWriterJPEG.Create;    Imagen.SaveToFile('pepe.jpg', jpeg);    Imagen.Free;      `But How Can I convert from Mono12 to RGA.

/BlueIcaro
« Last Edit: March 26, 2021, 04:10:59 pm by BlueIcaro »

#### BlueIcaro

• Hero Member
• Posts: 647
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #1 on: March 26, 2021, 05:08:16 pm »
Hello, I found the solution by my self.
Code: [Select]
`if PixelFormat[0] = 'Mono8' then    begin      src := pInt8(g_Frames.buffer);      for Y := 0 to g_Frames.Height - 1 do      begin        for X := 0 to g_Frames.Width - 1 do        begin          fpCol.Red := Abs(src^ * \$100);          fpCol.Green := Abs(src^ * \$100);          fpCol.Blue := Abs(src^ * \$100);          Imagen[x, y] := fpCol;          Inc(src);        end;      end;    end    else    begin      src16 := pInt12(g_Frames.buffer);      for Y := 0 to g_Frames.Height - 1 do      begin        for X := 0 to g_Frames.Width - 1 do        begin          color := src16^;          Color := Color shl 4;          fpCol.Red := Abs(color);          fpCol.Green := Abs(color);          fpCol.Blue := Abs(color);          Imagen[x, y] := fpCol;          Inc(src16);        end;      end;    end;    jpeg := TFPWriterJPEG.Create;    Imagen.SaveToFile('pepe.jpg', jpeg);    Imagen.Free;  end;                                                    `/BlueIcaro

#### marcov

• Global Moderator
• Hero Member
• Posts: 9285
• FPC developer.
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #2 on: March 26, 2021, 05:18:19 pm »
Note that there is packed mono12 and normal mono12.

Normal mono 12 has a 12-bit pixels per word, and the other bits zeroed or not important. However it can vary with camera if the upper or lower bits are used

Packed mono12 packs two 12-bit pixels in a 3 bytes.  So the 2nd byte holds 4-bits of the first and 4-bits of the second pixel.

Usually it is the highest bits of the first pixel in the lowest 4-bits and the lowest of the second pixel in the highest 4-bits, but depending on logic and sensor-logic connection this may vary in theory.

packed mono12 exists e.g. in e.g. linescan cameras.

#### BlueIcaro

• Hero Member
• Posts: 647
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #3 on: March 26, 2021, 08:26:14 pm »
Hi,  thanks marcov for the info.
My camera sends the image un mono12, without any package.  And I don't need to swap the bytes. So I tested it and it's works fine.
The camera (industrial camera) has a viewer program. I make a photo with this software, and it's the same photo that I make with my program.

/BlueIcaro

#### marcov

• Global Moderator
• Hero Member
• Posts: 9285
• FPC developer.
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #4 on: March 26, 2021, 08:30:54 pm »
I don't even convert simple cases anymore. I do my measurements in 16-bit with some ANDs, and handle displaying in the opengl shader.

#### BlueIcaro

• Hero Member
• Posts: 647
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #5 on: March 26, 2021, 08:37:51 pm »
My program, will run on a Nvida Jetson Nano, without gui. The photo will be send to other program (wrote in pyhton by other coworker) which will analize the photo, using AI. The coworker programs needs the photo in jpeg, I don't know why.

That main problem is haven't GUI. So many library like  bgrabitmap , and vampire, etc. need a gui to work.

Quote
I don't even convert simple cases anymore. I do my measurements in 16-bit with some ANDs, and handle displaying in the opengl shader.

But your idea songs good. I would like more about it. Do you have any info/link/demo to take a look?
May be in next project, I 'll have to make a program with the camera, and I have gui. Or may be I can use the gpu (shaders) to make something

/BlueIcaro
« Last Edit: March 26, 2021, 08:42:35 pm by BlueIcaro »

#### marcov

• Global Moderator
• Hero Member
• Posts: 9285
• FPC developer.
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #6 on: March 26, 2021, 08:55:15 pm »
Some older code is in the first post here:

https://forum.lazarus.freepascal.org/index.php/topic,30556.msg194627.html#msg194627

later I integrated the code into my work framework, and I currently don't have a standalone version.

It is windows only, but that is mostly due the basic context routines and some of the font stuff. I advise to first play on windows before trying to port anything.

#### lainz

• Hero Member
• Posts: 4044
• Leandro Diaz
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #7 on: March 27, 2021, 12:18:34 am »
That main problem is haven't GUI. So many library like  bgrabitmap , and vampire, etc. need a gui to work.

You can use BGRABitmap4NoGui target, that will work?

#### BlueIcaro

• Hero Member
• Posts: 647
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #8 on: March 27, 2021, 03:35:06 pm »
Hi, if you create a program, add as requirmient BGRABitmaPack, and try this code:
Code: [Select]
`program Project1;{\$mode objfpc}{\$H+}uses  {\$IFDEF UNIX}{\$IFDEF UseCThreads}  cthreads,  {\$ENDIF}{\$ENDIF}  Classes  { you can add units after this },  BGRABitmap;beginend.         `
As soon as you try to compile, you got a lot of errors, most of then talking Wsregister control
Quote
Compilar proyecto, Objetivo: C:\Users\Jorge\AppData\Local\Temp\project1.exe: Código de salida 1, Errores: 16
project1.lpr(14,1) Error: Undefined symbol: WSRegisterCustomImageListResolution
project1.lpr(14,1) Error: Undefined symbol: WSRegisterDragImageListResolution
project1.lpr(14,1) Error: Undefined symbol: WSRegisterLazAccessibleObject
project1.lpr(14,1) Error: Undefined symbol: WSRegisterControl
project1.lpr(14,1) Error: Undefined symbol: WSRegisterWinControl
project1.lpr(14,1) Error: Undefined symbol: WSRegisterGraphicControl
project1.lpr(14,1) Error: Undefined symbol: WSRegisterCustomControl
project1.lpr(14,1) Error: Undefined symbol: WSRegisterScrollingWinControl
project1.lpr(14,1) Error: Undefined symbol: WSRegisterScrollBox
project1.lpr(14,1) Error: Undefined symbol: WSRegisterCustomFrame
project1.lpr(14,1) Error: Undefined symbol: WSRegisterCustomForm
project1.lpr(14,1) Error: Undefined symbol: WSRegisterHintWindow

This test I did in w10 Lazarus 2.12, with BGRAbitmap install using OP.
But In linux I have the same problem as I remember. I don't have linux in this PC.

/BlueIcaro

#### Fred vS

• Hero Member
• Posts: 2210
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #9 on: March 27, 2021, 04:29:39 pm »
Hello.

Tested your code in Linux (with light modifs for using cthreads) and there is no problem at compil + run.
I dont have a Windows machine to test it now.
Tested on Windows and all is ok too.

Code: Pascal  [Select][+][-]
1. program Project1;
2.
3. {\$mode objfpc}{\$H+}
4.
5. uses
6.   {\$IFDEF UNIX}
8.   {\$ENDIF}
9.   Classes
10.   { you can add units after this },
11.   BGRABitmap;
12.
13. begin
14. end.

I used fpc 3.2.0, Lazarus 2.0.10 and trunk BGRABitmap : https://github.com/bgrabitmap/bgrabitmap/tree/dev-bgrabitmap.

[EDIT] Tested with Lazarus 2.0.12 and all is ok too here.

Fre;D
« Last Edit: March 27, 2021, 05:55:06 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

#### BlueIcaro

• Hero Member
• Posts: 647
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #10 on: March 28, 2021, 01:08:38 pm »
Hi Fred, I was making some test on Linux, and I found that OpenGl library was missing on my linux pc.
After installing it, following this help https://wiki.lazarus.freepascal.org/BGRABitmap_and_OpenGL
it's works fine!

/BlueIcaro.

P.D. In windows I don't know it doen't work.

#### Fred vS

• Hero Member
• Posts: 2210
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #11 on: March 28, 2021, 02:15:40 pm »
Hi Fred, I was making some test on Linux, and I found that OpenGl library was missing on my linux pc.

If no, OpenGL should not need to be installed.
I will un-install  OpenGL and test a simple BGRABitmap application, if it fails to compile like you pointed, I will create a issue.

P.D. In windows I don't know it doen't work.

Hum, strange, here on Windows 10 all works out of the box.

Fre;D
« Last Edit: March 28, 2021, 04:22:00 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

#### Fred vS

• Hero Member
• Posts: 2210
##### Re: Convert from Mono12 pixel format to RGBA
« Reply #12 on: March 28, 2021, 03:33:00 pm »
Hello BlueIcaro.

OK, libGl.so  and OpenGL friends are uninstalled.
Tested to run some applications that need OpenGL and, as expected, they crash or give error messages.

So trying to compile your demo-test:

Code: Pascal  [Select][+][-]
1. program Project1;
2.
3. {\$mode objfpc}{\$H+}
4.
5. uses
6.   {\$IFDEF UNIX}
8.   {\$ENDIF}
9.   Classes
10.   { you can add units after this },
11.    BGRABitmap;
12.
13. begin
14. end.
15.

And ... compilation is ok, runs ok too (as expected).

Quote
Hint: Start of reading config file /etc/fpc.cfg
Hint: End of reading config file /etc/fpc.cfg
Free Pascal Compiler version 3.2.0 [2020/06/20] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others.
Target OS: Linux for x86-64
Compiling project1.pas
14 lines compiled, 0.7 sec
2 hint(s) issued

[EDIT] Here the command-line used:

Code: Bash  [Select][+][-]
1. fpc -oproject1
2. -Fu/home/fred/bgrabitmap-master/bgrabitmap
3. -Fu/home/fred/lazarus/2.0.12/components/lazutils
4. -Mobjfpc -Sh -Fcutf8 -B -XX -Xs -CX -vewinh
5. -FUunits project1.pas

Fre;D
« Last Edit: March 28, 2021, 04:41:59 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs