更改每个像素的图像并保存到db [英] Change image per pixel and save to db

查看:106
本文介绍了更改每个像素的图像并保存到db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个网络游戏,我正在寻找一个方法来处理每个像素的图像,并将其存储在一个mysql数据库。确切地说,我需要能够执行以下操作:

I'm making a little web-game and I'm looking for a method to manipulate an image per pixel and store it in a mysql db. To be precise, I need to be able to do the following:


  • 从屏幕上的mysql db加载图片(250 x 250px)例如,每个像素的alpha是0);

  • 随机选择几个像素,将alpha设置为1;

  • 将新图片保存在mysql db中的旧图片

  • 加载包含多个像素的新图片;

  • 随机选择几个像素en将alpha设置为1;
    等..

  • load image (250 x 250px) from mysql db on screen (image is not visible. For example, the alpha of each pixel is 0);
  • Randomly pick a few pixels en set the alpha to 1;
  • Save the new image over the old image in the mysql db
  • load new image with a number of pixels visible;
  • randomly pick a few pixels en set the alpha to 1; etc..

我已经设法让这个东西工作,但是效率非常低。我有我的mysql数据库的成千上万的记录,在每个记录是一个像素存储与正确的位置,颜色和可见性。当图像渲染到屏幕上时,需要读取每个记录。

I've managed to get this thing worked, but in an very inefficient way. I have thousands of records in my mysql db and in each record is a pixel stored with the right position, color and visibility. When the image is rendered to the screen, each record needs to be read out.

我看过html5画布,但还没有找到正确的方法。其实,这时候我不知道最好的方法是什么。希望有人可以帮助这里。

I've looked at html5 canvas but haven't been able yet to figure out the right way. Actually, at this moment I don't know what the best way is. Hope someone can help here.

推荐答案

听起来像我想要的是一个线性同余随机数生成器。它们实际上是一个LOT LOT LOT更复杂的描述(甚至写的名称!)比他们编程和使用。

Sounds to me like what you want is a linear congruential random number generator. They are actually a LOT LOT LOT more complicated to describe (or even write the name of!) than they are to program and use. They're really easy to use.

http://en.wikipedia.org/wiki/Linear_congruential_generator

使用这些,您只能传递当前的 seed value(这是您首先设置的像素,或您先前设置的像素)到表达式中:

Using these, you can pass just the current seed value (which is the pixel you picked to set first, or which pixel you previously set, into the expression:

seed = ((seed * multiplier) + increment) % modulus.

选择值如下:
seed =您的初始值必须大于0并小于模数。

Pick the values as follows: seed = your initial value must be bigger than 0 and smaller than modulus.

modulus =图片中的总像素数(即 width * height ), 2 ^ 31或更少的工作在PHP。它可以多一点,虽然你的代码将不太有效:下一个2的幂大于像素数量可能是一个不错的选择。

modulus = the total number of pixels in your image (ie, width * height), which must be 2^31 or less to work in PHP. It can be a bit more, though your code will be less efficient: the next power of 2 larger than the number of pixels might be a good choice.

increment = Easiest:1或者,素数最多为23,622,320,123。或者如果不是这样,那么至少与模量相关,并且小于(2 ^ 53-(模数*乘数))。

increment = Easiest: "1". Or, a prime number at most 23,622,320,123. Or if not that, then at the very least relatively prime with the modulus, and smaller than (2^53-(modulus*multiplier)).

乘法器


  • 一个超过模数的所有素因子可分割的数字。 >
  • 如果模数是4的倍数,则为1的倍数。

  • 小于2 ^ 22。

  • <如果你选择一个2的幂的模数,那么这个乘数可以是(小的素数* 4)+ 1。所以,5或9,或者什么。

仔细选择值将意味着当你重复种子生成代码 modulus 次时,你永远不会得到重复的数字 -

This careful selection of values will mean that when you repeat that seed generation code modulus times, you will never get a repeated number - you will semi-randomly "select" every single pixel only once.

如果您的模数大于 width * height ,您有时会选择一个不在图片中的像素:如果您的图片为10x10或100像素,而您选择了模数为128(下一个最大幂为2),那么你有机会选择100和127之间的数字。在这种情况下,你可以再次循环,直到获得一个有效的像素,或者只是更改较少的像素。

If your modulus is larger than width * height, you will sometimes select a pixel that's not "in" your image: if your image is 10x10, or 100 pixels, and you picked a modulus of 128 (next largest power of 2), then you'd have a chance of it picking numbers between 100 and 127. In this case you can just loop it again until you get a valid pixel, or just change fewer pixels.

但如果它选择一个像素从0到99,那将是使用的像素。因此,对于10x10图像,37的种子可能意味着第四行,第八像素。

But if it picked a pixel from 0 to 99, that'd be the pixel to use. So for a 10x10 image, a seed of 37 might mean fourth row, eighth pixel.

具体:

$pixelX = $seed / $width;
$pixelY = $seed % $width;

这篇关于更改每个像素的图像并保存到db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆