MCPcopy Create free account
hub / github.com/LibreSprite/LibreSprite / read_image

Function read_image

src/doc/image_io.cpp:95–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93}
94
95Image* read_image(std::istream& is, bool setId)
96{
97 ObjectId id = read32(is);
98 int pixelFormat = read8(is); // Pixel format
99 int width = read16(is); // Width
100 int height = read16(is); // Height
101 uint32_t maskColor = read32(is); // Mask color
102
103 if ((pixelFormat != IMAGE_RGB &&
104 pixelFormat != IMAGE_GRAYSCALE &&
105 pixelFormat != IMAGE_INDEXED &&
106 pixelFormat != IMAGE_BITMAP) ||
107 (width < 1 || height < 1) ||
108 (width > 0xfffff || height > 0xfffff))
109 return nullptr;
110
111 std::unique_ptr<Image> image(Image::create(static_cast<PixelFormat>(pixelFormat), width, height));
112 int rowSize = image->getRowStrideSize();
113
114#if 0
115 {
116 for (int c=0; c<image->height(); c++)
117 is.read((char*)image->getPixelAddress(0, c), rowSize);
118 }
119#else
120 {
121 int avail_bytes = read32(is);
122
123 z_stream zstream;
124 zstream.zalloc = (alloc_func)0;
125 zstream.zfree = (free_func)0;
126 zstream.opaque = (voidpf)0;
127
128 int err = inflateInit(&zstream);
129 if (err != Z_OK)
130 throw base::Exception("ZLib error %d in inflateInit().", err);
131
132 int uncompressed_size = image->height() * rowSize;
133 int uncompressed_offset = 0;
134 int remain = avail_bytes;
135
136 std::vector<uint8_t> compressed(4096);
137 uint8_t* address = image->getPixelAddress(0, 0);
138 uint8_t* address_end = image->getPixelAddress(0, 0) + uncompressed_size;
139
140 while (remain > 0) {
141 int len = MIN(remain, (int)compressed.size());
142 if (is.read((char*)&compressed[0], len).fail()) {
143 ASSERT(false);
144 throw base::Exception("Error reading stream to restore image");
145 }
146
147 int bytes_read = (int)is.gcount();
148 if (bytes_read == 0) {
149 ASSERT(remain == 0);
150 break;
151 }
152

Callers 1

read_layerFunction · 0.70

Calls 9

read8Function · 0.85
ExceptionClass · 0.85
getRowStrideSizeMethod · 0.80
getPixelAddressMethod · 0.80
heightMethod · 0.45
sizeMethod · 0.45
setMaskColorMethod · 0.45
setIdMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected