screenshot.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "fileio_func.h"
00014 #include "viewport_func.h"
00015 #include "gfx_func.h"
00016 #include "screenshot.h"
00017 #include "blitter/factory.hpp"
00018 #include "zoom_func.h"
00019 #include "core/endian_func.hpp"
00020 #include "saveload/saveload.h"
00021 #include "company_func.h"
00022 #include "strings_func.h"
00023 #include "error.h"
00024 #include "window_gui.h"
00025 #include "window_func.h"
00026 #include "tile_map.h"
00027 
00028 #include "table/strings.h"
00029 
00030 static const char * const SCREENSHOT_NAME = "screenshot"; 
00031 static const char * const HEIGHTMAP_NAME  = "heightmap";  
00032 
00033 char _screenshot_format_name[8];      
00034 uint _num_screenshot_formats;         
00035 uint _cur_screenshot_format;          
00036 static char _screenshot_name[128];    
00037 char _full_screenshot_name[MAX_PATH]; 
00038 
00047 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
00048 
00060 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
00061 
00063 struct ScreenshotFormat {
00064   const char *name;            
00065   const char *extension;       
00066   ScreenshotHandlerProc *proc; 
00067 };
00068 
00069 /*************************************************
00070  **** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
00071  *************************************************/
00072 #if defined(_MSC_VER) || defined(__WATCOMC__)
00073 #pragma pack(push, 1)
00074 #endif
00075 
00077 struct BitmapFileHeader {
00078   uint16 type;
00079   uint32 size;
00080   uint32 reserved;
00081   uint32 off_bits;
00082 } GCC_PACK;
00083 assert_compile(sizeof(BitmapFileHeader) == 14);
00084 
00085 #if defined(_MSC_VER) || defined(__WATCOMC__)
00086 #pragma pack(pop)
00087 #endif
00088 
00090 struct BitmapInfoHeader {
00091   uint32 size;
00092   int32 width, height;
00093   uint16 planes, bitcount;
00094   uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
00095 };
00096 assert_compile(sizeof(BitmapInfoHeader) == 40);
00097 
00099 struct RgbQuad {
00100   byte blue, green, red, reserved;
00101 };
00102 assert_compile(sizeof(RgbQuad) == 4);
00103 
00116 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00117 {
00118   uint bpp; // bytes per pixel
00119   switch (pixelformat) {
00120     case 8:  bpp = 1; break;
00121     /* 32bpp mode is saved as 24bpp BMP */
00122     case 32: bpp = 3; break;
00123     /* Only implemented for 8bit and 32bit images so far */
00124     default: return false;
00125   }
00126 
00127   FILE *f = fopen(name, "wb");
00128   if (f == NULL) return false;
00129 
00130   /* Each scanline must be aligned on a 32bit boundary */
00131   uint bytewidth = Align(w * bpp, 4); // bytes per line in file
00132 
00133   /* Size of palette. Only present for 8bpp mode */
00134   uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
00135 
00136   /* Setup the file header */
00137   BitmapFileHeader bfh;
00138   bfh.type = TO_LE16('MB');
00139   bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
00140   bfh.reserved = 0;
00141   bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
00142 
00143   /* Setup the info header */
00144   BitmapInfoHeader bih;
00145   bih.size = TO_LE32(sizeof(BitmapInfoHeader));
00146   bih.width = TO_LE32(w);
00147   bih.height = TO_LE32(h);
00148   bih.planes = TO_LE16(1);
00149   bih.bitcount = TO_LE16(bpp * 8);
00150   bih.compression = 0;
00151   bih.sizeimage = 0;
00152   bih.xpels = 0;
00153   bih.ypels = 0;
00154   bih.clrused = 0;
00155   bih.clrimp = 0;
00156 
00157   /* Write file header and info header */
00158   if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
00159     fclose(f);
00160     return false;
00161   }
00162 
00163   if (pixelformat == 8) {
00164     /* Convert the palette to the windows format */
00165     RgbQuad rq[256];
00166     for (uint i = 0; i < 256; i++) {
00167       rq[i].red   = palette[i].r;
00168       rq[i].green = palette[i].g;
00169       rq[i].blue  = palette[i].b;
00170       rq[i].reserved = 0;
00171     }
00172     /* Write the palette */
00173     if (fwrite(rq, sizeof(rq), 1, f) != 1) {
00174       fclose(f);
00175       return false;
00176     }
00177   }
00178 
00179   /* Try to use 64k of memory, store between 16 and 128 lines */
00180   uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
00181 
00182   uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
00183   uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
00184   memset(line, 0, bytewidth);
00185 
00186   /* Start at the bottom, since bitmaps are stored bottom up */
00187   do {
00188     uint n = min(h, maxlines);
00189     h -= n;
00190 
00191     /* Render the pixels */
00192     callb(userdata, buff, h, w, n);
00193 
00194     /* Write each line */
00195     while (n-- != 0) {
00196       if (pixelformat == 8) {
00197         /* Move to 'line', leave last few pixels in line zeroed */
00198         memcpy(line, buff + n * w, w);
00199       } else {
00200         /* Convert from 'native' 32bpp to BMP-like 24bpp.
00201          * Works for both big and little endian machines */
00202         Colour *src = ((Colour *)buff) + n * w;
00203         byte *dst = line;
00204         for (uint i = 0; i < w; i++) {
00205           dst[i * 3    ] = src[i].b;
00206           dst[i * 3 + 1] = src[i].g;
00207           dst[i * 3 + 2] = src[i].r;
00208         }
00209       }
00210       /* Write to file */
00211       if (fwrite(line, bytewidth, 1, f) != 1) {
00212         free(buff);
00213         fclose(f);
00214         return false;
00215       }
00216     }
00217   } while (h != 0);
00218 
00219   free(buff);
00220   fclose(f);
00221 
00222   return true;
00223 }
00224 
00225 /*********************************************************
00226  **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
00227  *********************************************************/
00228 #if defined(WITH_PNG)
00229 #include <png.h>
00230 
00231 #ifdef PNG_TEXT_SUPPORTED
00232 #include "rev.h"
00233 #include "newgrf_config.h"
00234 #include "ai/ai_info.hpp"
00235 #include "company_base.h"
00236 #include "base_media_base.h"
00237 #endif /* PNG_TEXT_SUPPORTED */
00238 
00239 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
00240 {
00241   DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00242   longjmp(png_jmpbuf(png_ptr), 1);
00243 }
00244 
00245 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
00246 {
00247   DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00248 }
00249 
00262 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00263 {
00264   png_color rq[256];
00265   FILE *f;
00266   uint i, y, n;
00267   uint maxlines;
00268   uint bpp = pixelformat / 8;
00269   png_structp png_ptr;
00270   png_infop info_ptr;
00271 
00272   /* only implemented for 8bit and 32bit images so far. */
00273   if (pixelformat != 8 && pixelformat != 32) return false;
00274 
00275   f = fopen(name, "wb");
00276   if (f == NULL) return false;
00277 
00278   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, const_cast<char *>(name), png_my_error, png_my_warning);
00279 
00280   if (png_ptr == NULL) {
00281     fclose(f);
00282     return false;
00283   }
00284 
00285   info_ptr = png_create_info_struct(png_ptr);
00286   if (info_ptr == NULL) {
00287     png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
00288     fclose(f);
00289     return false;
00290   }
00291 
00292   if (setjmp(png_jmpbuf(png_ptr))) {
00293     png_destroy_write_struct(&png_ptr, &info_ptr);
00294     fclose(f);
00295     return false;
00296   }
00297 
00298   png_init_io(png_ptr, f);
00299 
00300   png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
00301 
00302   png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
00303     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
00304 
00305 #ifdef PNG_TEXT_SUPPORTED
00306   /* Try to add some game metadata to the PNG screenshot so
00307    * it's more useful for debugging and archival purposes. */
00308   png_text_struct text[2];
00309   memset(text, 0, sizeof(text));
00310   text[0].key = const_cast<char *>("Software");
00311   text[0].text = const_cast<char *>(_openttd_revision);
00312   text[0].text_length = strlen(_openttd_revision);
00313   text[0].compression = PNG_TEXT_COMPRESSION_NONE;
00314 
00315   char buf[8192];
00316   char *p = buf;
00317   p += seprintf(p, lastof(buf), "Graphics set: %s (%u)\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
00318   p = strecpy(p, "NewGRFs:\n", lastof(buf));
00319   for (const GRFConfig *c = _game_mode == GM_MENU ? NULL : _grfconfig; c != NULL; c = c->next) {
00320     p += seprintf(p, lastof(buf), "%08X ", BSWAP32(c->ident.grfid));
00321     p = md5sumToString(p, lastof(buf), c->ident.md5sum);
00322     p += seprintf(p, lastof(buf), " %s\n", c->filename);
00323   }
00324   p = strecpy(p, "\nCompanies:\n", lastof(buf));
00325   const Company *c;
00326   FOR_ALL_COMPANIES(c) {
00327     if (c->ai_info == NULL) {
00328       p += seprintf(p, lastof(buf), "%2i: Human\n", (int)c->index);
00329     } else {
00330       p += seprintf(p, lastof(buf), "%2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
00331     }
00332   }
00333   text[1].key = const_cast<char *>("Description");
00334   text[1].text = buf;
00335   text[1].text_length = p - buf;
00336   text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
00337   png_set_text(png_ptr, info_ptr, text, 2);
00338 #endif /* PNG_TEXT_SUPPORTED */
00339 
00340   if (pixelformat == 8) {
00341     /* convert the palette to the .PNG format. */
00342     for (i = 0; i != 256; i++) {
00343       rq[i].red   = palette[i].r;
00344       rq[i].green = palette[i].g;
00345       rq[i].blue  = palette[i].b;
00346     }
00347 
00348     png_set_PLTE(png_ptr, info_ptr, rq, 256);
00349   }
00350 
00351   png_write_info(png_ptr, info_ptr);
00352   png_set_flush(png_ptr, 512);
00353 
00354   if (pixelformat == 32) {
00355     png_color_8 sig_bit;
00356 
00357     /* Save exact colour/alpha resolution */
00358     sig_bit.alpha = 0;
00359     sig_bit.blue  = 8;
00360     sig_bit.green = 8;
00361     sig_bit.red   = 8;
00362     sig_bit.gray  = 8;
00363     png_set_sBIT(png_ptr, info_ptr, &sig_bit);
00364 
00365 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
00366     png_set_bgr(png_ptr);
00367     png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
00368 #else
00369     png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
00370 #endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
00371   }
00372 
00373   /* use by default 64k temp memory */
00374   maxlines = Clamp(65536 / w, 16, 128);
00375 
00376   /* now generate the bitmap bits */
00377   void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
00378 
00379   y = 0;
00380   do {
00381     /* determine # lines to write */
00382     n = min(h - y, maxlines);
00383 
00384     /* render the pixels into the buffer */
00385     callb(userdata, buff, y, w, n);
00386     y += n;
00387 
00388     /* write them to png */
00389     for (i = 0; i != n; i++) {
00390       png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
00391     }
00392   } while (y != h);
00393 
00394   png_write_end(png_ptr, info_ptr);
00395   png_destroy_write_struct(&png_ptr, &info_ptr);
00396 
00397   free(buff);
00398   fclose(f);
00399   return true;
00400 }
00401 #endif /* WITH_PNG */
00402 
00403 
00404 /*************************************************
00405  **** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
00406  *************************************************/
00407 
00409 struct PcxHeader {
00410   byte manufacturer;
00411   byte version;
00412   byte rle;
00413   byte bpp;
00414   uint32 unused;
00415   uint16 xmax, ymax;
00416   uint16 hdpi, vdpi;
00417   byte pal_small[16 * 3];
00418   byte reserved;
00419   byte planes;
00420   uint16 pitch;
00421   uint16 cpal;
00422   uint16 width;
00423   uint16 height;
00424   byte filler[54];
00425 };
00426 assert_compile(sizeof(PcxHeader) == 128);
00427 
00440 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00441 {
00442   FILE *f;
00443   uint maxlines;
00444   uint y;
00445   PcxHeader pcx;
00446   bool success;
00447 
00448   if (pixelformat == 32) {
00449     DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
00450     return false;
00451   }
00452   if (pixelformat != 8 || w == 0) return false;
00453 
00454   f = fopen(name, "wb");
00455   if (f == NULL) return false;
00456 
00457   memset(&pcx, 0, sizeof(pcx));
00458 
00459   /* setup pcx header */
00460   pcx.manufacturer = 10;
00461   pcx.version = 5;
00462   pcx.rle = 1;
00463   pcx.bpp = 8;
00464   pcx.xmax = TO_LE16(w - 1);
00465   pcx.ymax = TO_LE16(h - 1);
00466   pcx.hdpi = TO_LE16(320);
00467   pcx.vdpi = TO_LE16(320);
00468 
00469   pcx.planes = 1;
00470   pcx.cpal = TO_LE16(1);
00471   pcx.width = pcx.pitch = TO_LE16(w);
00472   pcx.height = TO_LE16(h);
00473 
00474   /* write pcx header */
00475   if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
00476     fclose(f);
00477     return false;
00478   }
00479 
00480   /* use by default 64k temp memory */
00481   maxlines = Clamp(65536 / w, 16, 128);
00482 
00483   /* now generate the bitmap bits */
00484   uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
00485 
00486   y = 0;
00487   do {
00488     /* determine # lines to write */
00489     uint n = min(h - y, maxlines);
00490     uint i;
00491 
00492     /* render the pixels into the buffer */
00493     callb(userdata, buff, y, w, n);
00494     y += n;
00495 
00496     /* write them to pcx */
00497     for (i = 0; i != n; i++) {
00498       const uint8 *bufp = buff + i * w;
00499       byte runchar = bufp[0];
00500       uint runcount = 1;
00501       uint j;
00502 
00503       /* for each pixel... */
00504       for (j = 1; j < w; j++) {
00505         uint8 ch = bufp[j];
00506 
00507         if (ch != runchar || runcount >= 0x3f) {
00508           if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
00509             if (fputc(0xC0 | runcount, f) == EOF) {
00510               free(buff);
00511               fclose(f);
00512               return false;
00513             }
00514           }
00515           if (fputc(runchar, f) == EOF) {
00516             free(buff);
00517             fclose(f);
00518             return false;
00519           }
00520           runcount = 0;
00521           runchar = ch;
00522         }
00523         runcount++;
00524       }
00525 
00526       /* write remaining bytes.. */
00527       if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
00528         if (fputc(0xC0 | runcount, f) == EOF) {
00529           free(buff);
00530           fclose(f);
00531           return false;
00532         }
00533       }
00534       if (fputc(runchar, f) == EOF) {
00535         free(buff);
00536         fclose(f);
00537         return false;
00538       }
00539     }
00540   } while (y != h);
00541 
00542   free(buff);
00543 
00544   /* write 8-bit colour palette */
00545   if (fputc(12, f) == EOF) {
00546     fclose(f);
00547     return false;
00548   }
00549 
00550   /* Palette is word-aligned, copy it to a temporary byte array */
00551   byte tmp[256 * 3];
00552 
00553   for (uint i = 0; i < 256; i++) {
00554     tmp[i * 3 + 0] = palette[i].r;
00555     tmp[i * 3 + 1] = palette[i].g;
00556     tmp[i * 3 + 2] = palette[i].b;
00557   }
00558   success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
00559 
00560   fclose(f);
00561 
00562   return success;
00563 }
00564 
00565 /*************************************************
00566  **** GENERIC SCREENSHOT CODE
00567  *************************************************/
00568 
00570 static const ScreenshotFormat _screenshot_formats[] = {
00571 #if defined(WITH_PNG)
00572   {"PNG", "png", &MakePNGImage},
00573 #endif
00574   {"BMP", "bmp", &MakeBMPImage},
00575   {"PCX", "pcx", &MakePCXImage},
00576 };
00577 
00579 const char *GetCurrentScreenshotExtension()
00580 {
00581   return _screenshot_formats[_cur_screenshot_format].extension;
00582 }
00583 
00585 void InitializeScreenshotFormats()
00586 {
00587   uint j = 0;
00588   for (uint i = 0; i < lengthof(_screenshot_formats); i++) {
00589     if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
00590       j = i;
00591       break;
00592     }
00593   }
00594   _cur_screenshot_format = j;
00595   _num_screenshot_formats = lengthof(_screenshot_formats);
00596 }
00597 
00603 const char *GetScreenshotFormatDesc(int i)
00604 {
00605   return _screenshot_formats[i].name;
00606 }
00607 
00612 void SetScreenshotFormat(uint i)
00613 {
00614   assert(i < _num_screenshot_formats);
00615   _cur_screenshot_format = i;
00616   strecpy(_screenshot_format_name, _screenshot_formats[i].extension, lastof(_screenshot_format_name));
00617 }
00618 
00623 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00624 {
00625   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00626   void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
00627   blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
00628 }
00629 
00638 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00639 {
00640   ViewPort *vp = (ViewPort *)userdata;
00641   DrawPixelInfo dpi, *old_dpi;
00642   int wx, left;
00643 
00644   /* We are no longer rendering to the screen */
00645   DrawPixelInfo old_screen = _screen;
00646   bool old_disable_anim = _screen_disable_anim;
00647 
00648   _screen.dst_ptr = buf;
00649   _screen.width = pitch;
00650   _screen.height = n;
00651   _screen.pitch = pitch;
00652   _screen_disable_anim = true;
00653 
00654   old_dpi = _cur_dpi;
00655   _cur_dpi = &dpi;
00656 
00657   dpi.dst_ptr = buf;
00658   dpi.height = n;
00659   dpi.width = vp->width;
00660   dpi.pitch = pitch;
00661   dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00662   dpi.left = 0;
00663   dpi.top = y;
00664 
00665   /* Render viewport in blocks of 1600 pixels width */
00666   left = 0;
00667   while (vp->width - left != 0) {
00668     wx = min(vp->width - left, 1600);
00669     left += wx;
00670 
00671     ViewportDoDraw(vp,
00672       ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
00673       ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
00674       ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
00675       ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
00676     );
00677   }
00678 
00679   _cur_dpi = old_dpi;
00680 
00681   /* Switch back to rendering to the screen */
00682   _screen = old_screen;
00683   _screen_disable_anim = old_disable_anim;
00684 }
00685 
00692 static const char *MakeScreenshotName(const char *default_fn, const char *ext)
00693 {
00694   bool generate = StrEmpty(_screenshot_name);
00695 
00696   if (generate) {
00697     if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
00698       strecpy(_screenshot_name, default_fn, lastof(_screenshot_name));
00699     } else {
00700       GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name));
00701     }
00702   }
00703 
00704   /* Add extension to screenshot file */
00705   size_t len = strlen(_screenshot_name);
00706   snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext);
00707 
00708   for (uint serial = 1;; serial++) {
00709     if (snprintf(_full_screenshot_name, lengthof(_full_screenshot_name), "%s%s", _personal_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) {
00710       /* We need more characters than MAX_PATH -> end with error */
00711       _full_screenshot_name[0] = '\0';
00712       break;
00713     }
00714     if (!generate) break; // allow overwriting of non-automatic filenames
00715     if (!FileExists(_full_screenshot_name)) break;
00716     /* If file exists try another one with same name, but just with a higher index */
00717     snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext);
00718   }
00719 
00720   return _full_screenshot_name;
00721 }
00722 
00724 static bool MakeSmallScreenshot()
00725 {
00726   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00727   return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height,
00728       BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
00729 }
00730 
00732 static bool MakeZoomedInScreenshot()
00733 {
00734   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00735   ViewPort vp;
00736 
00737   vp.zoom = _settings_client.gui.zoom_min;
00738   vp.left = w->viewport->left;
00739   vp.top = w->viewport->top;
00740   vp.virtual_left = w->viewport->virtual_left;
00741   vp.virtual_top = w->viewport->virtual_top;
00742   vp.virtual_width = w->viewport->virtual_width;
00743   vp.width = UnScaleByZoom(vp.virtual_width, vp.zoom);
00744   vp.virtual_height = w->viewport->virtual_height;
00745   vp.height = UnScaleByZoom(vp.virtual_height, vp.zoom);
00746   vp.overlay = NULL;
00747 
00748   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00749   return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), LargeWorldCallback, &vp, vp.width, vp.height,
00750       BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
00751 }
00752 
00754 static bool MakeWorldScreenshot()
00755 {
00756   ViewPort vp;
00757   const ScreenshotFormat *sf;
00758 
00759   /* We need to account for a hill or high building at tile 0,0. */
00760   int extra_height_top = TilePixelHeight(0) + 150;
00761   /* If there is a hill at the bottom don't create a large black area. */
00762   int reclaim_height_bottom = TilePixelHeight(MapSize() - 1);
00763 
00764   vp.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00765   vp.left = 0;
00766   vp.top = 0;
00767   vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS * ZOOM_LVL_BASE;
00768   vp.virtual_top = -extra_height_top * ZOOM_LVL_BASE;
00769   vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS;
00770   vp.width = vp.virtual_width;
00771   vp.virtual_height = ((MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1) + extra_height_top - reclaim_height_bottom;
00772   vp.height = vp.virtual_height;
00773   vp.overlay = NULL;
00774 
00775   sf = _screenshot_formats + _cur_screenshot_format;
00776   return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), LargeWorldCallback, &vp, vp.width, vp.height,
00777       BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
00778 }
00779 
00789 static void HeightmapCallback(void *userdata, void *buffer, uint y, uint pitch, uint n)
00790 {
00791   byte *buf = (byte *)buffer;
00792   while (n > 0) {
00793     TileIndex ti = TileXY(MapMaxX(), y);
00794     for (uint x = MapMaxX(); true; x--) {
00795       *buf = 16 * TileHeight(ti);
00796       buf++;
00797       if (x == 0) break;
00798       ti = TILE_ADDXY(ti, -1, 0);
00799     }
00800     y++;
00801     n--;
00802   }
00803 }
00804 
00809 bool MakeHeightmapScreenshot(const char *filename)
00810 {
00811   Colour palette[256];
00812   for (uint i = 0; i < lengthof(palette); i++) {
00813     palette[i].a = 0xff;
00814     palette[i].r = i;
00815     palette[i].g = i;
00816     palette[i].b = i;
00817   }
00818   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00819   return sf->proc(filename, HeightmapCallback, NULL, MapSizeX(), MapSizeY(), 8, palette);
00820 }
00821 
00828 bool MakeScreenshot(ScreenshotType t, const char *name)
00829 {
00830   if (t == SC_VIEWPORT) {
00831     /* First draw the dirty parts of the screen and only then change the name
00832      * of the screenshot. This way the screenshot will always show the name
00833      * of the previous screenshot in the 'successful' message instead of the
00834      * name of the new screenshot (or an empty name). */
00835     UndrawMouseCursor();
00836     DrawDirtyBlocks();
00837   }
00838 
00839   _screenshot_name[0] = '\0';
00840   if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
00841 
00842   bool ret;
00843   switch (t) {
00844     case SC_VIEWPORT:
00845     case SC_RAW:
00846       ret = MakeSmallScreenshot();
00847       break;
00848 
00849     case SC_ZOOMEDIN:
00850       ret = MakeZoomedInScreenshot();
00851       break;
00852 
00853     case SC_WORLD:
00854       ret = MakeWorldScreenshot();
00855       break;
00856 
00857     case SC_HEIGHTMAP: {
00858       const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00859       ret = MakeHeightmapScreenshot(MakeScreenshotName(HEIGHTMAP_NAME, sf->extension));
00860       break;
00861     }
00862 
00863     default:
00864       NOT_REACHED();
00865   }
00866 
00867   if (ret) {
00868     SetDParamStr(0, _screenshot_name);
00869     ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING);
00870   } else {
00871     ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR);
00872   }
00873 
00874   return ret;
00875 }