Image DPI

Just starting out? Need help? Post your questions and find answers here.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Image DPI

Post by coder14 »

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! :D
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Image DPI

Post by Kukulkan »

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).
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Image DPI

Post by wilbert »

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.
Windows (x64)
Raspberry Pi OS (Arm64)
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Image DPI

Post by Lebostein »

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.
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...
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Image DPI

Post by wilbert »

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...
You should insert a pHYs chunk before the IDAT chunk.
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)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Image DPI

Post by wilbert »

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.

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)
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Image DPI

Post by Lebostein »

Seems to work...
Cool! Thanks! :o
Post Reply