This could be a really noob question, but how can we adjust the DPI of an image? In Photoshop the DPI eg, 72dpi, 96dpi, 300dpi, 1200dpi, can be adjusted in the image properties. Can we do the same when resizing in PB? Or when creating an image?
TNX!
Image DPI
Re: Image DPI
Just calculate the needed size in pixels (https://en.wikipedia.org/wiki/Pixel_density) and scale the image. Photoshop is doing the same and it is no magic. DPI is just the number of pixels in one inch. For DPI calculation you need an output size in inch or a target DPI value (eg monitor resolution).
Re: Image DPI
PureBasic doesn't store dpi information for images.
You will have to modify the output of the image encoders to set dpi information if you want to.
You will have to modify the output of the image encoders to set dpi information if you want to.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Image DPI
How I can do that? I generate complex png files with the PB-VectorDrawing library. To get the real size while printing it is useful to store the DPI information inside the images (other image editing programs can store the DPI information)...wilbert wrote:PureBasic doesn't store dpi information for images.
You will have to modify the output of the image encoders to set dpi information if you want to.
At the moment I open the images with IrfanView, set the blank dpi information fields to 300 DPI (Image/Informations...) and save the images again...
Re: Image DPI
You should insert a pHYs chunk before the IDAT chunk.Lebostein wrote:How I can do that? I generate complex png files with the PB-VectorDrawing library. To get the real size while printing it is useful to store the DPI information inside the images (other image editing programs can store the DPI information)...
At the moment I open the images with IrfanView, set the blank dpi information fields to 300 DPI (Image/Informations...) and save the images again...
The easiest way to do so is probably to encode to memory first, copy each chunk from memory to the output file and insert a pHYs chunk while doing so.
The resolution inside the pHYs chunk is measured in pixels per meter.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Image DPI
Here's a small example.
The SetChunk_pHYs procedure writes 21 bytes to the specified memory location.
For this example I don't do any testing if the PNG data already contains a pHYs chunk.
Currently it should be working but if PB would ever output a pHYs chunk in the future, it might be safer to test if the chunk already exists and if it does, overwrite the data.
The SetChunk_pHYs procedure writes 21 bytes to the specified memory location.
For this example I don't do any testing if the PNG data already contains a pHYs chunk.
Currently it should be working but if PB would ever output a pHYs chunk in the future, it might be safer to test if the chunk already exists and if it does, overwrite the data.
Code: Select all
Procedure SetChunk_pHYs(*mem, dpi)
!mov eax, [p.v_dpi]
; convert dpi to pixels / meter
!mov ecx, 0x9d7af5ec
!mul ecx
!add eax, 0x02000000
!adc edx, 0
!shrd eax, edx, 26
; calculate crc
!bswap eax
!mov ecx, 0x69789a9c
!mov edx, 64
!.l0:
!test edx, 31
!jnz .l1
!xor ecx, eax
!.l1: shr ecx, 1
!jnc .l2
!xor ecx, 0xedb88320
!.l2: sub edx, 1
!jnz .l0
!xor ecx, 1
!mov edx, 8
!.l3: shr ecx, 1
!jnc .l4
!xor ecx, 0xedb88320
!.l4: sub edx, 1
!jnz .l3
!not ecx
!bswap ecx
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
!mov rdx, [p.p_mem]
!mov dword [rdx], 0x09000000 ; length
!mov dword [rdx+4], 0x73594870 ; type
!mov dword [rdx+8], eax ; data (9 bytes)
!mov dword [rdx+12],eax
!mov byte [rdx+16],1
!mov dword [rdx+17],ecx ; crc
CompilerElse
!mov edx, [p.p_mem]
!mov dword [edx], 0x09000000 ; length
!mov dword [edx+4], 0x73594870 ; type
!mov dword [edx+8], eax ; data (9 bytes)
!mov dword [edx+12],eax
!mov byte [edx+16],1
!mov dword [edx+17],ecx ; crc
CompilerEndIf
EndProcedure
UsePNGImageEncoder()
CreateImage(0, 256, 256, 24, $00ff00)
*PNGData = EncodeImage(0, #PB_ImagePlugin_PNG)
*pHYsChunk = AllocateMemory(21)
SetChunk_pHYs(*pHYsChunk, 300); 300 dpi
CreateFile(0, "DPI_Test.png")
WriteData(0, *PNGData, 33)
WriteData(0, *pHYsChunk, 21)
WriteData(0, *PNGData + 33, MemorySize(*PNGData)-33)
CloseFile(0)
FreeMemory(*pHYsChunk)
FreeMemory(*PNGData)
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Image DPI
Seems to work...
Cool! Thanks!
Cool! Thanks!