Script v1_7_1_0
- AI
- Animals
- Collections
- Contracts
- Debug
- Economy
- Elements
- EnvironmentalScore
- Errors
- Events
- GUI
- Handtools
- Hud
- I3d
- Input
- Jobs
- Maps
- Materials
- Misc
- Objects
- Parameters
- Placeables
- Placement
- Player
- Shop
- Sounds
- Specialization
- Specializations
- StateMachine
- Statistics
- Tasks
- Triggers
- Utils
- Vehicles
Engine v1_7_1_0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- general
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- NoteNode
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- VoiceChat
- XML
Foundation Reference
BitmapUtil
DescriptionUtil for bitmap operationsFunctions
writeBitmapToFile
DescriptionSaves a table of pixels into a portable anymap (pnm)Definition
writeBitmapToFile(table pixels, int width, int height, string filepath, int imageFormat)Arguments
table | pixels | table of pixels, each pixel is a table |
int | width | image width in pixels |
int | height | image height in pixels |
string | filepath | filepath for pnm |
int | imageFormat | image format, one of BitmapUtil.FORMAT |
57 | function BitmapUtil.writeBitmapToFile(pixels, width, height, filepath, imageFormat) |
58 | local maxBrightness = 255 |
59 | |
60 | local pnmFormat = BitmapUtil.FORMAT_TO_PNM[imageFormat] |
61 | |
62 | if pnmFormat == nil then |
63 | Logging.error("Invalid image format '%s'. Use one of BitmapUtil.FORMAT", imageFormat) |
64 | return false |
65 | end |
66 | |
67 | filepath = string.format("%s.%s", filepath, pnmFormat.extension or "pnm") |
68 | local file = createFile(filepath, FileAccess.WRITE) |
69 | if file == 0 then |
70 | Logging.error("BitmapUtil.writeBitmapToFile(): Unable to create file '%s'", filepath) |
71 | return false |
72 | end |
73 | |
74 | -- Portable Anymap (PNM) Header |
75 | fileWrite(file, pnmFormat.getHeader(width, height, maxBrightness)) |
76 | |
77 | local pixelsToWrite = {} |
78 | local concatFunc, clearFunc = table.concat, table.clear |
79 | |
80 | for pixelIndex=1, #pixels do |
81 | pixelsToWrite[#pixelsToWrite+1] = pnmFormat.getPixel(pixels[pixelIndex]) |
82 | if pixelIndex % 1025 == 0 then |
83 | fileWrite(file, concatFunc(pixelsToWrite)) |
84 | clearFunc(pixelsToWrite) |
85 | end |
86 | end |
87 | |
88 | if #pixelsToWrite > 0 then |
89 | fileWrite(file, concatFunc(pixelsToWrite)) |
90 | end |
91 | |
92 | delete(file) |
93 | |
94 | Logging.info("Wrote bitmap (width=%d, height=%d) to '%s'", width, height, filepath) |
95 | return true |
96 | end |
writeBitmapToFileFromIterator
DescriptionSaves pixels into a portable anymap (pnm)Definition
writeBitmapToFileFromIterator(function iterator, int width, int height, string filepath, int imageFormat)Arguments
function | iterator | iterator function returning the next pixel |
int | width | image width in pixels |
int | height | image height in pixels |
string | filepath | filepath for pnm |
int | imageFormat | image format, one of BitmapUtil.FORMAT |
105 | function BitmapUtil.writeBitmapToFileFromIterator(iterator, width, height, filepath, imageFormat) |
106 | local pnmFormat = BitmapUtil.FORMAT_TO_PNM[imageFormat] |
107 | |
108 | if pnmFormat == nil then |
109 | Logging.error("Invalid image format '%s'. Use one of BitmapUtil.FORMAT", imageFormat) |
110 | return false |
111 | end |
112 | |
113 | filepath = string.format("%s.%s", filepath, pnmFormat.extension or "pnm") |
114 | local file = createFile(filepath, FileAccess.WRITE) |
115 | if file == 0 then |
116 | Logging.error("BitmapUtil.writeBitmapToFileFromIterator(): Unable to create file '%s'", filepath) |
117 | return false |
118 | end |
119 | |
120 | -- Portable Anymap (PNM) Header |
121 | local maxBrightness = 255 |
122 | fileWrite(file, pnmFormat.getHeader(width, height, maxBrightness)) |
123 | |
124 | local pixelsToWrite = {} |
125 | local pixelIndex = 1 |
126 | local concatFunc, clearFunc = table.concat, table.clear |
127 | |
128 | for pixel in iterator() do |
129 | pixelsToWrite[#pixelsToWrite+1] = pnmFormat.getPixel(pixel) |
130 | |
131 | if pixelIndex % 1025 == 0 then -- only write to file every 1024 pixels |
132 | fileWrite(file, concatFunc(pixelsToWrite)) |
133 | clearFunc(pixelsToWrite) |
134 | end |
135 | |
136 | pixelIndex = pixelIndex + 1 |
137 | end |
138 | |
139 | if #pixelsToWrite > 0 then |
140 | fileWrite(file, concatFunc(pixelsToWrite)) |
141 | end |
142 | |
143 | delete(file) |
144 | |
145 | Logging.info("Wrote bitmap (width=%d, height=%d) to '%s'", width, height, filepath) |
146 | return true |
147 | end |