Annotation of hatari/src/screenSnapShot.c, revision 1.1.1.15

1.1       root        1: /*
1.1.1.5   root        2:   Hatari - screenSnapShot.c
1.1       root        3: 
1.1.1.14  root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1.1.5   root        6: 
                      7:   Screen Snapshots.
1.1       root        8: */
1.1.1.11  root        9: const char ScreenSnapShot_fileid[] = "Hatari screenSnapShot.c : " __DATE__ " " __TIME__;
1.1       root       10: 
1.1.1.2   root       11: #include <SDL.h>
                     12: #include <dirent.h>
                     13: #include <string.h>
1.1       root       14: #include "main.h"
1.1.1.13  root       15: #include "configuration.h"
1.1.1.6   root       16: #include "log.h"
1.1.1.8   root       17: #include "paths.h"
1.1       root       18: #include "screen.h"
                     19: #include "screenSnapShot.h"
1.1.1.13  root       20: #include "statusbar.h"
1.1       root       21: #include "video.h"
1.1.1.9   root       22: /* after above that bring in config.h */
                     23: #if HAVE_LIBPNG
                     24: # include <png.h>
                     25: # include <assert.h>
1.1.1.12  root       26: # include "pixel_convert.h"                            /* inline functions */
1.1.1.9   root       27: #endif
1.1       root       28: 
1.1.1.2   root       29: 
1.1.1.6   root       30: static int nScreenShots = 0;                /* Number of screen shots saved */
                     31: 
1.1       root       32: 
1.1.1.2   root       33: /*-----------------------------------------------------------------------*/
1.1.1.8   root       34: /**
                     35:  * Scan working directory to get the screenshot number
                     36:  */
1.1.1.5   root       37: static void ScreenSnapShot_GetNum(void)
                     38: {
1.1.1.8   root       39:        char dummy[5];
                     40:        int i, num;
                     41:        DIR *workingdir = opendir(Paths_GetWorkingDir());
                     42:        struct dirent *file;
                     43: 
                     44:        nScreenShots = 0;
                     45:        if (workingdir == NULL)  return;
                     46: 
                     47:        file = readdir(workingdir);
                     48:        while (file != NULL)
                     49:        {
                     50:                if ( strncmp("grab", file->d_name, 4) == 0 )
                     51:                {
                     52:                        /* copy next 4 numbers */
                     53:                        for (i = 0; i < 4; i++)
                     54:                        {
                     55:                                if (file->d_name[4+i] >= '0' && file->d_name[4+i] <= '9')
                     56:                                        dummy[i] = file->d_name[4+i];
                     57:                                else
                     58:                                        break;
                     59:                        }
                     60: 
                     61:                        dummy[i] = '\0'; /* null terminate */
                     62:                        num = atoi(dummy);
                     63:                        if (num > nScreenShots)  nScreenShots = num;
                     64:                }
                     65:                /* next file.. */
                     66:                file = readdir(workingdir);
                     67:        }
                     68: 
                     69:        closedir(workingdir);
1.1.1.2   root       70: }
                     71: 
1.1.1.8   root       72: 
1.1.1.9   root       73: #if HAVE_LIBPNG
1.1.1.8   root       74: /**
1.1.1.12  root       75:  * Save given SDL surface as PNG. Return png file size > 0 for success.
1.1.1.9   root       76:  */
1.1.1.12  root       77: static int ScreenSnapShot_SavePNG(SDL_Surface *surface, const char *filename)
1.1.1.9   root       78: {
1.1.1.12  root       79:        FILE *fp = NULL;
1.1.1.13  root       80:        int ret, bottom;
1.1.1.12  root       81:   
                     82:        fp = fopen(filename, "wb");
                     83:        if (!fp)
                     84:                return -1;
1.1.1.9   root       85: 
1.1.1.13  root       86:        if (ConfigureParams.Screen.bCrop)
                     87:                bottom = Statusbar_GetHeight();
                     88:        else
                     89:                bottom = 0;
                     90: 
                     91:        /* default compression/filter and configured cropping */
                     92:        ret = ScreenSnapShot_SavePNG_ToFile(surface, fp, -1, -1, 0, 0, 0, bottom);
1.1.1.9   root       93: 
1.1.1.12  root       94:        fclose (fp);
                     95:        return ret;                                     /* >0 if OK, -1 if error */
1.1.1.9   root       96: }
                     97: 
1.1.1.12  root       98: 
1.1.1.9   root       99: /**
1.1.1.12  root      100:  * Save given SDL surface as PNG in an already opened FILE, eventually cropping some borders.
                    101:  * Return png file size > 0 for success.
                    102:  * This function is also used by avi_record.c to save individual frames as png images.
1.1.1.9   root      103:  */
1.1.1.12  root      104: int ScreenSnapShot_SavePNG_ToFile(SDL_Surface *surface, FILE *fp, int png_compression_level, int png_filter ,
                    105:                int CropLeft , int CropRight , int CropTop , int CropBottom )
1.1.1.9   root      106: {
1.1.1.12  root      107:        bool do_lock;
1.1.1.9   root      108:        int y, ret = -1;
1.1.1.12  root      109:        int w = surface->w - CropLeft - CropRight;
                    110:        int h = surface->h - CropTop - CropBottom;
1.1.1.13  root      111:        Uint8 *src_ptr;
1.1.1.9   root      112:        Uint8 rowbuf[3*surface->w];
                    113:        SDL_PixelFormat *fmt = surface->format;
                    114:        png_infop info_ptr = NULL;
                    115:        png_structp png_ptr;
                    116:        png_text pngtext;
                    117:        char key[] = "Title";
                    118:        char text[] = "Hatari screenshot";
1.1.1.12  root      119:        long start;
1.1.1.9   root      120:        
                    121:        /* Create and initialize the png_struct with error handler functions. */
                    122:        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
                    123:        if (!png_ptr) 
                    124:        {
                    125:                return ret;
                    126:        }
                    127:        
                    128:        /* Allocate/initialize the image information data. */
                    129:        info_ptr = png_create_info_struct(png_ptr);
                    130:        if (!info_ptr)
                    131:                goto png_cleanup;
                    132: 
                    133:        /* libpng ugliness: Set error handling when not supplying own
                    134:         * error handling functions in the png_create_write_struct() call.
                    135:         */
                    136:        if (setjmp(png_jmpbuf(png_ptr)))
                    137:                goto png_cleanup;
                    138: 
1.1.1.12  root      139:        /* store current pos in fp (could be != 0 for avi recording) */
                    140:        start = ftell ( fp );
1.1.1.9   root      141: 
                    142:        /* initialize the png structure */
                    143:        png_init_io(png_ptr, fp);
                    144: 
                    145:        /* image data properties */
                    146:        png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_RGB,
                    147:                     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
                    148:                     PNG_FILTER_TYPE_DEFAULT);
1.1.1.12  root      149: 
                    150:        if ( png_compression_level >= 0 )
                    151:                png_set_compression_level ( png_ptr , png_compression_level );
                    152:        if ( png_filter >= 0 )
                    153:                png_set_filter ( png_ptr , 0 , png_filter );
                    154:                     
1.1.1.9   root      155:        /* image info */
                    156:        pngtext.key = key;
                    157:        pngtext.text = text;
                    158:        pngtext.compression = PNG_TEXT_COMPRESSION_NONE;
                    159: #ifdef PNG_iTXt_SUPPORTED
                    160:        pngtext.lang = NULL;
                    161: #endif
                    162:        png_set_text(png_ptr, info_ptr, &pngtext, 1);
                    163: 
                    164:        /* write the file header information */
                    165:        png_write_info(png_ptr, info_ptr);
                    166: 
1.1.1.12  root      167:        /* write surface data rows one at a time (after cropping if necessary) */
                    168:        src_ptr = (Uint8 *)surface->pixels + CropTop * surface->pitch + CropLeft * surface->format->BytesPerPixel;
                    169:        do_lock = SDL_MUSTLOCK(surface);
1.1.1.9   root      170:        for (y = 0; y < h; y++) {
1.1.1.12  root      171:                /* need to lock the surface while accessing it directly */
                    172:                if (do_lock)
                    173:                        SDL_LockSurface(surface);
1.1.1.9   root      174:                switch (fmt->BytesPerPixel) {
                    175:                case 1:
                    176:                        /* unpack 8-bit data with RGB palette */
1.1.1.13  root      177:                        PixelConvert_8to24Bits(rowbuf, src_ptr, w, fmt->palette->colors);
1.1.1.9   root      178:                        break;
                    179:                case 2:
                    180:                        /* unpack 16-bit RGB pixels */
1.1.1.13  root      181:                        PixelConvert_16to24Bits(rowbuf, (Uint16*)src_ptr, w, fmt);
1.1.1.9   root      182:                        break;
                    183:                case 3:
                    184:                        /* PNG can handle 24-bits */
                    185:                        break;
                    186:                case 4:
                    187:                        /* unpack 32-bit RGBA pixels */
1.1.1.13  root      188:                        PixelConvert_32to24Bits(rowbuf, (Uint32*)src_ptr, w, fmt);
1.1.1.9   root      189:                        break;
                    190:                }
1.1.1.12  root      191:                /* and unlock surface before syscalls */
                    192:                if (do_lock)
                    193:                        SDL_UnlockSurface(surface);
1.1.1.9   root      194:                src_ptr += surface->pitch;
                    195:                png_write_row(png_ptr, rowbuf);
                    196:        }
1.1.1.12  root      197: 
1.1.1.9   root      198:        /* write the additional chuncks to the PNG file */
                    199:        png_write_end(png_ptr, info_ptr);
                    200: 
1.1.1.12  root      201:        ret = ftell ( fp ) - start;                             /* size of the png image */
1.1.1.9   root      202: png_cleanup:
                    203:        if (png_ptr)
                    204:                png_destroy_write_struct(&png_ptr, NULL);
                    205:        return ret;
                    206: }
                    207: #endif
                    208: 
                    209: 
                    210: /*-----------------------------------------------------------------------*/
                    211: /**
1.1.1.11  root      212:  * Save screen shot file with filename like 'grab0000.[png|bmp]',
                    213:  * 'grab0001.[png|bmp]', etc... Whether screen shots are saved as BMP
                    214:  * or PNG depends on Hatari configuration.
1.1.1.8   root      215:  */
1.1       root      216: void ScreenSnapShot_SaveScreen(void)
                    217: {
1.1.1.8   root      218:        char *szFileName = malloc(FILENAME_MAX);
1.1.1.6   root      219: 
1.1.1.8   root      220:        if (!szFileName)  return;
1.1.1.5   root      221: 
1.1.1.8   root      222:        ScreenSnapShot_GetNum();
                    223:        /* Create our filename */
                    224:        nScreenShots++;
1.1.1.9   root      225: #if HAVE_LIBPNG
                    226:        /* try first PNG */
                    227:        sprintf(szFileName,"%s/grab%4.4d.png", Paths_GetWorkingDir(), nScreenShots);
1.1.1.12  root      228:        if (ScreenSnapShot_SavePNG(sdlscrn, szFileName) > 0)
1.1.1.9   root      229:        {
                    230:                fprintf(stderr, "Screen dump saved to: %s\n", szFileName);
                    231:                free(szFileName);
                    232:                return;
                    233:        }
                    234: #endif
1.1.1.8   root      235:        sprintf(szFileName,"%s/grab%4.4d.bmp", Paths_GetWorkingDir(), nScreenShots);
                    236:        if (SDL_SaveBMP(sdlscrn, szFileName))
                    237:                fprintf(stderr, "Screen dump failed!\n");
                    238:        else
                    239:                fprintf(stderr, "Screen dump saved to: %s\n", szFileName);
1.1.1.5   root      240: 
1.1.1.8   root      241:        free(szFileName);
1.1       root      242: }
                    243: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.