- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用来自 Apple 的 GLImageProcessing 示例中的一些 EAGLView 代码对 UIImage 进行一些图像处理。示例代码配置为对预装图像(Image.png)执行处理。我正在尝试修改代码,以便它接受我选择的 UIImage(或至少 CGImage 数据)并处理它。问题是,纹理加载器方法 loadTexture()(如下)似乎只接受 C 结构作为参数,而我无法让它接受 UIImage* 或 CGImage 作为参数.有人能告诉我如何弥合差距,以便我可以将 UIImage 传递给 C 方法吗?
------------ 来自 Texture.h ----------------
#ifndef TEXTURE_H
#define TEXTURE_H
#include "Imaging.h"
void loadTexture(const char *name, Image *img, RendererInfo *renderer);
#endif /* TEXTURE_H */
----------------来自 Texture.m--------------------
#import <UIKit/UIKit.h>
#import "Texture.h"
static unsigned int nextPOT(unsigned int x)
{
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
return x + 1;
}
// This is not a fully generalized image loader. It is an example of how to use
// CGImage to directly access decompressed image data. Only the most commonly
// used image formats are supported. It will be necessary to expand this code
// to account for other uses, for example cubemaps or compressed textures.
//
// If the image format is supported, this loader will Gen a OpenGL 2D texture object
// and upload texels from it, padding to POT if needed. For image processing purposes,
// border pixels are also replicated here to ensure proper filtering during e.g. blur.
//
// The caller of this function is responsible for deleting the GL texture object.
void loadTexture(const char *name, Image *img, RendererInfo *renderer)
{
GLuint texID = 0, components, x, y;
GLuint imgWide, imgHigh; // Real image size
GLuint rowBytes, rowPixels; // Image size padded by CGImage
GLuint POTWide, POTHigh; // Image size padded to next power of two
CGBitmapInfo info; // CGImage component layout info
CGColorSpaceModel colormodel; // CGImage colormodel (RGB, CMYK, paletted, etc)
GLenum internal, format;
GLubyte *pixels, *temp = NULL;
CGImageRef CGImage = [UIImage imageNamed:[NSString stringWithUTF8String:name]].CGImage;
rt_assert(CGImage);
if (!CGImage)
return;
// Parse CGImage info
info = CGImageGetBitmapInfo(CGImage); // CGImage may return pixels in RGBA, BGRA, or ARGB order
colormodel = CGColorSpaceGetModel(CGImageGetColorSpace(CGImage));
size_t bpp = CGImageGetBitsPerPixel(CGImage);
if (bpp < 8 || bpp > 32 || (colormodel != kCGColorSpaceModelMonochrome && colormodel != kCGColorSpaceModelRGB))
{
// This loader does not support all possible CGImage types, such as paletted images
CGImageRelease(CGImage);
return;
}
components = bpp>>3;
rowBytes = CGImageGetBytesPerRow(CGImage); // CGImage may pad rows
rowPixels = rowBytes / components;
imgWide = CGImageGetWidth(CGImage);
imgHigh = CGImageGetHeight(CGImage);
img->wide = rowPixels;
img->high = imgHigh;
img->s = (float)imgWide / rowPixels;
img->t = 1.0;
// Choose OpenGL format
switch(bpp)
{
default:
rt_assert(0 && "Unknown CGImage bpp");
case 32:
{
internal = GL_RGBA;
switch(info & kCGBitmapAlphaInfoMask)
{
case kCGImageAlphaPremultipliedFirst:
case kCGImageAlphaFirst:
case kCGImageAlphaNoneSkipFirst:
format = GL_BGRA;
break;
default:
format = GL_RGBA;
}
break;
}
case 24:
internal = format = GL_RGB;
break;
case 16:
internal = format = GL_LUMINANCE_ALPHA;
break;
case 8:
internal = format = GL_LUMINANCE;
break;
}
// Get a pointer to the uncompressed image data.
//
// This allows access to the original (possibly unpremultiplied) data, but any manipulation
// (such as scaling) has to be done manually. Contrast this with drawing the image
// into a CGBitmapContext, which allows scaling, but always forces premultiplication.
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(CGImage));
rt_assert(data);
pixels = (GLubyte *)CFDataGetBytePtr(data);
rt_assert(pixels);
// If the CGImage component layout isn't compatible with OpenGL, fix it.
// On the device, CGImage will generally return BGRA or RGBA.
// On the simulator, CGImage may return ARGB, depending on the file format.
if (format == GL_BGRA)
{
uint32_t *p = (uint32_t *)pixels;
int i, num = img->wide * img->high;
if ((info & kCGBitmapByteOrderMask) != kCGBitmapByteOrder32Host)
{
// Convert from ARGB to BGRA
for (i = 0; i < num; i++)
p[i] = (p[i] << 24) | ((p[i] & 0xFF00) << 8) | ((p[i] >> 8) & 0xFF00) | (p[i] >> 24);
}
// All current iPhoneOS devices support BGRA via an extension.
if (!renderer->extension[IMG_texture_format_BGRA8888])
{
format = GL_RGBA;
// Convert from BGRA to RGBA
for (i = 0; i < num; i++)
#if __LITTLE_ENDIAN__
p[i] = ((p[i] >> 16) & 0xFF) | (p[i] & 0xFF00FF00) | ((p[i] & 0xFF) << 16);
#else
p[i] = ((p[i] & 0xFF00) << 16) | (p[i] & 0xFF00FF) | ((p[i] >> 16) & 0xFF00);
#endif
}
}
// Determine if we need to pad this image to a power of two.
// There are multiple ways to deal with NPOT images on renderers that only support POT:
// 1) scale down the image to POT size. Loses quality.
// 2) pad up the image to POT size. Wastes memory.
// 3) slice the image into multiple POT textures. Requires more rendering logic.
//
// We are only dealing with a single image here, and pick 2) for simplicity.
//
// If you prefer 1), you can use CoreGraphics to scale the image into a CGBitmapContext.
POTWide = nextPOT(img->wide);
POTHigh = nextPOT(img->high);
if (!renderer->extension[APPLE_texture_2D_limited_npot] && (img->wide != POTWide || img->high != POTHigh))
{
GLuint dstBytes = POTWide * components;
GLubyte *temp = (GLubyte *)malloc(dstBytes * POTHigh);
for (y = 0; y < img->high; y++)
memcpy(&temp[y*dstBytes], &pixels[y*rowBytes], rowBytes);
img->s *= (float)img->wide/POTWide;
img->t *= (float)img->high/POTHigh;
img->wide = POTWide;
img->high = POTHigh;
pixels = temp;
rowBytes = dstBytes;
}
// For filters that sample texel neighborhoods (like blur), we must replicate
// the edge texels of the original input, to simulate CLAMP_TO_EDGE.
{
GLuint replicatew = MIN(MAX_FILTER_RADIUS, img->wide-imgWide);
GLuint replicateh = MIN(MAX_FILTER_RADIUS, img->high-imgHigh);
GLuint imgRow = imgWide * components;
for (y = 0; y < imgHigh; y++)
for (x = 0; x < replicatew; x++)
memcpy(&pixels[y*rowBytes+imgRow+x*components], &pixels[y*rowBytes+imgRow-components], components);
for (y = imgHigh; y < imgHigh+replicateh; y++)
memcpy(&pixels[y*rowBytes], &pixels[(imgHigh-1)*rowBytes], imgRow+replicatew*components);
}
if (img->wide <= renderer->maxTextureSize && img->high <= renderer->maxTextureSize)
{
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set filtering parameters appropriate for this application (image processing on screen-aligned quads.)
// Depending on your needs, you may prefer linear filtering, or mipmap generation.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, internal, img->wide, img->high, 0, format, GL_UNSIGNED_BYTE, pixels);
}
if (temp) free(temp);
CFRelease(data);
CGImageRelease(CGImage);
img->texID = texID;
}
旁注:以上代码是来自 Apple 的原始且未修改的示例代码,编译时不会产生任何错误。但是,当我尝试修改 .h 和 .m 以接受 UIImage* 参数(如下所示)时,编译器会生成以下错误:"Error: expected declaration specifiers or "..."before UIImage "
------------生成编译器错误的修改.h代码:------------
void loadTexture(const char name, Image *img, RendererInfo *renderer, UIImage* newImage)
最佳答案
您可能正在将此 .h 导入到某处的 .c 中。这告诉编译器使用 C 而不是 Objective-C。 UIKit.h
(还有很多子项)在 Objective-C 中,不能由 C 编译器编译。
您可以将所有 .c 文件重命名为 .m,但您真正想要的可能只是使用 CGImageRef
并导入 CGImage.h
。 CoreGraphics 是基于 C 语言的。 UIKit 是 Objective-C。如果您愿意,Texture.m 在 Objective-C 中是没有问题的。只需确保 Texture.h 是纯 C 语言即可。或者(我经常用 C++ 代码这样做),您可以制作一个 Texture+C.h
header ,它只提供您想要的 C 安全函数暴露。在Objective-C代码中导入Texture.h
,在C代码中导入Texture+C.h
。或者,如果更方便的话,使用 Texture+ObjC.h
以相反的方式命名它们。
关于c++ - 如何让 C 方法接受 UIImage 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1350108/
我有一个应用程序,它将在 SQLDatabase 中保存一组图像,然后用户将拍摄一张照片,我希望能够将它与数据库中的图像相匹配。 我不知道从哪里开始,有人可以帮忙吗?指出我正确的方向? 谢谢 最佳答案
在我的项目中,我有两个 UIViewController。在我的第一个 ViewController 上,我有一个 UIImageView,并且有一个来自 url 的图像。在第二个 ViewContr
如何从上下文创建 UIImage 以便我可以返回它?我只希望它有透明像素。我有一种不同的方法,可以将较小的 UIImage 添加到较大的 UIImage,但我需要从头开始。 + (UIImage *)
我想知道如何将 UIImage 居中到 UIButton。我正在使用 UIButton 的 setImage 方法来放置 UIImage .. 最佳答案 您可以使用 contentMode 属性: b
我从相机获取 UIimages 并将它们分配给要显示的 UIImageViews。当我这样做时,相机会给我一个 1200 x 1600 像素的图像,然后我将其分配给我的应用程序中的 UIImageVi
Swift 4 有 Codable这太棒了。但是UIImage默认情况下不符合它。我们怎么能做到这一点? 我试过 singleValueContainer和 unkeyedContainer exte
我的代码在普通设备上运行良好,但在视网膜设备上创建模糊图像。 有人知道我的问题的解决方案吗? + (UIImage *) imageWithView:(UIView *)view { UIGr
我总是得到:CGImageCreate:无效的图像大小:0 x 0。 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; // En
我有一张图片,想创建一张如下所示的黑色图片: 我有一个 View 和一个 ImageView ,因为它是 subview 。 ImageView 的 alpha 为 0.5,其色调颜色为黑色。 Vie
几天来我一直被困在一些必须非常简单的事情上,我已经搜索了几天,但我似乎无法找到正确的代码来工作。 我有两张图片,一张是背景图片,另一张会遮盖第一张图片。我可以用手势移动蒙版图像。我可以缩放、旋转和移动
我正在尝试拍摄类似背景透明的三角形图像,并根据用户偏好在三角形部分或背景部分上应用圆点图像。例如,我如何将圆点应用于三角形部分,然后将条纹应用于图像的透明部分。有针对这种事情的屏蔽技术吗? 非常感谢,
给出下面的图片,百分比为 25%。如何生成另一个代表给定图像径向部分 25% 的 UIImage?非常感谢任何帮助。 输入 输出 - (UIImage *)radialPortion:(float)p
这是我的代码: - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { // at this point the
我正在使用扩展程序对我的图像进行像素化处理,如下所示: func pixellated(scale: Int = 8) -> UIImage? { guard let ciImage = CI
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我有两个 UIImage 实例。我怎样才能创建第三个 UIImage 这只是拼接在一起的两个原始图像?我想要第一张图片在顶部,第二张图片在底部,这样顶部图片的底部边缘与底部图像的顶部边缘齐平。 最佳答
我有一个 UIImage 和一个 CGPoint,它们告诉我应该朝哪个方向移动它以创建另一个图像。背景可以是任何东西。 提供初始 UIImage 我如何创建新的?最有效的方法是什么? 这是我正在做的:
我有一个 UIImage,显示在 UIImageView 中。我在 UIImageView 中还有另一张图片,它位于第一张图片上方。我希望能够仅在第一张图片的边界内拖动第二张图片。为了让我的目标更清楚
我看过这个问题:UIImage Shadow Trouble 但接受的答案对我不起作用。 我想做的是获取一个 UIImage 并向其添加阴影,然后返回一个全新的 UIImage、阴影和所有内容。 这就
我有两张图片:第一张是用户个人图片,第二张是图标(角标(Badge))。 我想在第一个 uiimage(用户图像)的左下角添加第二个 uiimage(图标),并将它们保存到一个新的 Uiimage 中
我是一名优秀的程序员,十分优秀!