18 static ULONG_PTR m_gdiplusToken;
19 static HBITMAP bmpdib;
20 static CLSID encoderClsid;
27 static unsigned char* bmf, *bmPixels, *bmp;
30 extern char OutFile[1024], OutFileBase[1024];
36 static double logMaxPop;
40 mi = (int)(P.bwidth * P.bheight);
45 for (
int i = 0; i < mi; i++) bmPopulation[i] = 0;
46 for (
int i = 0; i < P.
PopSize; i++)
48 x = ((int)(Households[Hosts[i].hh].loc_x * P.scalex)) - P.bminx;
49 y = ((
int)(Households[Hosts[i].hh].loc_y * P.scaley)) - P.bminy;
50 if ((x >= 0) && (x < P.bwidth) && (y >= 0) && (y < P.bheight))
52 j = y * bmh->width + x;
53 if ((j < bmh->imagesize) && (j >= 0))
56 if (bmPopulation[j] > maxPop) maxPop = bmPopulation[j];
60 logMaxPop = log(1.001 * (
double)maxPop);
61 for (
int i = 0; i < P.NMC; i++)
65 if ((i < P.NMC - 1) && (i / P.get_number_of_micro_cells_high() == (i + 1) / P.get_number_of_micro_cells_high()) && (Mcells[i + 1].n > 0) && ((Mcells[i].country != Mcells[i + 1].country)
66 || ((P.DoAdunitBoundaryOutput) && ((AdUnits[Mcells[i].adunit].id % P.AdunitLevel1Mask) / P.AdunitBitmapDivisor != (AdUnits[Mcells[i + 1].adunit].id % P.AdunitLevel1Mask) / P.AdunitBitmapDivisor)))) f = 1;
67 if ((i > 0) && (i / P.get_number_of_micro_cells_high() == (i - 1) / P.get_number_of_micro_cells_high()) && (Mcells[i - 1].n > 0) && (Mcells[i].country != Mcells[i - 1].country)) f = 1;
68 if ((i < P.NMC - P.get_number_of_micro_cells_high()) && (Mcells[i + P.get_number_of_micro_cells_high()].n > 0) && ((Mcells[i].country != Mcells[i + P.get_number_of_micro_cells_high()].country)
69 || ((P.DoAdunitBoundaryOutput) && ((AdUnits[Mcells[i].adunit].id % P.AdunitLevel1Mask) / P.AdunitBitmapDivisor != (AdUnits[Mcells[i + P.get_number_of_micro_cells_high()].adunit].id % P.AdunitLevel1Mask) / P.AdunitBitmapDivisor)))) f = 1;
70 if ((i >= P.get_number_of_micro_cells_high()) && (Mcells[i - P.get_number_of_micro_cells_high()].n > 0) && (Mcells[i].country != Mcells[i - P.get_number_of_micro_cells_high()].country)) f = 1;
73 x = (int)(P.
in_microcells_.
width_ * (((
double)(i / P.get_number_of_micro_cells_high())) + 0.5) * P.scalex) - P.bminx;
74 y = (
int)(P.
in_microcells_.
height_ * (((double)(i % P.get_number_of_micro_cells_high())) + 0.5) * P.scaley) - P.bminy;
75 if ((x >= 0) && (x < P.bwidth) && (y >= 0) && (y < P.bheight))
77 j = y * bmh->width + x;
78 if ((j < bmh->imagesize) && (j >= 0)) bmPopulation[j] = -1;
82 for (
int i = 0; i < P.bwidth / 2; i++)
84 prev = floor(3.99999 * ((
double)i) * BWCOLS / ((
double)P.bwidth) * 2);
86 for (j = 0; j < 10; j++)
88 bmPixels[(j + P.bheight + 5) * bmh->width + P.bwidth / 4 + i] = f;
92 #pragma omp parallel for schedule(static,5000) default(none) \ 93 shared(mi, bmPixels, bmPopulation, bmInfected, bmTreated, bmRecovered, logMaxPop) 94 for (
int i = 0; i < mi; i++)
96 if (bmPopulation[i] == -1)
97 bmPixels[i] = BWCOLS - 1;
98 else if (bmInfected[i] > 0)
99 bmPixels[i] = (
unsigned char)(BWCOLS + BWCOLS * log((
double)bmInfected[i]) / logMaxPop);
100 else if (bmTreated[i] > 0)
101 bmPixels[i] = (
unsigned char)(2 * BWCOLS + BWCOLS * log((
double)bmTreated[i]) / logMaxPop);
102 else if (bmRecovered[i] > 0)
103 bmPixels[i] = (
unsigned char)(3 * BWCOLS + BWCOLS * log((
double)bmRecovered[i]) / logMaxPop);
104 else if (bmPopulation[i] > 0)
105 bmPixels[i] = (
unsigned char)(BWCOLS * log((
double)bmPopulation[i]) / logMaxPop);
111 void OutputBitmap(
int tp)
113 char buf[3000], OutF[3000];
115 static int cn1 = 0, cn2 = 0, cn3 = 0, cn4 = 0;
117 char *OutBaseName = strrchr(OutFile,
'/');
118 char *OutBaseName2 = strrchr(OutFile,
'\\');
119 if (OutBaseName2 !=
nullptr && (OutBaseName ==
nullptr || OutBaseName2 > OutBaseName)) {
120 OutBaseName = OutBaseName2;
122 if (OutBaseName ==
nullptr) {
123 OutBaseName = OutFile;
130 sprintf(OutF,
"%s.ge" DIRECTORY_SEPARATOR
"%s", OutFile, OutBaseName);
136 sprintf(OutF,
"%s.ge" DIRECTORY_SEPARATOR
"Mean.%s", OutFile, OutBaseName);
142 sprintf(OutF,
"%s.ge" DIRECTORY_SEPARATOR
"Min.%s", OutFile, OutBaseName);
148 sprintf(OutF,
"%s.ge" DIRECTORY_SEPARATOR
"Max.%s", OutFile, OutBaseName);
151 if (P.BitmapFormat == BF_PNG)
155 using namespace Magick;
156 fprintf(stderr,
"\noutputing ImageMagick stuff");
157 sprintf(buf,
"%s.bmp", OutF);
158 if (!(dat = fopen(buf,
"wb"))) ERR_CRITICAL(
"Unable to open bitmap file\n");
161 fwrite_big((
void*)bmf,
sizeof(bitmap_header), 1, dat);
162 for (
int i = 0; i < bmh->imagesize; i++) fputc(bmPixels[i], dat);
165 sprintf(buf,
"%s.%d.png", OutF, j);
166 ColorRGB white(1.0, 1.0, 1.0);
167 bmap.transparent(white);
169 #elif defined(_WIN32) 171 using namespace Gdiplus;
180 Bitmap* gdip_bmp = Bitmap::FromHBITMAP(bmpdib, NULL);
182 if ((cn1 == 1) && (tp == 0))
185 static ColorPalette* palette;
186 palsize = gdip_bmp->GetPaletteSize();
187 palette = (ColorPalette*)malloc(palsize);
188 if (!palette) ERR_CRITICAL(
"Unable to allocate palette memory\n");
189 (void)gdip_bmp->GetPalette(palette, palsize);
190 palette->Flags = PaletteFlagsHasAlpha;
191 palette->Entries[0] = 0x00ffffff;
192 gdip_bmp->SetPalette(palette);
195 sprintf(buf,
"%s.%05i.png", OutF, j + 1);
196 mbstowcs_s(&a, wbuf, strlen(buf) + 1, buf, _TRUNCATE);
197 gdip_bmp->Save(wbuf, &encoderClsid, NULL);
200 fprintf(stderr,
"Do not know how to output PNG\n");
203 else if (P.BitmapFormat == BF_BMP) {
204 sprintf(buf,
"%s.%05i.bmp", OutF, j);
206 if (!(dat = fopen(buf,
"wb"))) {
207 char* errMsg = strerror(errno);
208 if (errMsg ==
nullptr) {
209 ERR_CRITICAL(
"strerror failed.\n");
211 ERR_CRITICAL_FMT(
"Unable to open bitmap file %s (%d): %s\n", buf, errno, errMsg);
214 fwrite_big((
void*)bmf,
sizeof(
unsigned char),
sizeof(
BitmapHeader) /
sizeof(
unsigned char) + bmh->imagesize, dat);
219 fprintf(stderr,
"Unknown Bitmap format: %d\n", (
int)P.BitmapFormat);
224 int i, j, k, k2, value;
226 fprintf(stderr,
"Initialising bitmap\n");
227 k = P.bwidth * P.bheight2;
230 if (!(bmf = (
unsigned char*)calloc((
size_t)k + k2,
sizeof(
unsigned char))))
231 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
232 bmPixels = &(bmf[k2]);
237 bmh->headersize = 40;
238 bmh->width = P.bwidth;
239 bmh->height = P.bheight2;
240 bmh->PlanesAndBitspp = 1
243 bmh->imagesize = bmh->width * bmh->height;
247 bmh->hres = bmh->vres = (
int)(bmh->width * 10);
248 bmh->colours = BWCOLS * 4;
250 for (i = 0; i < BWCOLS * 4; i++)
251 bmh->palette[i][3] = 0;
252 for (j = 0; j < BWCOLS; j++)
254 value = 255 - 255 * j / (BWCOLS - 1);
256 bmh->palette[j][0] = bmh->palette[j][1] = bmh->palette[j][2] = (
unsigned char)value;
258 bmh->palette[BWCOLS + j][0] = 0;
259 bmh->palette[BWCOLS + j][1] = 0;
260 bmh->palette[BWCOLS + j][2] = (
unsigned char)value;
262 bmh->palette[2 * BWCOLS + j][0] = (
unsigned char)value;
263 bmh->palette[2 * BWCOLS + j][1] = 0;
264 bmh->palette[2 * BWCOLS + j][2] = 0;
266 bmh->palette[3 * BWCOLS + j][0] = 0;
267 bmh->palette[3 * BWCOLS + j][1] = (
unsigned char)value;
268 bmh->palette[3 * BWCOLS + j][2] = 0;
270 if (!(bmPopulation = (int32_t*)malloc(bmh->imagesize *
sizeof(int32_t))))
271 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
272 if (!(bmInfected = (int32_t*)malloc(bmh->imagesize *
sizeof(int32_t))))
273 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
274 if (!(bmRecovered = (int32_t*)malloc(bmh->imagesize *
sizeof(int32_t))))
275 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
276 if (!(bmTreated = (int32_t*)malloc(bmh->imagesize *
sizeof(int32_t))))
277 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
279 if (P.BitmapFormat == BF_PNG)
282 bmpdib = CreateDIBSection(GetDC(NULL), (BITMAPINFO*)bmp, DIB_RGB_COLORS, (
void**)&bmPixels, NULL, NULL);
283 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
284 Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
289 Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
290 Gdiplus::GetImageEncodersSize(&num, &size);
291 if (!(pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size))))
292 ERR_CRITICAL(
"Unable to allocate storage for bitmap\n");
293 Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
294 for (UINT j = 0; j < num; ++j) {
297 #pragma warning( suppress: 6385 ) 298 const WCHAR* type = pImageCodecInfo[j].MimeType;
299 if (wcscmp(type, L
"image/png") == 0) {
300 encoderClsid = pImageCodecInfo[j].Clsid;
304 free(pImageCodecInfo);
309 sprintf(buf,
"%s.ge", OutFileBase);
311 if (!(CreateDirectory(buf, NULL))) fprintf(stderr,
"Unable to create directory %s\n", buf);
313 if (!(mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))) fprintf(stderr,
"Unable to create directory %s\n", buf);
317 void Bitmap_Finalise()
319 if (P.BitmapFormat == BF_PNG)
322 Gdiplus::GdiplusShutdown(m_gdiplusToken);
DomainSize in_microcells_
Size of spatial domain in microcells.
double height_
The height.