Page 1 of 1

Display .PPM files in RGB colors - WIN 10 ONLY

Posted: 03 Sep 2017 10:31
by IcarusLives
Hello Everyone! ^-^

Today I would finally like to share this base project!

Image



If you have seen Displaying images from hex then you may have been curious as to how I ended p with the hex codes for the image.

Together, a very close friend and I have been working eagerly to finish this script.

How it works is we parse the PPM width and height, and the total size of the header. Using this information we can resize the console and begin gathering data from AFTER the header (since we now have the size). We utilized the FC command to grab each characters hex value and store it in an array of HEX[] (up to 990 characters, divisible by 6, and less than 1024). One thing that made this complicated was that FC needs to compare the data from the PPM to a file we create filled with 0s called "zeros.txt". This was convenient to convert to hex, but if the PPM contained a 0, it would skip an offset. For this, we needed to account for all missing offsets. (see LN 27-30).

After parsing all of the data in hex[] it is then ready to be displayed in RGB.

Each hex[] is 990 except for the very last hex[] which is the leftover hex from the PPM. We chose 990 because as stated before it's divisible by 6, which is largely essential to display RRGGBB.

I hope you all enjoy this script. We are still looking to further optimize this script to make it faster. Any suggestions are welcome!

DOWNLOAD this script and PPM
https://www.mediafire.com/file/2lypfeg6 ... %20PPM.zip

This script REQUIRES the PPM from the download

Code: Select all

@echo off & setlocal enableDelayedExpansion

set "image=pinkFloyd.ppm"
for %%a in (%image%) do set "imageFileSize=%%~za"
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
<nul set /p "=0">zeros.txt
call :bigger zeros.txt

REM Parse height and width from PPM, and obtain headerSize
if exist %image% ( if /i "%image:~-3%" equ "ppm" ( for /f "tokens=*" %%a in (%image%) do (
   set "str=X%%~a" & set "length=0"
   for /l %%b in (5,-1,0) do ( set /a "length|=1<<%%b"
      for /f "tokens=*" %%c in ("!length!") do if "!str:~%%c,1!" equ "" set /a "length&=~1<<%%b" )
   set /a "headerSize+=length + 1"
   if !i! equ 1 for /f "tokens=1,2" %%b in ("%%a") do ( set /a "width=%%b", "height=%%c" )
   if !i! equ 2 goto :break
   set /a "i+=1"
)) else ( echo Format not supported & timeout /t 2 > nul & exit )
) else ( echo File not found & timeout /t 2 > nul & exit )

:break
mode %width%,%height%
REM Parse all data from %image% and collect in hex[] up to 990 characters (divisible by 6)
set /a "expectedOffset=headerSize", "skip=headerSize", "i*=0", "arr=0"
for /f "tokens=1,2 skip=%skip%" %%a in ('fc /b %image% zeros.txt') do if "%%a" neq "FC:" (
   set "offset=%%a"   &   set /a "offset=0x!offset:~0,-1!"
   if !offset! gtr !expectedOffset! (
      set /a "correctedOffset=offset - expectedOffset",   "expectedOffset+=correctedOffset",   "arrC+=correctedOffset"
      for /l %%l in (1,1,!correctedOffset!) do set "hex=!hex!30"
   )
   set "hex=!hex!%%b"   &   set /a "expectedOffset+=1",   "arrC+=1",   "j=arrC %% 495"
   if !j! equ 0 ( set "hex[!arr!]=!hex!"   &   set "hex="   &   set /a "arr+=1" )
)
REM Prepare last of hex[] if any
set "hex[%arr%]=!hex!"

REM Display hex[] in RGB Color
for /l %%a in (0,1,%arr%) do ( if %%a equ %arr% (
      set "str=X!hex[%%a]!" & set "length=0"
      for /l %%b in (10,-1,0) do ( set /a "length|=1<<%%b"
         for /f "tokens=*" %%c in ("!length!") do if "!str:~%%c,1!" equ "" set /a "length&=~1<<%%b" )
   ) else ( set /a "length=990")
   set /a "out=length - 6"
   for /l %%b in (0,6,!out!) do (     set "c=!hex[%%a]:~%%b,6!" & set /a "r=0x!c:~0,2!", "g=0x!c:~2,2!", "b=0x!c:~4,2!"
      <nul set /p "=%ESC%[38;2;!r!;!g!;!b!mÛ%ESC%[0m"
))
del zeros.txt
pause >nul & exit

:bigger
   for %%a in (%~1) do set "zeroSize=%%~za"
   if %zeroSize% LSS %imageFileSize% (
      copy /b %~1+%~1 double%~1 >nul
      del %~1
      ren double%~1 %~1
      goto :bigger
   )
goto :eof

Re: Display .PPM files in RGB colors - WIN 10 ONLY

Posted: 07 Sep 2017 05:42
by dbenham
Great work 8)

I think one way to speed it up would be to use CERTUTIL to get the hex values.

CERTUTIL -encodehex documentation:

Code: Select all

C:\test>certutil -encodehex -?
Usage:
  CertUtil [Options] -encodehex InFile OutFile [type]
  Encode file in hexadecimal

Options:
  -f                -- Force overwrite
  -Unicode          -- Write redirected output in Unicode
  -UnicodeText      -- Write output file in Unicode
  -gmt              -- Display times as GMT
  -seconds          -- Display times with seconds and milliseconds
  -v                -- Verbose operation
  -privatekey       -- Display password and private key data
  -pin PIN                  -- Smart Card PIN
  -sid WELL_KNOWN_SID_TYPE  -- Numeric SID
            22 -- Local System
            23 -- Local Service
            24 -- Network Service

CertUtil -?              -- Display a verb list (command list)
CertUtil -encodehex -?   -- Display help text for the "encodehex" verb
CertUtil -v -?           -- Display all help text for all verbs


Example usage:

Code: Select all

certutil -f -encodehex test.ppm test.ppm.hex


Dave Benham

Re: Display .PPM files in RGB colors - WIN 10 ONLY

Posted: 07 Sep 2017 18:13
by IcarusLives
Thank you Dave! That is much faster! I was unaware of certutils ability to convert to hex, thank you!!

Re: Display .PPM files in RGB colors - WIN 10 ONLY

Posted: 08 Sep 2017 10:02
by einstein1969
Hi IcarusLives!

I not have windows 10 but this is very great. 8)

einstein1969

Re: Display .PPM files in RGB colors - WIN 10 ONLY

Posted: 08 Sep 2017 10:40
by einstein1969
This remember me my old work (not finished?) on old windows.