Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ImageHelper.c
Go to the documentation of this file.
1#ifdef WORKBENCH
2class SCR_ImageHelper
3{
4 protected static string s_sImageFilePath;
5
6 protected static const string DEFAULT_FILE_PATH = "image.png";
7 protected static const string PNG_DOTTED_EXTENSION = ".png";
8 protected static const string DDS_DOTTED_EXTENSION = ".dds";
9 protected static const string EDDS_DOTTED_EXTENSION = ".edds";
10 protected static const string META_DOTTED_EXTENSION = ".meta";
11
12 //------------------------------------------------------------------------------------------------
19 static bool TEMP_SaveImageDataWrapper(string filePath, int width, int height, notnull array<int> data)
20 {
21 int dataCount = data.Count();
22 if (dataCount < 1)
23 {
24 Print("[SCR_ImageHelper.TEMP_SaveImageDataWrapper] provided data is empty", LogLevel.ERROR);
25 return false;
26 }
27
28 if (dataCount != width * height)
29 {
30 Print("[SCR_ImageHelper.TEMP_SaveImageDataWrapper] data / width × height mismatch", LogLevel.ERROR);
31 return false;
32 }
33
34 array<int> dataCopy = {};
35 dataCopy.Resize(dataCount);
36
37 foreach (int i, int argb : data)
38 {
39 int a = argb & 0xFF000000;
40 int b = (argb & 0x000000FF) * 0x10000;
41 int g = argb & 0x0000FF00;
42 int r = (argb & 0x00FF0000) / 0x10000;
43
44 dataCopy[i] = a | b | g | r;
45 }
46
47 return TexTools.SaveImageData(filePath, width, height, dataCopy);
48 }
49
50 //------------------------------------------------------------------------------------------------
53 static array<int> GetImageDataBlackAndWhite(notnull array<float> greyscaleValues01)
54 {
55 array<int> result = {};
56 result.Reserve(greyscaleValues01.Count());
57
58 foreach (float greyscaleValue01 : greyscaleValues01)
59 {
60 if (greyscaleValue01 > 1)
61 greyscaleValue01 = 1;
62 else
63 if (greyscaleValue01 < 0)
64 greyscaleValue01 = 0;
65
66 int greyscaleValue0255 = greyscaleValue01 * 255;
67 result.Insert((greyscaleValue0255 * 0x10000 + greyscaleValue0255 * 0x100 + greyscaleValue0255) | 0xFF000000);
68 }
69
70 return result;
71 }
72
73 //------------------------------------------------------------------------------------------------
77 static array<int> GetImageDataAlphaColour(notnull array<float> greyscaleValues01, Color colour = null)
78 {
79 array<int> result = {};
80 result.Reserve(greyscaleValues01.Count());
81
82 int colourMask;
83 if (colour)
84 colourMask = colour.PackToInt() & 0x00FFFFFF; // drop the alpha
85 else
86 colourMask = 0x00FFFFFF;
87
88 foreach (float greyscaleValue01 : greyscaleValues01)
89 {
90 if (greyscaleValue01 > 1)
91 greyscaleValue01 = 1;
92 else
93 if (greyscaleValue01 < 0)
94 greyscaleValue01 = 0;
95
96 result.Insert(((int)(greyscaleValue01 * 255) * 0x1000000) | colourMask);
97 }
98
99 return result;
100 }
101
102 //------------------------------------------------------------------------------------------------
107 static bool IncreaseResolution(notnull inout array<int> imageData, int width, int height, int factor)
108 {
109 if (factor < 1 || width < 1 || height < 1)
110 return false;
111
112 if (factor == 1)
113 return true;
114
115 int imageDataCount = imageData.Count();
116 if (imageDataCount != width * height)
117 {
118 Print("[SCR_ImageHelper.IncreaseResolution] imageData array size does not match width × height", LogLevel.WARNING);
119 return false;
120 }
121
122 array<int> enhancedData = {};
123 enhancedData.Reserve(imageDataCount * factor * factor);
124
125 for (int rowId; rowId < height; ++rowId)
126 {
127 for (int rowRepeat; rowRepeat < factor; ++rowRepeat)
128 {
129 for (int colId; colId < width; ++colId)
130 {
131 int value = imageData[rowId * width + colId];
132 for (int colRepeat; colRepeat < factor; ++colRepeat)
133 {
134 enhancedData.Insert(value);
135 }
136 }
137 }
138 }
139
140 imageData.Copy(enhancedData);
141
142 return true;
143 }
144
145 //------------------------------------------------------------------------------------------------
152 static bool SaveImageDataToPNG(string filePath, int width, int height, notnull array<int> imageData)
153 {
154 if (imageData.Count() != width * height)
155 {
156 Print("[SCR_ImageHelper.SaveImageDataToPNG] imageData array size does not match width × height", LogLevel.WARNING);
157 return false;
158 }
159
160 filePath.TrimInPlace();
161 if (!filePath)
162 filePath = DEFAULT_FILE_PATH;
163
164 if (!filePath.EndsWith(PNG_DOTTED_EXTENSION))
165 filePath += PNG_DOTTED_EXTENSION;
166
167 string fileWithoutExtension = FilePath.StripExtension(filePath);
168
169 // 1. create DDS texture
170 string absoluteDDSFilePath;
171 if (!Workbench.GetAbsolutePath(fileWithoutExtension + DDS_DOTTED_EXTENSION, absoluteDDSFilePath, false))
172 return false;
173
174 string absoluteEDDSFilePath;
175 if (!Workbench.GetAbsolutePath(fileWithoutExtension + EDDS_DOTTED_EXTENSION, absoluteEDDSFilePath, false))
176 return false;
177
178 if (!TexTools.SaveImageData(absoluteDDSFilePath, width, height, imageData))
179 return false;
180
181 // 2. register texture
182 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
183 if (!resourceManager)
184 return false;
185
186 resourceManager.RegisterResourceFile(absoluteDDSFilePath, true);
187
188 MetaFile eddsMetaFile = resourceManager.GetMetaFile(absoluteEDDSFilePath);
189 if (!eddsMetaFile)
190 return false;
191
192 ArmaReforgerScripted game = GetGame();
193 if (!game)
194 {
195 Print("[SCR_ImageHelper.SaveImageDataToPNG] No Game!", LogLevel.WARNING);
196 return false;
197 }
198
199 WorkspaceWidget workspaceWidget = game.GetWorkspace();
200 if (!workspaceWidget)
201 {
202 Print("[SCR_ImageHelper.SaveImageDataToPNG] No WorkspaceWidget!", LogLevel.WARNING);
203 return false;
204 }
205
206 // 3. create an image widget and load the texture in it
207 ImageWidget imageWidget = ImageWidget.Cast(workspaceWidget.CreateWidget(WidgetType.ImageWidgetTypeID, WidgetFlags.CENTER, Color.White, 0, null));
208 if (!imageWidget)
209 return false;
210
211 if (!imageWidget.LoadImageTexture(0, eddsMetaFile.GetResourceID(), false, true))
212 return false;
213
214 // 4. get PixelRawData and save to PNG
215 s_sImageFilePath = filePath;
216 if (!imageWidget.GetTextureRawData(0, GetTextureRawDataCallbackMethod)) // saves the image to s_sImageFilePath
217 {
218 Print("[SCR_ImageHelper.SaveImageDataToPNG] Cannot get texture raw data", LogLevel.WARNING);
219 return false;
220 }
221
222 FileIO.DeleteFile(absoluteDDSFilePath);
223 FileIO.DeleteFile(absoluteDDSFilePath + META_DOTTED_EXTENSION);
224 FileIO.DeleteFile(absoluteEDDSFilePath);
225 FileIO.DeleteFile(absoluteEDDSFilePath + META_DOTTED_EXTENSION);
226
227 return true;
228 }
229
230 //------------------------------------------------------------------------------------------------
231 protected static void GetTextureRawDataCallbackMethod(PixelRawData data, int imageWidth, int imageHeight, int stride)
232 {
233// PrintFormat("Saving to %1 (%2x%3 stride %4)", s_sImageFilePath, imageWidth, imageHeight, stride);
234 Workbench.SavePixelRawData(s_sImageFilePath, data, imageWidth, imageHeight, stride);
235 }
236}
237#endif // WORKBENCH
ref DSGameConfig game
Definition DSConfig.c:81
ArmaReforgerScripted GetGame()
Definition game.c:1398
Get all prefabs that have the spawner data
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
WidgetFlags
Widget flags. See enf::Widget::SetFlags().
Definition WidgetFlags.c:14
TypeID WidgetType
Definition EnWidgets.c:6