summaryrefslogtreecommitdiff
path: root/common/image.cpp
blob: 02b963823274657f1b95e9dd1ec6a238e6ae78bd (plain)
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
// Image I/O routines
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "image.h"
#include "file.h"

// =============================================================================
// Function declarations (functions from the im_xxx.cpp files)

LC_IMAGE* OpenJPG (char* filename);
LC_IMAGE* OpenBMP (char* filename);
LC_IMAGE* OpenPNG (char* filename);
LC_IMAGE* OpenGIF (File* file);

bool SaveJPG (char* filename, LC_IMAGE* image, int quality, bool progressive);
bool SaveBMP (char* filename, LC_IMAGE* image, bool quantize);
bool SavePNG (char* filename, LC_IMAGE* image, bool transparent, bool interlaced, unsigned char* background);
bool SaveGIF (File* file, LC_IMAGE* image, bool transparent, bool interlaced, unsigned char* background);

// =============================================================================
// Static functions

static LC_IMAGE* ResizeImage(LC_IMAGE* image)
{
  int i, j;
  long shifted_x, shifted_y;
  if (image == NULL)
    return NULL;

  shifted_x = image->width;
  for (i = 0; ((i < 16) && (shifted_x != 0)); i++)
    shifted_x = shifted_x >> 1;
  shifted_x = (i != 0) ? 1 << (i-1) : 1;

  shifted_y = image->height;
  for (i = 0; ((i < 16) && (shifted_y != 0)); i++)
    shifted_y = shifted_y >> 1;
  shifted_y = (i != 0) ? 1 << (i-1) : 1;

  if ((shifted_x == image->width) && (shifted_y == image->height))
    return image;

  LC_IMAGE* newimage = (LC_IMAGE*)malloc(shifted_x*shifted_y*3+sizeof(LC_IMAGE));
  newimage->width = (unsigned short)shifted_x;
  newimage->height = (unsigned short)shifted_y;
  newimage->bits = (unsigned char*)newimage + sizeof(LC_IMAGE);
  memset(newimage->bits, 0, shifted_x*shifted_y*3);

  float accumx, accumy;
  int stx, sty;
  unsigned char *oldbits = (unsigned char*)image->bits,
                *newbits = (unsigned char*)newimage->bits;

  for (j = 0; j < image->height; j++)
  {
    accumy = (float)newimage->height*j/(float)image->height;
    sty = (int)floor(accumy);

    for (i = 0; i < image->width; i++)
    {
      accumx = (float)newimage->width*i/(float)image->width;
      stx = (int)floor(accumx);

      newbits[(stx+sty*newimage->width)*3] = oldbits[(i+j*image->width)*3];
      newbits[(stx+sty*newimage->width)*3+1] = oldbits[(i+j*image->width)*3+1];
      newbits[(stx+sty*newimage->width)*3+2] = oldbits[(i+j*image->width)*3+2];
    }
  }

  free(image);
  return newimage;
}

// =============================================================================
// Global functions

// Reads a file from disk
LC_IMAGE* OpenImage(char* filename)
{
  char ext[5];
  if (strlen(filename) != 0)
  {
    char *p = strrchr(filename, '.');
    if (p != NULL)
      strcpy (ext, p+1);
  }
  strlwr(ext);

  if ((strcmp(ext, "jpg") == 0) || (strcmp (ext, "jpeg") == 0))
    return ResizeImage(OpenJPG(filename));
  if (strcmp(ext, "bmp") == 0)
    return ResizeImage(OpenBMP(filename));
  if (strcmp(ext, "png") == 0)
    return ResizeImage(OpenPNG(filename));
  if ((strcmp (ext, "gif") == 0) || (strcmp (ext, "tmp") == 0))
  {
    FileDisk file;
    if (!file.Open(filename, "rb"))
      return NULL;
    LC_IMAGE* image = ResizeImage(OpenGIF(&file));
    file.Close();
    return image;
  }

//	MessageBox (NULL, "Unknown File Format", "Error", MB_ICONSTOP);

  return NULL;
}

LC_IMAGE* OpenImage(File* file, unsigned char format)
{
  if (format != LC_IMAGE_GIF)
    return NULL;
  return OpenGIF(file);
}

bool SaveImage(char* filename, LC_IMAGE* image, LC_IMAGE_OPTS* opts)
{
  char ext[5];
  if (strlen(filename) != 0)
  {
    char *p = strrchr(filename, '.');
    if (p != NULL)
      strcpy(ext, p+1);
  }
  strlwr(ext);

  if ((strcmp (ext, "jpg") == 0) || (strcmp (ext, "jpeg") == 0))
    return SaveJPG(filename, image, opts->quality, opts->interlaced);

  if (strcmp (ext, "gif") == 0)
  {
    FileDisk file;
    if (!file.Open(filename, "wb"))
      return false;

    bool ret = SaveGIF(&file, image, opts->transparent, opts->interlaced, opts->background);
    file.Close();
    return ret;
  }

  if (strcmp (ext, "bmp") == 0)
    return SaveBMP(filename, image, opts->truecolor == false);

  if (strcmp (ext, "png") == 0)
    return SavePNG(filename, image, opts->transparent, opts->interlaced, opts->background);

//	MessageBox (NULL, "Could not save file", "Error", MB_ICONSTOP);

  return false;
}

bool SaveImage(File* file, LC_IMAGE* image, LC_IMAGE_OPTS* opts)
{
  if (opts->format != LC_IMAGE_GIF)
    return false;
  return SaveGIF(file, image, opts->transparent, opts->interlaced, opts->background);
}



#ifdef LC_WINDOWS
//#include <windows.h>
//#include <vfw.h>

#define AVIIF_KEYFRAME	0x00000010L // this frame is a key frame.

void SaveVideo(char* filename, LC_IMAGE** images, int count, float fps)
{
/*
	AVISTREAMINFO strhdr;
	PAVIFILE pfile = NULL;
	PAVISTREAM ps = NULL, psCompressed = NULL;
	AVICOMPRESSOPTIONS opts;
	AVICOMPRESSOPTIONS FAR * aopts[1] = { &opts };
	_fmemset(&opts, 0, sizeof(opts));

	// first let's make sure we are running on 1.1
	WORD wVer = HIWORD(VideoForWindowsVersion());
	if (wVer < 0x010a)
	{
		AfxMessageBox("Video for Windows 1.1 or later required", MB_OK|MB_ICONSTOP);
		return;
	}

	AVIFileInit();

	if (AVIFileOpen(&pfile, fn, OF_WRITE | OF_CREATE, NULL) == AVIERR_OK)
	{
		// Fill in the header for the video stream.
		_fmemset(&strhdr, 0, sizeof(strhdr));
		strhdr.fccType = streamtypeVIDEO;
		strhdr.fccHandler = 0;
		strhdr.dwScale = 1;
/////////////// set FPS
		strhdr.dwRate = 15;	 // 15 fps
		strhdr.dwSuggestedBufferSize  = plpbi[0]->biSizeImage;
		SetRect(&strhdr.rcFrame, 0, 0, (int) plpbi[0]->biWidth, (int) plpbi[0]->biHeight);

		// And create the stream.
		if (AVIFileCreateStream(pfile, &ps, &strhdr) == AVIERR_OK)
		if (AVISaveOptions(AfxGetMainWnd()->m_hWnd, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
		if (AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL) == AVIERR_OK)
		if (AVIStreamSetFormat(psCompressed, 0, plpbi[0], plpbi[0]->biSize + plpbi[0]->biClrUsed * sizeof(RGBQUAD)) == AVIERR_OK)
		{
			float fPause = (float)AfxGetApp()->GetProfileInt("Default", "AVI Pause", 100)/100;
			int time = (int)(fPause * 15);
///////////// set FPS
			time = 1;
		
			for (int i = 0; i < nCount; i++) 
			{
				if (AVIStreamWrite(psCompressed, i * time, 1, 
					(LPBYTE) plpbi[i] +	plpbi[i]->biSize + plpbi[i]->biClrUsed * sizeof(RGBQUAD),
					plpbi[i]->biSizeImage, i%5 ? 0 : AVIIF_KEYFRAME, NULL, NULL) != AVIERR_OK)
					break;
			}
		}
	}
	
	// Now close the file
	if (ps) AVIStreamClose(ps);
	if (psCompressed) AVIStreamClose(psCompressed);
	if (pfile) AVIFileClose(pfile);
	AVIFileExit();
*/
}
#else
void SaveVideo(char* filename, LC_IMAGE** images, int count, float fps)
{
  //	SystemDoMessageBox("Format not supported under this platform.", LC_MB_OK|LC_MB_ERROR);
}
#endif