1 | // Aseprite |
2 | // Copyright (C) 2001-2018 David Capello |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/util/pic_file.h" |
12 | #include "base/cfile.h" |
13 | #include "base/file_handle.h" |
14 | #include "base/fs.h" |
15 | #include "doc/image.h" |
16 | #include "doc/mask.h" |
17 | |
18 | #include <memory> |
19 | |
20 | namespace app { |
21 | |
22 | using namespace doc; |
23 | |
24 | // Loads a MSK file (Animator and Animator Pro format) |
25 | Mask* load_msk_file(const char* filename) |
26 | { |
27 | int orig_size = base::file_size(filename); |
28 | int i, c, u, v, byte, magic, size; |
29 | Mask* mask = NULL; |
30 | |
31 | FILE* f = base::open_file_raw(filename, "r" ); |
32 | if (!f) |
33 | return NULL; |
34 | |
35 | size = base::fgetl(f); |
36 | magic = base::fgetw(f); |
37 | |
38 | // Animator Pro MSK format |
39 | if ((size == orig_size) && (magic == 0x9500)) { |
40 | fclose(f); |
41 | |
42 | // Just load an Animator Pro PIC file |
43 | int x, y; |
44 | std::unique_ptr<Image> image(load_pic_file(filename, &x, &y, NULL)); |
45 | |
46 | if (image != NULL && (image->pixelFormat() == IMAGE_BITMAP)) { |
47 | mask = new Mask(); |
48 | mask->replace(gfx::Rect(x, y, image->width(), image->height())); |
49 | mask->bitmap()->copy(image.get(), gfx::Clip(image->bounds())); |
50 | mask->shrink(); |
51 | } |
52 | } |
53 | // Animator MSK format |
54 | else if (orig_size == 8000) { |
55 | mask = new Mask(); |
56 | mask->replace(gfx::Rect(0, 0, 320, 200)); |
57 | |
58 | u = v = 0; |
59 | for (i=0; i<8000; i++) { |
60 | byte = getc(f); |
61 | for (c=0; c<8; c++) { |
62 | mask->bitmap()->putPixel(u, v, byte & (1<<(7-c))); |
63 | u++; |
64 | if (u == 320) { |
65 | u = 0; |
66 | v++; |
67 | } |
68 | } |
69 | } |
70 | fclose(f); |
71 | } |
72 | else { |
73 | fclose(f); |
74 | } |
75 | |
76 | return mask; |
77 | } |
78 | |
79 | // Saves an Animator Pro MSK file (really a PIC file) |
80 | int save_msk_file(const Mask* mask, const char* filename) |
81 | { |
82 | if (mask->bitmap()) |
83 | return save_pic_file(filename, |
84 | mask->bounds().x, |
85 | mask->bounds().y, NULL, |
86 | mask->bitmap()); |
87 | else |
88 | return -1; |
89 | } |
90 | |
91 | } // namespace app |
92 | |