LUADOC - Farming Simulator 22

BitmapUtil

Description
Util for bitmap operations
Functions

writeBitmapToFile

Description
Saves a table of pixels into a portable anymap (pnm)
Definition
writeBitmapToFile(table pixels, int width, int height, string filepath, int imageFormat)
Arguments
tablepixelstable of pixels, each pixel is a table
intwidthimage width in pixels
intheightimage height in pixels
stringfilepathfilepath for pnm
intimageFormatimage format, one of BitmapUtil.FORMAT
Code
57function 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
96end

writeBitmapToFileFromIterator

Description
Saves pixels into a portable anymap (pnm)
Definition
writeBitmapToFileFromIterator(function iterator, int width, int height, string filepath, int imageFormat)
Arguments
functioniteratoriterator function returning the next pixel
intwidthimage width in pixels
intheightimage height in pixels
stringfilepathfilepath for pnm
intimageFormatimage format, one of BitmapUtil.FORMAT
Code
105function 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
147end