making a stab at generated images by bending the null image plugin

未关闭
#4,197 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
停滞
技术栈
cpp

调研方向

从 src/null.imageio/nullimageio.cpp 开始,重点查看 NullInput::open 以及 scanline 和 tile 读取方法,并审查提议的 OpenImageIO/feeder.h 接口。检查 null 插件如何解析 REST 参数,以及 FEED=feed_gradient 示例应如何运行。完成的标准是:已达成一致的 feeder 设计能够通过 null 插件针对所示用法生成图像数据。

由索引模型根据 Issue 内容生成。

描述

Is your feature request related to a problem? Please describe.

I was looking for 'an easy way in' to introduce arbitrary generated image content with minimal coding effort. My problem was that I have such content and I did not find a way to make OIIO 'suck it in'.

Describe the solution you'd like

I want this capability to 'look like' an image, in order to make it 'palatable' for the higher OIIO functions.

Describe alternatives you've considered

I considered writing a specific plugin, but to flesh out a plugin for the purpose requires considerable coding effort. I looked at the extant plugins, and I found a candidate which can easily be bent to the task: the 'null' image format. While this provides a nice infrastructure, it's core functionality is very simple: colour pixels with some default colour. If, instead, it can be made to colour pixels by calling a user-provided function, it becomes much more versatile and can do the job I want done perfectly.

I have coded a proof of concept. First, I introduced a header 'feeder.h' into the OpenImageIO includes. It looks like this:

#include <map>

// to make generated images easily accessible, we introduce a new
// mode to 'null' images which obtains pixel data by calling a 'feeder'.
// The feeder has a member function 'feed' which receives a pointer to
// the pixel's memory it's supposed to feed into, plus the number of
// bytes it's supposed to fill, and the x, y, and z coordinate of that
// pixel. The feeder has a specific function to satisfy the request,
// resulting in the 'null' image being 'painted' by the feeder instead
// of being initialized with a given constant value. With this method,
// we can introduce arbitrary generated image content with minimal
// coding effort - we simply 'piggyback' on the extant null plugin
// end use it's infrastructure.
// feeders are initialized with a std::function like these:

bool feed_not ( void* trg , int nbytes , int x , int y , int z )
{
  return true ;
}

bool feed_grey ( void* trg , int nbytes , int x , int y , int z )
{
  memset ( trg , 127 , nbytes ) ;
  return true ;
}

bool feed_gradient ( void* trg , int nbytes , int x , int y , int z )
{
  auto * pc = (unsigned char*) trg ;
  pc[0] = x ;
  pc[1] = y ;
  pc[2] = 0 ;
  return true ;
}

// struct feeder_t contains - as of yet - only a single std::fuction
// of the type feed_f - like those above

struct feeder_t
{
  typedef std::function < bool ( void* , // target memory
                                 int ,   // bytes per pixel
                                 int ,   // x
                                 int ,   // y
                                 int     // z
                               ) > feed_f ;

  feed_f feed ;

  feeder_t ( const feed_f & _feed = feed_not )
  : feed ( _feed )
  { }
} ;

// let's set up two 'stock' feeders

feeder_t black_feeder ( feed_grey ) ;
feeder_t gradient_feeder ( feed_gradient ) ;

// finally, we set up a std::map, which provides feeders by name.
// we'll use this map from the plugin code to look up feeders which
// are passed in as part of the REST parameters. This way we can
// do two things:
// - we can add (or remove) feeders anytime
// - we can prescribe their use by passing a FEED=... REST parameter
// The use of the feeder isn't very fast, because it actually walks
// through all the pixels in a nested loop and calls a rather bulky
// function to deposit it's data, but if the image is read once and
// later on the data from the read are used, this is not an issue:
// the slow code is only executed when reading the image.

std::map < std::string , feeder_t * >
  feed_store { { "feed_grey" , & black_feeder } ,
               { "feed_gradient" , & gradient_feeder }
             } ;

The changes to the plugin are small - rather than pasting the entire code here, I just give the diff, I'm sure you'll get my drift:

~/src/OpenImageIO$ git diff
diff --git a/src/null.imageio/nullimageio.cpp b/src/null.imageio/nullimageio.cpp
index 1fce00363..5d4825e82 100644
--- a/src/null.imageio/nullimageio.cpp
+++ b/src/null.imageio/nullimageio.cpp
@@ -10,6 +10,7 @@
 #include <OpenImageIO/filesystem.h>
 #include <OpenImageIO/imageio.h>
 #include <OpenImageIO/strutil.h>
+#include <OpenImageIO/feeder.h>
 
 #include "imageio_pvt.h"
 
@@ -74,6 +75,7 @@ private:
     int m_subimage;                ///< What subimage are we looking at?
     int m_miplevel;                ///< What miplevel are we looking at?
     bool m_mip;                    ///< MIP-mapped?
+    feeder_t * m_feed;
     std::vector<uint8_t> m_value;  ///< Pixel value (if not black)
     ImageSpec m_topspec;
 
@@ -84,6 +86,7 @@ private:
         m_miplevel = -1;
         m_mip      = false;
         m_value.clear();
+        m_feed = nullptr ;
     }
 };
 
@@ -311,6 +314,15 @@ NullInput::open(const std::string& name, ImageSpec& newspec,
         } else if (a.first == "PIXEL") {
             Strutil::extract_from_list_string(fvalue, a.second);
             fvalue.resize(m_topspec.nchannels);
+        } else if (a.first == "FEED") {
+            auto feed = feed_store.find ( std::string ( a.second ) ) ;
+            if ( feed == feed_store.end() ) {
+              std::cout << "can't find feed " << a.second << " in store"
+                        << std::endl ;
+              m_feed = nullptr ;
+            } else {
+              m_feed = feed->second ;
+            }
         } else if (a.first.size() && a.second.size()) {
             parse_param(a.first, a.second, m_topspec);
         }
@@ -362,10 +374,14 @@ NullInput::seek_subimage(int subimage, int miplevel)
 
 
 bool
-NullInput::read_native_scanline(int /*subimage*/, int /*miplevel*/, int /*y*/,
-                                int /*z*/, void* data)
+NullInput::read_native_scanline(int /*subimage*/, int /*miplevel*/, int y,
+                                int z, void* data)
 {
-    if (m_value.size()) {
+    if ( m_feed ) {
+        size_t s = m_spec.pixel_bytes();
+        for (size_t x = 0, e = m_spec.width; x < e; ++x)
+            m_feed->feed ( (unsigned char*)data + s * x , s , x , y , z ) ;
+    } else if (m_value.size()) {
         size_t s = m_spec.pixel_bytes();
         for (int x = 0; x < m_spec.width; ++x)
             memcpy((char*)data + s * x, m_value.data(), s);
@@ -378,10 +394,23 @@ NullInput::read_native_scanline(int /*subimage*/, int /*miplevel*/, int /*y*/,
 
 
 bool
-NullInput::read_native_tile(int /*subimage*/, int /*miplevel*/, int /*x*/,
-                            int /*y*/, int /*z*/, void* data)
+NullInput::read_native_tile(int /*subimage*/, int /*miplevel*/, int x,
+                            int y, int z, void* data)
 {
-    if (m_value.size()) {
+    if ( m_feed ) {
+      auto w = m_topspec.tile_width ;
+      auto h = m_topspec.tile_height ;
+      auto xx = ( x / w ) * w ;
+      auto yy = ( y / h ) * h ;
+      size_t s = m_spec.pixel_bytes();
+      size_t ofs = 0 ;
+      for (size_t v = 0 ; v < h ; ++v ) {
+        for ( size_t u = 0 ; u < w ; ++u , ofs += s ) {
+          m_feed->feed ( (unsigned char*)data + ofs , s ,
+                         u + xx , v + yy , z ) ;
+        }
+      }
+    } else if (m_value.size()) {
         size_t s = m_spec.pixel_bytes();
         for (size_t x = 0, e = m_spec.tile_pixels(); x < e; ++x)
             memcpy((char*)data + s * x, m_value.data(), s);

To use the 'feeder', it can be specified in the REST parameters, passing an image 'filename' like this:
foo.null?RES=640x480&CHANNELS=3&FEED=feed_gradient&TYPE=uint8

主要语言
C++
星标
2.4k
派生
727
平均合并
4 天 8 小时
30 天内合并 PR
67

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AcademySoftwareFoundation/OpenImageIO 的其他 Issue

查看 AcademySoftwareFoundation/OpenImageIO 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。