00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "fileio_func.h"
00015 #include "viewport_func.h"
00016 #include "gfx_func.h"
00017 #include "screenshot.h"
00018 #include "blitter/factory.hpp"
00019 #include "zoom_func.h"
00020 #include "core/endian_func.hpp"
00021 #include "map_func.h"
00022 #include "saveload/saveload.h"
00023 #include "company_func.h"
00024 #include "strings_func.h"
00025 #include "gui.h"
00026
00027 #include "table/strings.h"
00028
00029
00030 char _screenshot_format_name[8];
00031 uint _num_screenshot_formats;
00032 uint _cur_screenshot_format;
00033 char _screenshot_name[128];
00034 char _full_screenshot_name[MAX_PATH];
00035
00036
00037 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
00038 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
00039
00040 struct ScreenshotFormat {
00041 const char *name;
00042 const char *extension;
00043 ScreenshotHandlerProc *proc;
00044 };
00045
00046
00047
00048
00049 #if defined(_MSC_VER) || defined(__WATCOMC__)
00050 #pragma pack(push, 1)
00051 #endif
00052
00054 struct BitmapFileHeader {
00055 uint16 type;
00056 uint32 size;
00057 uint32 reserved;
00058 uint32 off_bits;
00059 } GCC_PACK;
00060 assert_compile(sizeof(BitmapFileHeader) == 14);
00061
00062 #if defined(_MSC_VER) || defined(__WATCOMC__)
00063 #pragma pack(pop)
00064 #endif
00065
00067 struct BitmapInfoHeader {
00068 uint32 size;
00069 int32 width, height;
00070 uint16 planes, bitcount;
00071 uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
00072 };
00073 assert_compile(sizeof(BitmapInfoHeader) == 40);
00074
00076 struct RgbQuad {
00077 byte blue, green, red, reserved;
00078 };
00079 assert_compile(sizeof(RgbQuad) == 4);
00080
00082 struct RgbTriplet {
00083 byte b, g, r;
00084 };
00085 assert_compile(sizeof(RgbTriplet) == 3);
00086
00098 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00099 {
00100 uint bpp;
00101 switch (pixelformat) {
00102 case 8: bpp = 1; break;
00103
00104 case 32: bpp = 3; break;
00105
00106 default: return false;
00107 }
00108
00109 FILE *f = fopen(name, "wb");
00110 if (f == NULL) return false;
00111
00112
00113 uint bytewidth = Align(w * bpp, 4);
00114
00115
00116 uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
00117
00118
00119 BitmapFileHeader bfh;
00120 bfh.type = TO_LE16('MB');
00121 bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
00122 bfh.reserved = 0;
00123 bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
00124
00125
00126 BitmapInfoHeader bih;
00127 bih.size = TO_LE32(sizeof(BitmapInfoHeader));
00128 bih.width = TO_LE32(w);
00129 bih.height = TO_LE32(h);
00130 bih.planes = TO_LE16(1);
00131 bih.bitcount = TO_LE16(bpp * 8);
00132 bih.compression = 0;
00133 bih.sizeimage = 0;
00134 bih.xpels = 0;
00135 bih.ypels = 0;
00136 bih.clrused = 0;
00137 bih.clrimp = 0;
00138
00139
00140 if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
00141 fclose(f);
00142 return false;
00143 }
00144
00145 if (pixelformat == 8) {
00146
00147 RgbQuad rq[256];
00148 for (uint i = 0; i < 256; i++) {
00149 rq[i].red = palette[i].r;
00150 rq[i].green = palette[i].g;
00151 rq[i].blue = palette[i].b;
00152 rq[i].reserved = 0;
00153 }
00154
00155 if (fwrite(rq, sizeof(rq), 1, f) != 1) {
00156 fclose(f);
00157 return false;
00158 }
00159 }
00160
00161
00162 uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128);
00163
00164 uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8);
00165 uint8 *line = AllocaM(uint8, bytewidth);
00166 memset(line, 0, bytewidth);
00167
00168
00169 do {
00170 uint n = min(h, maxlines);
00171 h -= n;
00172
00173
00174 callb(userdata, buff, h, w, n);
00175
00176
00177 while (n-- != 0) {
00178 if (pixelformat == 8) {
00179
00180 memcpy(line, buff + n * w, w);
00181 } else {
00182
00183
00184 Colour *src = ((Colour *)buff) + n * w;
00185 RgbTriplet *dst = (RgbTriplet *)line;
00186 for (uint i = 0; i < w; i++) {
00187 dst[i].r = src[i].r;
00188 dst[i].g = src[i].g;
00189 dst[i].b = src[i].b;
00190 }
00191 }
00192
00193 if (fwrite(line, bytewidth, 1, f) != 1) {
00194 free(buff);
00195 fclose(f);
00196 return false;
00197 }
00198 }
00199 } while (h != 0);
00200
00201 free(buff);
00202 fclose(f);
00203
00204 return true;
00205 }
00206
00207
00208
00209
00210 #if defined(WITH_PNG)
00211 #include <png.h>
00212
00213 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
00214 {
00215 DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00216 longjmp(png_ptr->jmpbuf, 1);
00217 }
00218
00219 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
00220 {
00221 DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00222 }
00223
00224 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00225 {
00226 png_color rq[256];
00227 FILE *f;
00228 uint i, y, n;
00229 uint maxlines;
00230 uint bpp = pixelformat / 8;
00231 png_structp png_ptr;
00232 png_infop info_ptr;
00233
00234
00235 if (pixelformat != 8 && pixelformat != 32) return false;
00236
00237 f = fopen(name, "wb");
00238 if (f == NULL) return false;
00239
00240 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)name, png_my_error, png_my_warning);
00241
00242 if (png_ptr == NULL) {
00243 fclose(f);
00244 return false;
00245 }
00246
00247 info_ptr = png_create_info_struct(png_ptr);
00248 if (info_ptr == NULL) {
00249 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
00250 fclose(f);
00251 return false;
00252 }
00253
00254 if (setjmp(png_jmpbuf(png_ptr))) {
00255 png_destroy_write_struct(&png_ptr, &info_ptr);
00256 fclose(f);
00257 return false;
00258 }
00259
00260 png_init_io(png_ptr, f);
00261
00262 png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
00263
00264 png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
00265 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
00266
00267 if (pixelformat == 8) {
00268
00269 for (i = 0; i != 256; i++) {
00270 rq[i].red = palette[i].r;
00271 rq[i].green = palette[i].g;
00272 rq[i].blue = palette[i].b;
00273 }
00274
00275 png_set_PLTE(png_ptr, info_ptr, rq, 256);
00276 }
00277
00278 png_write_info(png_ptr, info_ptr);
00279 png_set_flush(png_ptr, 512);
00280
00281 if (pixelformat == 32) {
00282 png_color_8 sig_bit;
00283
00284
00285 sig_bit.alpha = 0;
00286 sig_bit.blue = 8;
00287 sig_bit.green = 8;
00288 sig_bit.red = 8;
00289 sig_bit.gray = 8;
00290 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
00291
00292 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
00293 png_set_bgr(png_ptr);
00294 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
00295 #else
00296 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
00297 #endif
00298 }
00299
00300
00301 maxlines = Clamp(65536 / w, 16, 128);
00302
00303
00304 void *buff = CallocT<uint8>(w * maxlines * bpp);
00305
00306 y = 0;
00307 do {
00308
00309 n = min(h - y, maxlines);
00310
00311
00312 callb(userdata, buff, y, w, n);
00313 y += n;
00314
00315
00316 for (i = 0; i != n; i++)
00317 png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
00318 } while (y != h);
00319
00320 png_write_end(png_ptr, info_ptr);
00321 png_destroy_write_struct(&png_ptr, &info_ptr);
00322
00323 free(buff);
00324 fclose(f);
00325 return true;
00326 }
00327 #endif
00328
00329
00330
00331
00332
00333
00334 struct PcxHeader {
00335 byte manufacturer;
00336 byte version;
00337 byte rle;
00338 byte bpp;
00339 uint32 unused;
00340 uint16 xmax, ymax;
00341 uint16 hdpi, vdpi;
00342 byte pal_small[16 * 3];
00343 byte reserved;
00344 byte planes;
00345 uint16 pitch;
00346 uint16 cpal;
00347 uint16 width;
00348 uint16 height;
00349 byte filler[54];
00350 };
00351 assert_compile(sizeof(PcxHeader) == 128);
00352
00353 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00354 {
00355 FILE *f;
00356 uint maxlines;
00357 uint y;
00358 PcxHeader pcx;
00359 bool success;
00360
00361 if (pixelformat == 32) {
00362 DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick an other format.");
00363 return false;
00364 }
00365 if (pixelformat != 8 || w == 0)
00366 return false;
00367
00368 f = fopen(name, "wb");
00369 if (f == NULL) return false;
00370
00371 memset(&pcx, 0, sizeof(pcx));
00372
00373
00374 pcx.manufacturer = 10;
00375 pcx.version = 5;
00376 pcx.rle = 1;
00377 pcx.bpp = 8;
00378 pcx.xmax = TO_LE16(w - 1);
00379 pcx.ymax = TO_LE16(h - 1);
00380 pcx.hdpi = TO_LE16(320);
00381 pcx.vdpi = TO_LE16(320);
00382
00383 pcx.planes = 1;
00384 pcx.cpal = TO_LE16(1);
00385 pcx.width = pcx.pitch = TO_LE16(w);
00386 pcx.height = TO_LE16(h);
00387
00388
00389 if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
00390 fclose(f);
00391 return false;
00392 }
00393
00394
00395 maxlines = Clamp(65536 / w, 16, 128);
00396
00397
00398 uint8 *buff = CallocT<uint8>(w * maxlines);
00399
00400 y = 0;
00401 do {
00402
00403 uint n = min(h - y, maxlines);
00404 uint i;
00405
00406
00407 callb(userdata, buff, y, w, n);
00408 y += n;
00409
00410
00411 for (i = 0; i != n; i++) {
00412 const uint8 *bufp = buff + i * w;
00413 byte runchar = bufp[0];
00414 uint runcount = 1;
00415 uint j;
00416
00417
00418 for (j = 1; j < w; j++) {
00419 uint8 ch = bufp[j];
00420
00421 if (ch != runchar || runcount >= 0x3f) {
00422 if (runcount > 1 || (runchar & 0xC0) == 0xC0)
00423 if (fputc(0xC0 | runcount, f) == EOF) {
00424 free(buff);
00425 fclose(f);
00426 return false;
00427 }
00428 if (fputc(runchar, f) == EOF) {
00429 free(buff);
00430 fclose(f);
00431 return false;
00432 }
00433 runcount = 0;
00434 runchar = ch;
00435 }
00436 runcount++;
00437 }
00438
00439
00440 if (runcount > 1 || (runchar & 0xC0) == 0xC0)
00441 if (fputc(0xC0 | runcount, f) == EOF) {
00442 free(buff);
00443 fclose(f);
00444 return false;
00445 }
00446 if (fputc(runchar, f) == EOF) {
00447 free(buff);
00448 fclose(f);
00449 return false;
00450 }
00451 }
00452 } while (y != h);
00453
00454 free(buff);
00455
00456
00457 if (fputc(12, f) == EOF) {
00458 fclose(f);
00459 return false;
00460 }
00461
00462
00463 byte tmp[256 * 3];
00464
00465 for (uint i = 0; i < 256; i++) {
00466 tmp[i * 3 + 0] = palette[i].r;
00467 tmp[i * 3 + 1] = palette[i].g;
00468 tmp[i * 3 + 2] = palette[i].b;
00469 }
00470 success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
00471
00472 fclose(f);
00473
00474 return success;
00475 }
00476
00477
00478
00479
00480
00481 static const ScreenshotFormat _screenshot_formats[] = {
00482 #if defined(WITH_PNG)
00483 {"PNG", "png", &MakePNGImage},
00484 #endif
00485 {"BMP", "bmp", &MakeBMPImage},
00486 {"PCX", "pcx", &MakePCXImage},
00487 };
00488
00489 void InitializeScreenshotFormats()
00490 {
00491 int i, j;
00492 for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++)
00493 if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
00494 j = i;
00495 break;
00496 }
00497 _cur_screenshot_format = j;
00498 _num_screenshot_formats = lengthof(_screenshot_formats);
00499 }
00500
00501 const char *GetScreenshotFormatDesc(int i)
00502 {
00503 return _screenshot_formats[i].name;
00504 }
00505
00506 void SetScreenshotFormat(int i)
00507 {
00508 _cur_screenshot_format = i;
00509 strecpy(_screenshot_format_name, _screenshot_formats[i].extension, lastof(_screenshot_format_name));
00510 }
00511
00512
00513 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00514 {
00515 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00516 void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
00517 blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
00518 }
00519
00527 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00528 {
00529 ViewPort *vp = (ViewPort *)userdata;
00530 DrawPixelInfo dpi, *old_dpi;
00531 int wx, left;
00532
00533
00534 DrawPixelInfo old_screen = _screen;
00535 bool old_disable_anim = _screen_disable_anim;
00536
00537 _screen.dst_ptr = buf;
00538 _screen.width = pitch;
00539 _screen.height = n;
00540 _screen.pitch = pitch;
00541 _screen_disable_anim = true;
00542
00543 old_dpi = _cur_dpi;
00544 _cur_dpi = &dpi;
00545
00546 dpi.dst_ptr = buf;
00547 dpi.height = n;
00548 dpi.width = vp->width;
00549 dpi.pitch = pitch;
00550 dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00551 dpi.left = 0;
00552 dpi.top = y;
00553
00554
00555 left = 0;
00556 while (vp->width - left != 0) {
00557 wx = min(vp->width - left, 1600);
00558 left += wx;
00559
00560 ViewportDoDraw(vp,
00561 ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
00562 ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
00563 ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
00564 ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
00565 );
00566 }
00567
00568 _cur_dpi = old_dpi;
00569
00570
00571 _screen = old_screen;
00572 _screen_disable_anim = old_disable_anim;
00573 }
00574
00575 static const char *MakeScreenshotName(const char *ext)
00576 {
00577 bool generate = StrEmpty(_screenshot_name);
00578
00579 if (generate) {
00580 if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
00581 strecpy(_screenshot_name, "screenshot", lastof(_screenshot_name));
00582 } else {
00583 GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name));
00584 }
00585 }
00586
00587
00588 size_t len = strlen(_screenshot_name);
00589 snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext);
00590
00591 for (uint serial = 1;; serial++) {
00592 if (snprintf(_full_screenshot_name, lengthof(_full_screenshot_name), "%s%s", _personal_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) {
00593
00594 _full_screenshot_name[0] = '\0';
00595 break;
00596 }
00597 if (!generate) break;
00598 if (!FileExists(_full_screenshot_name)) break;
00599
00600 snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext);
00601 }
00602
00603 return _full_screenshot_name;
00604 }
00605
00606 static bool MakeSmallScreenshot()
00607 {
00608 const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00609 return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
00610 }
00611
00612 static bool MakeWorldScreenshot()
00613 {
00614 ViewPort vp;
00615 const ScreenshotFormat *sf;
00616
00617 vp.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00618 vp.left = 0;
00619 vp.top = 0;
00620 vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS;
00621 vp.virtual_top = 0;
00622 vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS;
00623 vp.width = vp.virtual_width;
00624 vp.virtual_height = (MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1;
00625 vp.height = vp.virtual_height;
00626
00627 sf = _screenshot_formats + _cur_screenshot_format;
00628 return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
00629 }
00630
00637 bool MakeScreenshot(ScreenshotType t, const char *name)
00638 {
00639 _screenshot_name[0] = '\0';
00640 if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
00641
00642 bool ret;
00643 switch (t) {
00644 case SC_VIEWPORT:
00645 UndrawMouseCursor();
00646 DrawDirtyBlocks();
00647
00648 case SC_RAW:
00649 ret = MakeSmallScreenshot();
00650 break;
00651
00652 case SC_WORLD:
00653 ret = MakeWorldScreenshot();
00654 break;
00655
00656 default:
00657 NOT_REACHED();
00658 }
00659
00660 if (ret) {
00661 SetDParamStr(0, _screenshot_name);
00662 ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, 0, 0);
00663 } else {
00664 ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, 0, 0);
00665 }
00666
00667 return ret;
00668 }