Tuesday, August 24, 2021

Converting animated GIF to animated BPG

 The batch file below converts animated GIF to animated BPG. The main point to note is that when extracting frames from the animated GIF, transparency must be removed and the frames must have the same color type, otherwise BpgEnc will crash. Hence PNG24 must be specified to force removal of transparency and ensure the same color type.

@echo off

@Echo %~n0 converts animated GIF to BPG

md ~temp

if not '%1==' call :gif2bpg %*

if '%1==' For %%v in (*.gif) do if not exist "%%~nv.bpg" call :Gif2Bpg "%%v"

if '%1==' For %%v in (*.jpg *.png) do C:\PUB\bpg\bpgenc.exe "%%v" -o "%%~nv.bpg"

rd ~temp

goto :Eof


:Gif2Bpg

:: Coalesce and adjoin is needed to extract individual full frames from animated gif 

:: Extracted PNG must have same color type and without transparency otherwise BPGEnc will crash

Echo Using GraphicsMagick to extract PNG frames from %1 with same color depth, without transparency and coalescing previous frames

"C:\Program Files\GraphicsMagick-1.3.34-Q16\gm.exe" convert -coalesce +adjoin %1 PNG24:"~temp\%~n1_%%03d.png"


Echo Using GraphicsMagick to retrieve animation frame info from %1 and saving it to %~n1.anim

"C:\Program Files\GraphicsMagick-1.3.34-Q16\gm.exe" identify -format "%%T\n" %1 > "~temp\%~n1.anim"


Echo Using BpgEnc to convert extracted PNG frames to %~n1.bpg

bpgenc.exe -a "~temp\%~n1_%%3d.png" -fps 25 -loop 0 -delayfile "~temp\%~n1.anim" -o "%~n1.bpg"


del ~temp\%~n1*.*


Sunday, July 29, 2018

Enabling IE9 rendering of HTMLHelp (chm) files

Chm files are rendered in IE7 mode by default. This is a problem if you want to use CSS3 features such as box-shadow and rounded corners. To enable compatibility with CSS3, add

<meta http-equiv="X-UA-Compatible" content="IE=9" />

right after the tag <head>, e.g.
e.g. <html><head><meta http-equiv="X-UA-Compatible" content="IE=9" />

As a bonus, here is a simple batch file to quickly create a very basic .chm file. This batch file can be used to convert a webpage that is saved using Save Complete in your browser into a standalone chm file. If the resulting chm file does not display properly, try adding
<meta http-equiv="X-UA-Compatible" content="IE=9" />
to the source html file.

::@echo off
if "%HH%"=="" set HH=C:\Program Files\HTML Help Workshop\HHC.EXE
if not exist "%HH%" set HH=C:\Program Files (x86)\HTML Help Workshop\HHC.EXE
if "%~1"=="" goto default
if %1==/? goto help
if exist "%~1\*" goto dir
if exist %1 goto %~x1
exit

:help
echo HHC
echo     Attempt to make a chm out of current directory
echo HHC x.htm
echo    Attempt to make a chm out of x.htm
echo HHC x.hhp
echo    Compiles x.hhp
echo.
echo Uses environment variable HH to compile chm
echo Default for HH is: C:\Program Files\HTML Help Workshop\HHC.EXE

:dir
set d=%~1
set f=%d:~-6%
set d=%d:~0,-6%
if "%f%"=="_files" if exist "%d%.htm" %0 "%d%.htm"
if not exist "%~1\*.hhp" call :hhp %1
for %%v in ("%~1\*.hhp") do "%HH%" "%%v"
exit

:default
if not exist *.hhp call :hhp "%CD%"
for %%v in (*.hhp) do "%HH%" "%%v"
exit

:hhp
for %%v in (index.html index.htm main.html main.htm %~nx1.htm *.htm) do if exist "%%v" (set SRC=%%v) & goto :make
echo Error: No Html files found
exit

:make
set DEST="%tmp%\%~nx1.hhp"
>%DEST% echo [OPTIONS]
::if not exist index.hhk >>%DEST% echo Auto Index=Yes
::if not exist toc.hhc >>%DEST% echo Auto TOC=9
::>>%DEST% echo Binary TOC=Yes
>>%DEST% echo Compatibility=1.1 or later
>>%DEST% echo Compiled file="%~nx1.chm"
::>>%DEST% echo Contents file=toc.hhc
>>%DEST% echo Default topic=%SRC%
::>>%DEST% echo Default Window=Main
>>%DEST% echo Display compile progress=No
if exist index.hhk >>%DEST% echo Index file=Index.hhk
>>%DEST% echo Language=0x409 English (United States)
>>%DEST% echo Title=%~n1
::>>%DEST% echo.
::>>%DEST% echo [WINDOWS]
::if exist index.hhk >>%DEST% echo echo Main="%~n1",toc.hhc,index.hhk,"%SRC%","%SRC%",,,,,0x3520,,0x60384e,,,,,,,,0
::>>%DEST% echo Main="%~n1",toc.hhc,,"%SRC%","%SRC%",,,,,0x3520,,0x60384e,,,,,,,,0
>>%DEST% echo.
>>%DEST% echo [FILES]
>>%DEST% dir /b /s /a-d %1
::>>%DEST% echo.
::>>%DEST% echo [INFOTYPES]
move "%tmp%\%~nx1.hhp" %1
goto :eof

:.htm
:.html
set SRC=%1
set DEST="%tmp%\%~n1.hhp"
>%DEST% echo [OPTIONS]
>>%DEST% echo Compatibility=1.1 or later
>>%DEST% echo Compiled file=%~nx1.chm
>>%DEST% echo Default topic=%~1
>>%DEST% echo Display compile progress=No
>>%DEST% echo Language=0x409 English (United States)
>>%DEST% echo Title=%~n1
>>%DEST% echo.
>>%DEST% echo [FILES]
>>%DEST% echo %~1
>>%DEST% dir /b /s /a-d "%~n1%_files"
move "%tmp%\%~n1.hhp" .
"%HH%" "%~n1.hhp"
goto :eof

:.hhp
"%HH%" %1
goto :eof

Wednesday, August 31, 2016

80x86 snippets

Here are some branchless routines in 80x86 assembly:

Here is a simple routine to get the absolute difference between AL & BL
AL, DL destroyed, absolute difference in AL
sub al, bl ; AL to BL ; positive difference negative differencesbb dl, dl ; DL=0  DL=-1 xor al, dl ; AL xor 0 = AL AL xor -1 = not ALsub al, dl ; AL - 0 = AL not AL --1 = neg AL

Instead of sbb, one can also use cbw (sign extend AL into AH), cwd (sign extend AX into DX) and cdq (sign extend EAX into EDX). See below:
cdq ; sign extend EAX into EDX ( = -1 if EAX negative)
xor eax, edx
sub eax, edx
The last 2 lines can also be re-arranged so:
cdq
add eax, edx
xor eax, edx

 For maximum of 2 numbers, one can use
; AL = max(AH, AL)  ; AL > AH AL < AH
sub ah, al ; AH = AH - AL
cmc
sbb dl, dl ; DL = 0 DL = AH - AL
and dl, ah
add al, dl ; AL = AL AL = AL + AH - AL
For minimum of 2 numbers, one can use
sub     ebx,eax
sbb     edx,edx
and     edx,ebx
add     eax,edx

Monday, November 30, 2015

Installing drivers for Kodak All-In-One printers in Windows

Had to recently re-install drivers for a Kodak AIO printer after removing the re-image adware. However, despite detecting the printer, the driver installation always fails, even after removing the previous drivers. Eventually I figured out it was because Windows automatically tries to install the device as a scanner, thus preventing the device from being installed as an AIO printer. The solution: Disable automatic device detection first before installing the driver.

Sunday, August 30, 2015

Far Manager Multiarc custom.ini for zpaq, nanozip

ZPAQ.EXE lacks delete and extract without path functionality, so those don't work

[zpaq]    ;For zpaq v7.05 journaling archiver, compiled Apr 17 2015
ID=37 6B 53 74 A0 31 83 D3 8C B2 28 B0 D3
IDPos=0
Extension=zpaq
Description="ZPAQ archiver"
Start=" MB"
Format0="  yyyy tt dd hh mm ss  zzzzzzzzzzz aaaaa nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
End=" files) shown"
Extract=zpaq.exe x %%AQ %%FQ
ExtractWithoutPath=zpaq.exe e %%AQ %%FQ -to %%FQ
Test=zpaq.exe t %%AQ {-key %%P}
Delete=zpaq.exe d %%AQ %%FQ {-key %%P}
List=zpaq.exe l %%AQ
Add=zpaq.exe a %%AQ %%FQ -method 5 {-key %%P}
AllFilesMask="*.*"

[NANOZIP]
TypeName=Nanozip
Extension=nz
ID=AE 01 4E 61
Start="^checksum"
End="^Total of "
Format0="rrrrrrrr yyyy-TTT-dd hh:mm:ss zz zzz zzz zzz  nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
List=nz.exe l -v %%AQ
ExtractWithoutPath=nz.exe x -sp %%AQ %%FQ
Extract=nz.exe x %%AQ %%FQ
Test=nz.exe t %%AQ
Add=nz.exe a %%AQ %%FQ
AllFilesMask="*"

Saturday, August 30, 2014

Setting default audio & subtitle in MKV files using a batch file.

Matroska (mkv) files are video files that can contain multiple tracks, allowing for multiple subtitles and audios, and one can set a default audio & subtitle for those.

mkvtoolnix is a collection of tools (mkvmerge, mkvmerge gui [mmg], mkvinfo, mkvextract, and mkvpropedit) for matroska (mkv) files.

The batch file below allows one to view the default flag for the tracks in a mkv file and then toggle the default flag on/off. Copy the text block below and save it to a batch file, say MKV.BAT and put that bat file somewhere in your path (e.g. C:\WINDOWS).

@echo off
if '%1==' goto ?
::change code page to utf8 (mkvinfo always writes utf8 when redirected)
set track=
set mkvtoolnix=%ProgramFiles%\mkvtoolnix
%windir%\system32\chcp 65001>nul
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
if '%2==' goto info

set flag=unknown
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :flag %2 %%a %%b %%c %%d %%e %%f %%g %%h %%i
echo Track %2 (%name% %type%) default flag is %flag%
if %flag%==1 goto unset
echo Now setting flag to 1
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=1
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
goto info

:unset
echo Now setting flag to 0
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=0
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt

:info
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :filter %%a %%b %%c %%d %%e %%f %%g %%h %%i
if not "%track%"=="" echo %track%
goto :eof

:filter
if '%2==' goto :eof
for %%v in (Content A Pixel Display (size: Lacing CodecPrivate size head Timecode Muxing Writing Segment Sampling Channels version: read maximum type type: Duration: Date: MinCache: Video Audio Algorithm: Max Order: Scope: Type: Interlaced: Enabled: Forced) do if %1==%%v goto :eof
for %%v in (EbmlVoid Chapters EditionEntry EditionFlagHidden: EditionFlagDefault: EditionUID: ChapterAtom ChapterUID: ChapterTimeStart: ChapterFlagHidden: ChapterFlagEnabled: ChapterDisplay ChapterString: ChapterLanguage: Cluster) do if %1==%%v goto :eof
for %%v in (UID: duration: decode) do if %2==%%v goto :eof
if %2==number: goto :track
if "%track%"=="" goto :eof
if %2==type: (set track=%track% %3) & goto :eof
if %2==ID: (set track=%track%, %3) & goto :eof
set track=%track%, %*
goto :eof

:track
if not "%track%"=="" echo %track%
if %3 LSS 10 (set track=Track %3: ) else set track=Track %3:
goto :eof

:flag
if '%3==' goto :eof
if %3==number: if '%1=='%4 (set track=1) else set track=
if '%track%==' goto :eof
if %2==Name: set name=%3
if %2==Default if %3==flag: set flag=%4
if %2==Track if %3==type: set type=%4
goto :eof

:?
echo MKV mkvfile [track number]
echo.
echo If track number not specified, the tracks will be listed
echo.
echo If track number specified, the default flag will be toggled for that track.

You will need to set the path to mkvtoolnix appropriately (edit the line set mkvtoolnix=...) to point to the mkvtoolnix folder.

Remember if you set a default flag on for one audio track, remember to unset the default flag for any other audio track that had the default flag on.

Monday, July 14, 2014

Compiling swftools on Windows

Compiling swftools on Windows...

1. Download swftools-master.zip

2. Download mingw-get-setup.exe from www.mingw.org, run it and install base, msys & c++ to C:\MINGW

3. Download from http://gnuwin32.sourceforge.net/packages.html and install to C:\MINGW\GNUWIN32:
bison-2.4.1-setup.exe
flex-2.5.4a-1.exe
freetype-2.3.5-1-setup.exe
gettext-0.14.4.exe
giflib-4.1.4-1.exe
jpeg-6b-4.exe
libgw32c-0.4.exe
libiconv-1.9.2-1.exe
libpng-1.2.37-setup.exe
pdflib-lite-6.0.2.exe
t1lib-5.1.0.exe
yasm-1.1.0-win32.exe
zlib-1.2.3.exe

4. Download and compile lame

5. Download  fontconfig-2.2.2-20040412.zip,   fontconfig-dev-2.2.2-20040412.zip
    and extract to c:\mingw\fontconfig

6. Create C:\MinGW\msys\1.0\etc\fstab and put the following lines in:
c:\mingw /mingw
c:\mingw\gnuwin32 /gw32
c:\mingw\fontconfig /fc

7. Extract swftools to C:\MinGW\msys\1.0\home\

8. Run msys
cd ..
cd swftools
.\configure
make

8. If gcc complains about ..
- #define boolean int  in config.h -- just comment out that line
- not found -- copy lib\pdf\aconf.h to lib\pdf\xpdf directory and edit the line 
   #include "../../config.h" in xpdf\aconf.h into #include "../../../config.h"
- "cmyk.h" not found -- edit GfxState.cc and change "cmyk.h" to "../cmyk.h"
extern "C" int unlink(char *filename); definition conflict in stream.cc -- comment out that line

9. To compile gfx2gfx (exampel usage: gfx2gfx infile.swf -o outfile.pdf)
- edit swftools\src\gfx2gfx.c and insert the line       out->setparameter(out, "maxdpi", "0");
   below the line           gfxdevice_pdf_init(out);
- edit swftools\src\makefile and append gfx2gfx$(E) to the end of the line
   programs = $(install_programs) swfbytes$(E) ttftool$(E)
- run make again

note: pdf2pdf also accepts swf input and sends to pdf output

References: 
http://lists.gnu.org/archive/html/swftools-common/2005-02/msg00023.html
http://sudofixit.blogspot.com/2014/02/guide-convert-book-style-swfs-to-pdfs.html
http://go.kblog.us/2009/08/mingw-to-compile-lame-for-windows.html

Other:
If you want to build pdflib lite, and get
1) DWORD errors: add to pc_util.c
    typedef unsigned int DWORD;
2) png dereferencing errors: change #include "png.h" in p_image.h to
   #include "..\png\png.h"
(this error is caused by gcc reading a different (newer) version of libpng that may be installed elsewhere)
and similarly search and replace #include "tiffio.h" to #include "..\tiff\tiffio.h"
and #include "zlib.h" to #include "..\flate\zlib.h"