1 // Aseprite Document Library
2 // Copyright (c) 2001-2015 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "doc/cel_data_io.h"
12 
13 #include "base/serialization.h"
14 #include "base/unique_ptr.h"
15 #include "doc/cel_data.h"
16 #include "doc/subobjects_io.h"
17 #include "doc/user_data_io.h"
18 
19 #include <iostream>
20 
21 namespace doc {
22 
23 using namespace base::serialization;
24 using namespace base::serialization::little_endian;
25 
write_celdata(std::ostream & os,const CelData * celdata)26 void write_celdata(std::ostream& os, const CelData* celdata)
27 {
28   write32(os, celdata->id());
29   write32(os, celdata->bounds().x);
30   write32(os, celdata->bounds().y);
31   write32(os, celdata->bounds().w);
32   write32(os, celdata->bounds().h);
33   write8(os, celdata->opacity());
34   write32(os, celdata->image()->id());
35   write_user_data(os, celdata->userData());
36 }
37 
read_celdata(std::istream & is,SubObjectsIO * subObjects,bool setId)38 CelData* read_celdata(std::istream& is, SubObjectsIO* subObjects, bool setId)
39 {
40   ObjectId id = read32(is);
41   int x = read32(is);
42   int y = read32(is);
43   int w = read32(is);
44   int h = read32(is);
45   int opacity = read8(is);
46   ObjectId imageId = read32(is);
47   UserData userData = read_user_data(is);
48 
49   ImageRef image(subObjects->getImageRef(imageId));
50   if (!image)
51     return nullptr;
52 
53   base::UniquePtr<CelData> celdata(new CelData(image));
54   celdata->setBounds(gfx::Rect(x, y, w, h));
55   celdata->setOpacity(opacity);
56   celdata->setUserData(userData);
57   if (setId)
58     celdata->setId(id);
59   return celdata.release();
60 }
61 
62 }
63