aseprite/src/util/msk_file.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

2007-11-17 02:25:45 +08:00
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2009 David Capello
2007-09-19 07:57:02 +08:00
*
* 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.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <allegro.h>
2007-09-19 07:57:02 +08:00
#include "raster/image.h"
#include "raster/mask.h"
#include "util/pic_file.h"
/* loads a MSK file (Animator and Animator Pro format) */
Mask *load_msk_file(const char *filename)
2007-09-19 07:57:02 +08:00
{
#if (MAKE_VERSION(4, 2, 1) >= MAKE_VERSION(ALLEGRO_VERSION, \
ALLEGRO_SUB_VERSION, \
ALLEGRO_WIP_VERSION))
int orig_size = file_size(filename);
#else
int orig_size = file_size_ex(filename);
#endif
int i, c, u, v, byte, magic, size;
2007-09-19 07:57:02 +08:00
Mask *mask = NULL;
PACKFILE *f;
f = pack_fopen(filename, F_READ);
2007-09-19 07:57:02 +08:00
if (!f)
return NULL;
size = pack_igetl(f);
magic = pack_igetw(f);
2007-09-19 07:57:02 +08:00
/* Animator Pro MSK format */
if ((size == orig_size) && (magic == 0x9500)) {
Image *image;
int x, y;
pack_fclose(f);
2007-09-19 07:57:02 +08:00
/* just load an Animator Pro PIC file */
image = load_pic_file(filename, &x, &y, NULL);
2007-09-19 07:57:02 +08:00
if ((!image) || (image->imgtype != IMAGE_BITMAP)) {
if (image)
image_free(image);
2007-09-19 07:57:02 +08:00
}
else {
mask = mask_new();
2007-09-19 07:57:02 +08:00
mask->x = x;
mask->y = y;
mask->w = image->w;
mask->h = image->h;
mask->bitmap = image;
}
}
/* Animator MSK format */
else if (orig_size == 8000) {
mask = mask_new();
mask_replace(mask, 0, 0, 320, 200);
2007-09-19 07:57:02 +08:00
u = v = 0;
for (i=0; i<8000; i++) {
byte = pack_getc (f);
for (c=0; c<8; c++) {
mask->bitmap->method->putpixel(mask->bitmap, u, v, byte & (1<<(7-c)));
2007-09-19 07:57:02 +08:00
u++;
if (u == 320) {
u = 0;
v++;
}
}
}
pack_fclose(f);
2007-09-19 07:57:02 +08:00
}
else {
pack_fclose(f);
2007-09-19 07:57:02 +08:00
}
return mask;
}
/* saves an Animator Pro MSK file (really a PIC file) */
int save_msk_file(Mask *mask, const char *filename)
2007-09-19 07:57:02 +08:00
{
if (mask->bitmap)
return save_pic_file(filename, mask->x, mask->y, NULL, mask->bitmap);
2007-09-19 07:57:02 +08:00
else
return -1;
}