File: fieldimageloader.cpp

package info (click to toggle)
asc 2.6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 82,860 kB
  • ctags: 26,395
  • sloc: cpp: 158,660; sh: 11,274; ansic: 6,878; makefile: 604; perl: 138
file content (267 lines) | stat: -rw-r--r-- 8,810 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/***************************************************************************
                          textfile_evaluation.cpp  -  description
                             -------------------
    begin                : Thu Oct 06 2002
    copyright            : (C) 2002 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <vector>
#include <algorithm>
#include <iostream>
#include <SDL_image.h>
#include <boost/regex.hpp>

#include "global.h"
#include "ascstring.h"
#include "fieldimageloader.h"
#include "typen.h"
#include "graphics/blitter.h"

#include "basestreaminterface.h"
#include "stringtokenizer.h"
#include "graphics/surface2png.h"


const char* fileNameDelimitter = " =*/+<>,";


void snowify( Surface& s, bool adaptive )
{
   if ( !s.valid() )
      return;
   
   if ( s.GetPixelFormat().BitsPerPixel() != 32 )
      return;


   const int targetWhite = 210;

   float avg = 0;
   if ( adaptive ) {
      for ( int y = 0; y < s.h(); ++y ) {
         const char* c = (char*) s.pixels();
         c += y * s.pitch();
         for ( int x = 0; x < s.w(); ++x ) {
            uint32_t* i = (uint32_t*) c;
            avg +=  ((*i >> s.GetPixelFormat().Rshift()) & 0xff ) 
                  + ((*i >> s.GetPixelFormat().Gshift()) & 0xff ) 
                  + ((*i >> s.GetPixelFormat().Bshift()) & 0xff ) ;
            c += 4;
         }
      }

      avg /= float(s.h() * s.w() * 3);
   } else
      avg = 150;

   for ( int y = 0; y < s.h(); ++y ) {
      char* c = (char*) s.pixels();
      c += y * s.pitch();
      for ( int x = 0; x < s.w(); ++x ) {
         uint32_t* i = (uint32_t*) c;
         if ( ((*i >> s.GetPixelFormat().Ashift()) & 0xff ) != Surface::transparent ) {
            int v =  ((*i >> s.GetPixelFormat().Rshift()) & 0xff ) 
                  + ((*i >> s.GetPixelFormat().Gshift()) & 0xff ) 
                  + ((*i >> s.GetPixelFormat().Bshift()) & 0xff ) ;
            v /= 3;
            
            int nv = targetWhite + ( v - int(avg)) * (255-targetWhite) / 64;
            if ( nv > 255)
               nv = 255;
            if ( nv < 0 )
               nv = 0;
            
            *i = (nv << s.GetPixelFormat().Rshift()) | (nv << s.GetPixelFormat().Gshift() ) | (nv << s.GetPixelFormat().Bshift()) | (*i & s.GetPixelFormat().Amask() );
         }
         c += 4;
      }
   }
}

bool imageEmpty( const Surface&  s ) 
{
   bool allWhite = true;
   bool allTransparent = true;
   int amask = s.GetPixelFormat().Amask();
   int areference = Surface::transparent << s.GetPixelFormat().Ashift();
   
   int colmask = s.GetPixelFormat().Rmask() | s.GetPixelFormat().Gmask() | s.GetPixelFormat().Bmask();
   int colref = (0xff << s.GetPixelFormat().Rshift()) | (0xff << s.GetPixelFormat().Gshift()) | (0xff << s.GetPixelFormat().Bshift());
   
   for ( int y = 0; y < s.h(); ++y ) {
      const char* c = (char*) s.pixels();
      c += y * s.pitch();
      for ( int x = 0; x < s.w(); ++x ) {
         const uint32_t* i = (uint32_t*) c;
         if( (*i & amask) != areference )
            allTransparent  = false;

         if ( (*i & colmask) != colref )
            allWhite = false;
         
         c += 4;
      }
      if ( !allWhite && !allTransparent )
         return false;
   }
   return allWhite || allTransparent;
}

vector<Surface> loadASCFieldImageArray ( const ASCString& file, int num )
{
   vector<Surface> images;

   tnfilestream fs ( file, tnstream::reading );
   
   Surface s ( IMG_Load_RW ( SDL_RWFromStream( &fs ), 1));
   if ( !s.valid() )
      fatalError("could not read image " + file );
   
   if ( s.GetPixelFormat().BitsPerPixel() == 8 )
      s.assignDefaultPalette();

      
   int depth = s.GetPixelFormat().BitsPerPixel();
   
   for ( int i = 0; i < num; i++ ) {
      int x1 = (i % 10) * 100;
      int y1 = (i / 10) * 100;
      if ( depth > 8 && depth < 32 )
         depth = 32;
         
      Surface s2 = Surface::createSurface(fieldsizex,fieldsizey, depth );
      
      if ( s2.GetPixelFormat().BitsPerPixel() != 8 || s.GetPixelFormat().BitsPerPixel() != 8 ) {
         
         bool colorKeyAllowed = false;
         static boost::regex pcx( ".*\\.pcx$");
         boost::smatch what;
         if( boost::regex_match( copytoLower(file) , what, pcx)) {
            // warningMessage("Truecolor PCX image detected: " + file);
            colorKeyAllowed = true;
         }
         
         if ( s.GetPixelFormat().BitsPerPixel() == 32 ) {
            MegaBlitter<4,4,ColorTransform_None,ColorMerger_PlainOverwrite,SourcePixelSelector_Rectangle > blitter;
            blitter.setSrcRectangle(SDLmm::SRect(SPoint(x1,y1),fieldsizex,fieldsizey));
            blitter.blit( s, s2, SPoint(0,0)  );
         } else {
            s2.Blit( s, SDLmm::SRect(SPoint(x1,y1),fieldsizex,fieldsizey), SPoint(0,0));
         }
         applyLegacyFieldMask(s2,0,0, colorKeyAllowed);
         if ( colorKeyAllowed ) 
            s2.ColorKey2AlphaChannel();
         
         s2.detectColorKey();
         if ( depth != 32 || imageEmpty(s2))
            images.push_back( Surface() );
         else
            images.push_back ( s2 );
         
       } else {
          // we don't want any transformations from one palette to another; we just assume that all 8-Bit images use the same colorspace
          MegaBlitter<1,1,ColorTransform_None,ColorMerger_AlphaOverwrite,SourcePixelSelector_Rectangle > blitter;
          blitter.setSrcRectangle(SDLmm::SRect(SPoint(x1,y1),fieldsizex,fieldsizey));
          blitter.blit( s, s2, SPoint(0,0)  );
          applyFieldMask(s2);
          s2.detectColorKey();
          images.push_back ( s2 );
       }
   }
   return images;
}

Surface loadASCFieldImage ( const ASCString& file, bool applyFieldMaskToImage )
{
   StringTokenizer st ( file, fileNameDelimitter );
   FileName fn = st.getNextToken();
   fn.toLower();

   displayLogMessage ( 6, "loading file " + file + "\n" );

   if ( fn.suffix() == "png" ) {
      SDLmm::Surface* s = NULL;
      do {
         tnfilestream fs ( fn, tnstream::reading );
         RWOPS_Handler rwo( SDL_RWFromStream( &fs ) );
         SDLmm::Surface s2 ( IMG_LoadPNG_RW ( rwo.Get() ));
         rwo.Close();
         // s2.SetAlpha ( SDL_SRCALPHA, SDL_ALPHA_OPAQUE );
         if ( !s )
            s = new SDLmm::Surface ( s2 );
         else {
            int res = s->Blit ( s2 );
            if ( res < 0 )
               warningMessage ( "ImageProperty::operation_eq - couldn't blit surface "+fn);
         }

         fn = st.getNextToken();
      } while ( !fn.empty() );
      if ( s )  {
         Surface s3( *s );
         if ( applyFieldMaskToImage )
            applyFieldMask(s3,0,0,false);

         delete s;
         return s3;
      } else
         return Surface();

   } else
      if ( fn.suffix() == "pcx" ) {
         SDL_Surface* surface;
         {
            tnfilestream fs ( fn, tnstream::reading );

            RWOPS_Handler rwo ( SDL_RWFromStream( &fs ));
            surface = IMG_LoadPCX_RW ( rwo.Get() );
            rwo.Close();
         }

         if ( !surface )
            errorMessage( "error loading file " + fn );
            
         Surface s ( surface );

         if ( s.GetPixelFormat().BitsPerPixel() == 8)
            s.SetColorKey( SDL_SRCCOLORKEY, 255 );
         /*
         else 
            s.SetColorKey( SDL_SRCCOLORKEY, 0xfefefe );
         s.SetAlpha(SDL_SRCALPHA,SDL_ALPHA_OPAQUE);
         */

         fn = st.getNextToken();
         while ( !fn.empty() ) {
            tnfilestream fs ( fn, tnstream::reading );
            RWOPS_Handler rwo( SDL_RWFromStream( &fs ) );
            SDLmm::Surface s2 ( IMG_LoadPNG_RW ( rwo.Get() ));
            rwo.Close();
            int res = s.Blit ( s2 );
            if ( res < 0 )
               warningMessage ( "ImageProperty::operation_eq - couldn't blit surface "+fn);

            fn = st.getNextToken();
         }


         if ( applyFieldMaskToImage )
            if ( s.w() >= fieldsizex && s.h() >= fieldsizey )
               applyFieldMask(s,0,0,false);

         return s;
      }
   throw ASCexception();
}