| 1 |
{ |
| 2 |
Copyright 2001-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Convert one image to another. |
| 17 |
See README.md for details. } |
| 18 |
|
| 19 |
program image_convert; |
| 20 |
|
| 21 |
uses SysUtils, CastleUtils, CastleImages, CastleParameters; |
| 22 |
|
| 23 |
var |
| 24 |
{ required params } |
| 25 |
InputImageName, OutputImageName: string; |
| 26 |
|
| 27 |
{ optional params } |
| 28 |
ResizeX: Cardinal = 0; |
| 29 |
ResizeY: Cardinal = 0; |
| 30 |
FloatScale: Single = 1.0; |
| 31 |
FloatGamma: Single = 1.0; |
| 32 |
WasParam_GrayScale: boolean = false; |
| 33 |
Param_ConvertToChannel: Integer = -1; { -1 means "don't convert" } |
| 34 |
Param_StripToChannel: Integer = -1; { -1 means "don't convert" } |
| 35 |
WasParam_AlphaBleed: boolean = false; |
| 36 |
|
| 37 |
const |
| 38 |
Options: array [0..6] of TOption = |
| 39 |
( |
| 40 |
(Short:'h'; Long:'help'; Argument: oaNone), |
| 41 |
(Short:'s'; Long:'scale'; Argument: oaRequired), |
| 42 |
(Short:'g'; Long:'gamma'; Argument: oaRequired), |
| 43 |
(Short:#0 ; Long:'grayscale'; Argument: oaNone), |
| 44 |
(Short:#0 ; Long:'convert-to-channel'; Argument: oaRequired), |
| 45 |
(Short:#0 ; Long:'strip-to-channel'; Argument: oaRequired), |
| 46 |
(Short:#0 ; Long:'alpha-bleed'; Argument: oaNone) |
| 47 |
); |
| 48 |
|
| 49 |
procedure OptionProc(OptionNum: Integer; HasArgument: boolean; |
| 50 |
const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer); |
| 51 |
begin |
| 52 |
case OptionNum of |
| 53 |
0: begin |
| 54 |
Writeln( |
| 55 |
'image_convert <inputfile> <outputfile> [<resize-x> <resize-y>]' +nl+ |
| 56 |
'will convert image in <inputfile> to image in <outputfile>.' +nl+ |
| 57 |
'Format of both images will be recognized from file extension.' +nl+ |
| 58 |
'<inputfile> and <outputfile> may be the same file, it doesn''t matter.' +nl+ |
| 59 |
'' +nl+ |
| 60 |
'If <resize-x> <resize-y> parameters are given then output image' +nl+ |
| 61 |
'is scaled to that size. Size = 0 means "do not scale in this dimension",' +nl+ |
| 62 |
'e.g. <resize-x> <resize-y> = 200 0 means "scale x dimension to 200' +nl+ |
| 63 |
'and leave y dimension as is". (effectively, specifying' +nl+ |
| 64 |
'<resize-x> <resize-y> = 0 0 has the same effect as omitting these' +nl+ |
| 65 |
'parameters).' +nl+ |
| 66 |
'' +nl+ |
| 67 |
'Additional params with no fixed position:' +nl+ |
| 68 |
'-s <float> or' +nl+ |
| 69 |
'--scale <float>' +nl+ |
| 70 |
' Valid only if input file has a float format (this is currently' +nl+ |
| 71 |
' the case only for RGBE (Radiance) image format).' +nl+ |
| 72 |
' Effect : before writing output image, scales each pixel color' +nl+ |
| 73 |
' (it''s red, green and blue value) by <float>.' +nl+ |
| 74 |
' Multiple --scale *cummulate* : e.g.' +nl+ |
| 75 |
' "--scale 1.5 --scale 2" would have the same effect as' +nl+ |
| 76 |
' "--scale 3".' +nl+ |
| 77 |
'-g <float>' +nl+ |
| 78 |
'--gamma <float>' +nl+ |
| 79 |
' Similiar to --scale - valid only when input has float precision,' +nl+ |
| 80 |
' multiple params are cummulated, default is 1.0.' +nl+ |
| 81 |
' Each component is raised to 1/<float>.' +nl+ |
| 82 |
'' +nl+ |
| 83 |
'--grayscale' +nl+ |
| 84 |
' Convert to grayscale.' +nl+ |
| 85 |
'--convert-to-channel 0|1|2' +nl+ |
| 86 |
' Converts colors to red / green / blue channel,' +nl+ |
| 87 |
' it''s like converting to grayscale and then' +nl+ |
| 88 |
' writing output to only one channel.' +nl+ |
| 89 |
'--strip-to-channel 0|1|2' +nl+ |
| 90 |
' Strips colors to only one channel, i.e.' +nl+ |
| 91 |
' sets to zero intensities of other two channels.' +nl+ |
| 92 |
''); |
| 93 |
Halt; |
| 94 |
end; |
| 95 |
1: FloatScale := FloatScale * StrToFloatDot(Argument); |
| 96 |
2: FloatGamma := FloatGamma * StrToFloatDot(Argument); |
| 97 |
3: WasParam_GrayScale := true; |
| 98 |
4: Param_ConvertToChannel := StrToInt(Argument); |
| 99 |
5: Param_StripToChannel := StrToInt(Argument); |
| 100 |
6: WasParam_AlphaBleed := true; |
| 101 |
else raise EInternalError.Create('option not impl'); |
| 102 |
end; |
| 103 |
end; |
| 104 |
|
| 105 |
var |
| 106 |
{ helper variables } |
| 107 |
Img: TCastleImage; |
| 108 |
begin |
| 109 |
{ parse free position params } |
| 110 |
Parameters.Parse(Options, @OptionProc, nil); |
| 111 |
|
| 112 |
{ parse fixed position params } |
| 113 |
if Parameters.High = 4 then |
| 114 |
begin |
| 115 |
ResizeX := StrToInt(Parameters[3]); Parameters.Delete(3); |
| 116 |
ResizeY := StrToInt(Parameters[3]); Parameters.Delete(3); |
| 117 |
end; |
| 118 |
Parameters.CheckHigh(2); |
| 119 |
InputImageName := Parameters[1]; |
| 120 |
OutputImageName := Parameters[2]; |
| 121 |
|
| 122 |
{ do work } |
| 123 |
Img := LoadImage(InputImageName, [], ResizeX, ResizeY); |
| 124 |
try |
| 125 |
if FloatScale <> 1.0 then |
| 126 |
begin |
| 127 |
Check(Img is TRGBFloatImage, '--scale <> 1.0 but input image is not in a float (like Radiance RGBE) format'); |
| 128 |
(Img as TRGBFloatImage).ScaleColors(FloatScale); |
| 129 |
end; |
| 130 |
if FloatGamma <> 1.0 then |
| 131 |
begin |
| 132 |
Check(Img is TRGBFloatImage, '--gamma <> 1.0 but input image is not in a float (like Radiance RGBE) format'); |
| 133 |
(Img as TRGBFloatImage).ExpColors(1/FloatGamma); |
| 134 |
end; |
| 135 |
|
| 136 |
if WasParam_GrayScale then |
| 137 |
Img.Grayscale; |
| 138 |
if Param_ConvertToChannel <> -1 then |
| 139 |
Img.ConvertToChannelRGB(Param_ConvertToChannel); |
| 140 |
if Param_StripToChannel <> -1 then |
| 141 |
Img.StripToChannelRGB(Param_StripToChannel); |
| 142 |
if WasParam_AlphaBleed then |
| 143 |
Img.AlphaBleed('Alpha Bleeding'); |
| 144 |
|
| 145 |
SaveImage(Img, OutputImageName); |
| 146 |
finally Img.Free end; |
| 147 |
end. |