void zu::xml_writer::do_function_declaration_node(zu::function_declaration_node * const node, int lvl) { // FIXME openTag(node, lvl); openTag("identifier", lvl + 2); node->identifier()->accept(this, lvl + 4); closeTag("identifier", lvl + 2); openTag("visibility", lvl + 2); os() << std::string(lvl + 4, ' ') << node->visibility() << std::endl; closeTag("visibility", lvl + 2); openTag("external", lvl + 2); os() << std::string(lvl + 4, ' ') << node->ext() << std::endl; closeTag("external", lvl + 2); if(node->return_type() != nullptr){ openTag("return_type type=" + convert_types(node->return_type()), lvl + 2); closeTag("return_type", lvl + 2); } openTag("arguments", lvl + 2); if(node->args() != nullptr) node->args()->accept(this, lvl + 4); closeTag("arguments", lvl + 2); openTag("literal", lvl + 2); if(node->literal() != nullptr) node->literal()->accept(this, lvl + 4); closeTag("literal", lvl + 2); closeTag(node, lvl); }
bool ImageBuf::copy_pixels (int xbegin, int xend, int ybegin, int yend, TypeDesc format, void *result) const { #if 1 // Fancy method -- for each possible base type that the user // wants for a destination type, call a template specialization. switch (format.basetype) { case TypeDesc::UINT8 : copy_pixels<unsigned char> (xbegin, xend, ybegin, yend, (unsigned char *)result); break; case TypeDesc::INT8: copy_pixels<char> (xbegin, xend, ybegin, yend, (char *)result); break; case TypeDesc::UINT16 : copy_pixels<unsigned short> (xbegin, xend, ybegin, yend, (unsigned short *)result); break; case TypeDesc::INT16 : copy_pixels<short> (xbegin, xend, ybegin, yend, (short *)result); break; case TypeDesc::UINT : copy_pixels<unsigned int> (xbegin, xend, ybegin, yend, (unsigned int *)result); break; case TypeDesc::INT : copy_pixels<int> (xbegin, xend, ybegin, yend, (int *)result); break; case TypeDesc::HALF : copy_pixels<half> (xbegin, xend, ybegin, yend, (half *)result); break; case TypeDesc::FLOAT : copy_pixels<float> (xbegin, xend, ybegin, yend, (float *)result); break; case TypeDesc::DOUBLE : copy_pixels<double> (xbegin, xend, ybegin, yend, (double *)result); break; case TypeDesc::UINT64 : copy_pixels<unsigned long long> (xbegin, xend, ybegin, yend, (unsigned long long *)result); break; case TypeDesc::INT64 : copy_pixels<long long> (xbegin, xend, ybegin, yend, (long long *)result); break; default: return false; } #else // Naive method -- loop over pixels, calling getpixel() size_t usersize = format.size() * nchannels(); float *pel = (float *) alloca (nchannels() * sizeof(float)); for (int y = ybegin; y < yend; ++y) for (int x = xbegin; x < xend; ++x) { getpixel (x, y, pel); convert_types (TypeDesc::TypeFloat, pel, format, result, nchannels()); result = (void *) ((char *)result + usersize); } #endif return true; }
bool ImageInput::read_scanline (int y, int z, TypeDesc format, void *data, stride_t xstride) { // native_pixel_bytes is the size of a pixel in the FILE, including // the per-channel format. stride_t native_pixel_bytes = (stride_t) m_spec.pixel_bytes (true); // perchanfile is true if the file has different per-channel formats bool perchanfile = m_spec.channelformats.size(); // native_data is true if the user asking for data in the native format bool native_data = (format == TypeDesc::UNKNOWN || (format == m_spec.format && !perchanfile)); if (native_data && xstride == AutoStride) xstride = native_pixel_bytes; else m_spec.auto_stride (xstride, format, m_spec.nchannels); // Do the strides indicate that the data area is contiguous? bool contiguous = (native_data && xstride == native_pixel_bytes) || (!native_data && xstride == (stride_t)m_spec.pixel_bytes(false)); // If user's format and strides are set up to accept the native data // layout, read the scanline directly into the user's buffer. if (native_data && contiguous) return read_native_scanline (y, z, data); // Complex case -- either changing data type or stride int scanline_values = m_spec.width * m_spec.nchannels; unsigned char *buf = (unsigned char *) alloca (m_spec.scanline_bytes(true)); bool ok = read_native_scanline (y, z, buf); if (! ok) return false; if (! perchanfile) { // No per-channel formats -- do the conversion in one shot ok = contiguous ? convert_types (m_spec.format, buf, format, data, scanline_values) : convert_image (m_spec.nchannels, m_spec.width, 1, 1, buf, m_spec.format, AutoStride, AutoStride, AutoStride, data, format, xstride, AutoStride, AutoStride); } else { // Per-channel formats -- have to convert/copy channels individually ASSERT (m_spec.channelformats.size() == (size_t)m_spec.nchannels); size_t offset = 0; for (int c = 0; ok && c < m_spec.nchannels; ++c) { TypeDesc chanformat = m_spec.channelformats[c]; ok = convert_image (1 /* channels */, m_spec.width, 1, 1, buf+offset, chanformat, native_pixel_bytes, AutoStride, AutoStride, (char *)data + c*format.size(), format, xstride, AutoStride, AutoStride); offset += chanformat.size (); } } if (! ok) error ("ImageInput::read_scanline : no support for format %s", m_spec.format.c_str()); return ok; }
static inline void transfer_pixels_ (ImageBuf &buf, ColorTransfer *tfunc) { for (ImageBuf::Iterator<T> pixel (buf); pixel.valid(); ++pixel) { convert_types (buf.spec().format, pixel.rawptr(), buf.spec().format, pixel.rawptr(), buf.nchannels(), tfunc, buf.spec().alpha_channel, buf.spec().z_channel); } }
bool ImageInput::read_tile (int x, int y, int z, TypeDesc format, void *data, stride_t xstride, stride_t ystride, stride_t zstride) { stride_t native_pixel_bytes = (stride_t) m_spec.pixel_bytes (true); if (format == TypeDesc::UNKNOWN && xstride == AutoStride) xstride = native_pixel_bytes; m_spec.auto_stride (xstride, ystride, zstride, format, m_spec.nchannels, m_spec.tile_width, m_spec.tile_height); bool contiguous = (xstride == native_pixel_bytes && ystride == xstride*m_spec.tile_width && (zstride == ystride*m_spec.tile_height || zstride == 0)); // If user's format and strides are set up to accept the native data // layout, read the tile directly into the user's buffer. bool rightformat = (format == TypeDesc::UNKNOWN) || (format == m_spec.format && m_spec.channelformats.empty()); if (rightformat && contiguous) return read_native_tile (x, y, z, data); // Simple case // Complex case -- either changing data type or stride int tile_values = m_spec.tile_width * m_spec.tile_height * std::max(1,m_spec.tile_depth) * m_spec.nchannels; boost::scoped_array<char> buf (new char [m_spec.tile_bytes(true)]); bool ok = read_native_tile (x, y, z, &buf[0]); if (! ok) return false; if (m_spec.channelformats.empty()) { // No per-channel formats -- do the conversion in one shot ok = contiguous ? convert_types (m_spec.format, &buf[0], format, data, tile_values) : convert_image (m_spec.nchannels, m_spec.tile_width, m_spec.tile_height, m_spec.tile_depth, &buf[0], m_spec.format, AutoStride, AutoStride, AutoStride, data, format, xstride, ystride, zstride); } else { // Per-channel formats -- have to convert/copy channels individually size_t offset = 0; for (size_t c = 0; c < m_spec.channelformats.size(); ++c) { TypeDesc chanformat = m_spec.channelformats[c]; ok = convert_image (1 /* channels */, m_spec.tile_width, m_spec.tile_height, m_spec.tile_depth, &buf[offset], chanformat, native_pixel_bytes, AutoStride, AutoStride, (char *)data + c*m_spec.format.size(), format, xstride, AutoStride, AutoStride); offset += chanformat.size (); } } if (! ok) error ("ImageInput::read_tile : no support for format %s", m_spec.format.c_str()); return ok; }
void zu::xml_writer::do_var_declaration_node(zu::var_declaration_node * const node, int lvl){ // FIXME openTag(node, lvl); openTag("visibility", lvl + 2); os() << std::string(lvl + 4, ' ') << node->visibility() << std::endl; closeTag("visibility", lvl + 2); openTag("external", lvl + 2); os() << std::string(lvl + 4, ' ') << node->ext() << std::endl; closeTag("external", lvl + 2); openTag("type type=" + convert_types(node->type()), lvl + 2); closeTag("type", lvl + 2); openTag("identifier", lvl + 2); node->identifier()->accept(this, lvl + 4); closeTag("identifier", lvl + 2); closeTag(node, lvl); }
bool NullInput::open(const std::string& name, ImageSpec& newspec, const ImageSpec& config) { m_filename = name; m_subimage = -1; m_miplevel = -1; m_mip = false; m_topspec = config; // std::vector<std::pair<string_view,string_view> > args; // string_view filename = deconstruct_uri (name, &args); std::map<std::string, std::string> args; std::string filename; if (!Strutil::get_rest_arguments(name, filename, args)) return false; if (filename.empty()) return false; // To keep the "null" input reader from reading from ANY name, only // succeed if it ends in ".null" or ".nul" --OR-- if the config has a // special override "null:force" set to nonzero (that lets the caller // guarantee a null input even if the name has no extension, say). if (!Strutil::ends_with(filename, ".null") && !Strutil::ends_with(filename, ".nul") && config.get_int_attribute("null:force") == 0) return false; // Override the config with default resolution if it was not set if (m_topspec.width <= 0) m_topspec.width = 1024; if (m_topspec.height <= 0) m_topspec.height = 1024; if (m_topspec.depth <= 0) m_topspec.depth = 1; if (m_topspec.full_width <= 0) m_topspec.full_width = m_topspec.width; if (m_topspec.full_height <= 0) m_topspec.full_height = m_topspec.height; if (m_topspec.full_depth <= 0) m_topspec.full_depth = m_topspec.depth; if (m_topspec.nchannels <= 0) m_topspec.nchannels = 4; if (m_topspec.format == TypeUnknown) m_topspec.format = TypeFloat; m_filename = filename; std::vector<float> fvalue; for (const auto& a : args) { if (a.first == "RES") { parse_res(a.second, m_topspec.width, m_topspec.height, m_topspec.depth); m_topspec.full_x = m_topspec.x; m_topspec.full_y = m_topspec.y; m_topspec.full_z = m_topspec.z; m_topspec.full_width = m_topspec.width; m_topspec.full_height = m_topspec.height; m_topspec.full_depth = m_topspec.depth; } else if (a.first == "TILE" || a.first == "TILES") { parse_res(a.second, m_topspec.tile_width, m_topspec.tile_height, m_topspec.tile_depth); } else if (a.first == "CHANNELS") { m_topspec.nchannels = Strutil::from_string<int>(a.second); m_topspec.default_channel_names(); } else if (a.first == "MIP") { m_mip = Strutil::from_string<int>(a.second); } else if (a.first == "TEX") { if (Strutil::from_string<int>(a.second)) { if (!m_spec.tile_width) { m_topspec.tile_width = 64; m_topspec.tile_height = 64; m_topspec.tile_depth = 1; } m_topspec.attribute("wrapmodes", "black,black"); m_topspec.attribute("textureformat", "Plain Texture"); m_mip = true; } } else if (a.first == "TYPE") { m_topspec.set_format(TypeDesc(a.second)); } else if (a.first == "PIXEL") { Strutil::extract_from_list_string(fvalue, a.second); fvalue.resize(m_topspec.nchannels); } else if (a.first.size() && a.second.size()) { parse_param(a.first, a.second, m_topspec); } } if (fvalue.size()) { // Convert float to the native type fvalue.resize(m_topspec.nchannels, 0.0f); m_value.resize(m_topspec.pixel_bytes()); convert_types(TypeFloat, fvalue.data(), m_topspec.format, m_value.data(), m_topspec.nchannels); } bool ok = seek_subimage(0, 0); newspec = spec(); return ok; }
bool ImageInput::read_tile (int x, int y, int z, TypeDesc format, void *data, stride_t xstride, stride_t ystride, stride_t zstride) { if (! m_spec.tile_width || ((x-m_spec.x) % m_spec.tile_width) != 0 || ((y-m_spec.y) % m_spec.tile_height) != 0 || ((z-m_spec.z) % m_spec.tile_depth) != 0) return false; // coordinates are not a tile corner // native_pixel_bytes is the size of a pixel in the FILE, including // the per-channel format. stride_t native_pixel_bytes = (stride_t) m_spec.pixel_bytes (true); // perchanfile is true if the file has different per-channel formats bool perchanfile = m_spec.channelformats.size(); // native_data is true if the user asking for data in the native format bool native_data = (format == TypeDesc::UNKNOWN || (format == m_spec.format && !perchanfile)); if (format == TypeDesc::UNKNOWN && xstride == AutoStride) xstride = native_pixel_bytes; m_spec.auto_stride (xstride, ystride, zstride, format, m_spec.nchannels, m_spec.tile_width, m_spec.tile_height); // Do the strides indicate that the data area is contiguous? bool contiguous = (native_data && xstride == native_pixel_bytes) || (!native_data && xstride == (stride_t)m_spec.pixel_bytes(false)); contiguous &= (ystride == xstride*m_spec.tile_width && (zstride == ystride*m_spec.tile_height || zstride == 0)); // If user's format and strides are set up to accept the native data // layout, read the tile directly into the user's buffer. if (native_data && contiguous) return read_native_tile (x, y, z, data); // Simple case // Complex case -- either changing data type or stride size_t tile_values = (size_t)m_spec.tile_pixels() * m_spec.nchannels; std::vector<char> buf (m_spec.tile_bytes(true)); bool ok = read_native_tile (x, y, z, &buf[0]); if (! ok) return false; if (! perchanfile) { // No per-channel formats -- do the conversion in one shot ok = contiguous ? convert_types (m_spec.format, &buf[0], format, data, tile_values) : convert_image (m_spec.nchannels, m_spec.tile_width, m_spec.tile_height, m_spec.tile_depth, &buf[0], m_spec.format, AutoStride, AutoStride, AutoStride, data, format, xstride, ystride, zstride); } else { // Per-channel formats -- have to convert/copy channels individually if (native_data) { ASSERT (contiguous && "Per-channel native input requires contiguous strides"); } ASSERT (format != TypeDesc::UNKNOWN); ASSERT (m_spec.channelformats.size() == (size_t)m_spec.nchannels); size_t offset = 0; for (int c = 0; c < m_spec.nchannels; ++c) { TypeDesc chanformat = m_spec.channelformats[c]; ok = convert_image (1 /* channels */, m_spec.tile_width, m_spec.tile_height, m_spec.tile_depth, &buf[offset], chanformat, native_pixel_bytes, AutoStride, AutoStride, (char *)data + c*format.size(), format, xstride, AutoStride, AutoStride); offset += chanformat.size (); } } if (! ok) error ("ImageInput::read_tile : no support for format %s", m_spec.format.c_str()); return ok; }
bool ImageInput::read_scanlines (int ybegin, int yend, int z, int firstchan, int nchans, TypeDesc format, void *data, stride_t xstride, stride_t ystride) { nchans = std::min (nchans, m_spec.nchannels-firstchan); yend = std::min (yend, spec().y+spec().height); size_t native_pixel_bytes = m_spec.pixel_bytes (firstchan, nchans, true); imagesize_t native_scanline_bytes = clamped_mult64 ((imagesize_t)m_spec.width, (imagesize_t)native_pixel_bytes); bool native = (format == TypeDesc::UNKNOWN); size_t pixel_bytes = native ? native_pixel_bytes : format.size()*nchans; if (native && xstride == AutoStride) xstride = pixel_bytes; stride_t zstride = AutoStride; m_spec.auto_stride (xstride, ystride, zstride, format, nchans, m_spec.width, m_spec.height); bool contiguous = (xstride == (stride_t) native_pixel_bytes && ystride == (stride_t) native_scanline_bytes); // If user's format and strides are set up to accept the native data // layout, read the scanlines directly into the user's buffer. bool rightformat = (format == TypeDesc::UNKNOWN) || (format == m_spec.format && m_spec.channelformats.empty()); if (rightformat && contiguous) { if (firstchan == 0 && nchans == m_spec.nchannels) return read_native_scanlines (ybegin, yend, z, data); else return read_native_scanlines (ybegin, yend, z, firstchan, nchans, data); } // No such luck. Read scanlines in chunks. const imagesize_t limit = 16*1024*1024; // Allocate 16 MB, or 1 scanline int chunk = std::max (1, int(limit / native_scanline_bytes)); std::vector<unsigned char> buf (chunk * native_scanline_bytes); bool ok = true; int scanline_values = m_spec.width * nchans; for (; ok && ybegin < yend; ybegin += chunk) { int y1 = std::min (ybegin+chunk, yend); ok &= read_native_scanlines (ybegin, y1, z, firstchan, nchans, &buf[0]); if (! ok) break; int nscanlines = y1 - ybegin; int chunkvalues = scanline_values * nscanlines; if (m_spec.channelformats.empty()) { // No per-channel formats -- do the conversion in one shot if (contiguous) { ok = convert_types (m_spec.format, &buf[0], format, data, chunkvalues); } else { ok = convert_image (nchans, m_spec.width, nscanlines, 1, &buf[0], m_spec.format, AutoStride, AutoStride, AutoStride, data, format, xstride, ystride, zstride); } } else { // Per-channel formats -- have to convert/copy channels individually size_t offset = 0; for (int c = 0; ok && c < nchans; ++c) { TypeDesc chanformat = m_spec.channelformats[c+firstchan]; ok = convert_image (1 /* channels */, m_spec.width, nscanlines, 1, &buf[offset], chanformat, pixel_bytes, AutoStride, AutoStride, (char *)data + c*m_spec.format.size(), format, xstride, ystride, zstride); offset += chanformat.size (); } } if (! ok) error ("ImageInput::read_scanlines : no support for format %s", m_spec.format.c_str()); data = (char *)data + ystride*nscanlines; } return ok; }