text:
describe MainPolicy do
  let(:context) { { user: user } }

  describe_rule :login_as? do
    succeed 'user is admin' do
      let(:user) { create :admin }
    end
    succeed 'real user is admin logged in as ta' do
      let(:context) { { user: create(:ta), real_user: create(:admin) } }
    end
    succeed 'real user is admin logged in as student' do
      let(:context) { { user: create(:student), real_user: create(:admin) } }
    end
    failed 'real user is not admin' do
      let(:context) { { user: create(:student), real_user: create(:student) } }
    end
  end

  describe_rule :manage? do
    succeed 'user is admin' do
      let(:user) { create :admin }
    end
    succeed 'user is ta' do
      let(:user) { create :ta }
    end
    succeed 'user is student' do
      let(:user) { create :student }
    end
    failed 'user is not a user' do
      let(:user) { 'not a user' }
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { GithubService } from '../../github.service';

@Component({
  selector: 'app-github',
  templateUrl: './github.component.html',
  styleUrls: ['./github.component.css']
})
export class GithubComponent implements OnInit {

  id: any;
  user: Object;
  user2: Object;
  user3: Object;
  followers: any;

  color: String = 'white';

  filter: String = '-login';
  pseudo: String;
  formUser = {
    pseudo: ''
  };

  constructor(
    private route: ActivatedRoute,
    private github: GithubService,
    private router: Router
  ) { }

  ngOnInit() {
    this.route.params.subscribe( (params) => {
      this.id = params.id;

      this.github.getUserC(this.id).subscribe( (data) => {
        this.user3 = data;
      });

      this.github.getFollowers(this.id).subscribe( (data) => {
        this.followers = data;
      });
    });
    let id = this.route.snapshot.paramMap.get('id');
    this.github.getUser().subscribe( (data) => {
      this.user = data;
    });

    this.github.getUserP().then( (data) => {
      this.user2 = data;
    });

    console.log(this.route.snapshot.data['resolvData']);
  }

  doClick(): void {
    this.router.navigate([`/contact/github/${this.pseudo}`]);
  }

  doSubmit(): void {
    console.log(this.formUser);
  }

  doSort(): void {
    if(this.filter === 'login') {
      this.filter = '-login';
    }
    else {
      this.filter = 'login';
    }
  }
}


text:
```
import React, { Component } from 'react';
import { Router, Route, hashHistory, Link, Redirect } from 'react-router';

const Home = () => <div><h1>Home</h1><Links /></div>;
const About = () => <div><h1>About</h1><Links /></div>;
const Contact = () => <div><h1>Contact</h1><Links /></div>;

const Links = () =>
  <nav>
    <Link to="/">Home</Link>
    <Link to="/about">About</Link>
    <Link to="/contact">Contact</Link>
    <Link to="/contact-us">Contact US</Link>
  </nav>

class App extends Component {
  render() {
    return (
      <Router history={hashHistory}>
        <Route path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact-us" component={Contact} />
        <Redirect from="/contact" to="/contact-us" />
      </Router>
    );
  }
}

export default App;

```


text:
package org.apache.druid.segment.join;

import org.apache.druid.segment.column.ColumnHolder;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class JoinPrefixUtilsTest
{
  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  @Test
  public void test_validatePrefix_null()
  {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Join clause cannot have null or empty prefix");

    JoinPrefixUtils.validatePrefix(null);
  }

  @Test
  public void test_validatePrefix_empty()
  {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Join clause cannot have null or empty prefix");

    JoinPrefixUtils.validatePrefix("");
  }

  @Test
  public void test_validatePrefix_underscore()
  {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Join clause cannot have prefix[_]");

    JoinPrefixUtils.validatePrefix("_");
  }

  @Test
  public void test_validatePrefix_timeColumn()
  {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Join clause cannot have prefix[__time]");

    JoinPrefixUtils.validatePrefix(ColumnHolder.TIME_COLUMN_NAME);
  }

  @Test
  public void test_isPrefixedBy()
  {
    Assert.assertTrue(JoinPrefixUtils.isPrefixedBy("foo", ""));
    Assert.assertTrue(JoinPrefixUtils.isPrefixedBy("foo", "f"));
    Assert.assertTrue(JoinPrefixUtils.isPrefixedBy("foo", "fo"));
    Assert.assertFalse(JoinPrefixUtils.isPrefixedBy("foo", "foo"));
  }
}


text:
=======
Duktape
=======

Duktape is a small and portable ECMAScript E5/E5.1 implementation.  It is
intended to be easily embeddable into C programs, with a C API similar in
spirit to Lua's.

Duktape supports the full E5/E5.1 feature set (with some semantics updated
from ES2015+) including errors, Unicode strings, and regular expressions,
a subset of ECMAScript 2015 (E6) and ECMAScript 2016 (E7) features (e.g.
computed property names, Proxy objects, exponentiation operator, Reflect),
ES2015 ArrayBuffer/TypedView, Node.js Buffer, performance.now(), CBOR, and
WHATWG Encoding API living standard.

Duktape also provides a number of custom features such as error tracebacks,
additional data types for better C integration, combined reference counting
and mark-and sweep garbage collector, object finalizers, co-operative
threads a.k.a. coroutines, tail calls, a built-in debugger protocol, function
bytecode dump/load, and so on.  Bundled extra modules provide functionality
such as CommonJS module loading and a logging framework.

You can browse Duktape programmer's API and other documentation at:

* http://duktape.org/

In particular, you should read the getting started section:

* http://duktape.org/guide.html#gettingstarted

More examples and how-to articles are in the Duktape Wiki:

* http://wiki.duktape.org/

To build an example command line tool with interactive evaluation (REPL) and
the ability to run script files::

  $ cd <dist_root>
  $ make -f Makefile.cmdline
  [...]

  $ ./duk
  ((o) Duktape
  duk> print('Hello world!');
  Hello world!
  = undefined

  $ ./duk mandel.js
  [...]

To integrate Duktape into your program:

* Use ``tools/configure.py`` to prepare Duktape source and header files
  for build::

      # Duktape options can be customized via command line options.
      # In this example, enable "fastint" support and disable ES2015
      # Proxy support

      $ python2 tools/configure.py --output-directory duktape-src \
            -DDUK_USE_FASTINT -UDUK_USE_ES6_PROXY

* The output directory (duktape-src) will then contain ``duktape.c``,
  ``duktape.h``, and ``duk_config.h`` which you include in your build.

For more details, see:

* http://duktape.org/guide.html#compiling

* http://wiki.duktape.org/Configuring.html

This distributable contains:

* ``src/``: main Duktape library in a "single source file" format (duktape.c,
  duktape.h, and duk_config.h; no ``#line`` directives), using Duktape default
  configuration.

* ``src-input/``: raw input source files used by ``configure.py`` for creating
  custom configurations.

* ``tools/``: various Python tools, such as ``configure.py`` for preparing
  a ``duk_config.h`` header and Duktape source files for compilation, see
  http://wiki.duktape.org/Configuring.html.

* ``config/``: configuration metadata for ``configure.py``.

* ``examples/``: further examples for using Duktape.  Although Duktape
  itself is widely portable, some of the examples are Linux only.
  For instance the ``eventloop`` example illustrates how ``setTimeout()``
  and other standard timer functions could be implemented on Unix/Linux.

* ``extras/``: utilities and modules which don't comfortably fit into the
  main Duktape library because of footprint or portability concerns.
  Extras are maintained and bug fixed code, but don't have the same version
  guarantees as the main Duktape library.

* ``polyfills/``: a few replacement suggestions for non-standard Javascript
  functions provided by other implementations.

* ``debugger/``: a debugger with a web UI, see ``debugger/README.rst`` and
  https://github.com/svaarala/duktape/blob/master/doc/debugger.rst for
  details on Duktape debugger support.  Also contains a JSON debug proxy
  (one written in Node.js and another in DukLuv) to make talking to the
  debug target easier.

* ``licenses/``: licensing information.

You can find release notes at:

* https://github.com/svaarala/duktape/blob/master/RELEASES.rst
  (summary of all versions)

* https://github.com/svaarala/duktape/blob/master/doc/release-notes-v@DUK_MAJOR@-@DUK_MINOR@.rst
  (more detailed notes for this version)

This distributable contains Duktape version @DUK_VERSION_FORMATTED@, created from git
commit @GIT_COMMIT@ (@GIT_DESCRIBE@).

Duktape is copyrighted by its authors (see ``AUTHORS.rst``) and licensed
under the MIT license (see ``LICENSE.txt``).  Duktape contains independent
implementations, with no included code, of the following algorithms: Lua
string hash (MIT license), djb2 hash, Murmurhash2 (MIT license), Adi
Shamir's three-op hash algorithm, xoroshiro128+ PRNG (public domain),
SplitMix64 PRNG (public domain), and CommonJS module loading specification
(MIT license).  Duktape tooling relies on js-yaml (MIT license).

Have fun!

Sami Vaarala (sami.vaarala@iki.fi)


text:
    {{ notice.actor }}
    {% if notice.target %}
        在 {{ notice.target }} 中
    {% endif %}
    {{ notice.verb }}

{{ notice.timesince }} 前

{{ notice.description|safe|linebreaksbr }}

    删除
  {% if notice.unread %}
    标记为已读
  {% endif %}


text:
<component name="index">
	<require src="text.xml"/>
	<require src="if.xml"/>
	<require src="for.xml"/>
	<template>
		<link rel="stylesheet" href="base.css"/>

todos

				<text id="new-todo" placeholder="What needs to be done?" autofocus="" model="$newTodo.name" onKeyPress="$create()"/>

				<input id="toggle-all" type="checkbox"/>
				Mark all as complete

				<for items="$todos" as="todo">
				<each>

						<input class="toggle" type="checkbox"/>

						<button class="destroy"></button>

					<input class="edit" data="$todo.name"/>

				</each>
				</for>

			1 item left

					All

					Active

					Completed

Double-click to edit a todo

Written by Yosuke Torii

Part of TodoMVC

	</template>

</component>


text:
#import "TGImageBlur.h"

#import "TGImageUtils.h"

#import <Accelerate/Accelerate.h>

#import "UIImage+TG.h"
#import "TGStaticBackdropImageData.h"

#define TG_USE_NEON 0

#if TG_USE_NEON
#   import <arm_neon.h>
#endif

static inline uint64_t get_colors (const uint8_t *p) {
    return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32);
}

static void fastBlur (int imageWidth, int imageHeight, int imageStride, void *pixels)
{
    uint8_t *pix = (uint8_t *)pixels;
    const int w = imageWidth;
    const int h = imageHeight;
    const int stride = imageStride;
    const int radius = 3;
    const int r1 = radius + 1;
    const int div = radius * 2 + 1;

    if (radius > 15 || div >= w || div >= h)
    {
        return;
    }

    uint64_t rgb[imageStride * imageHeight];

    int x, y, i;

    int yw = 0;
    const int we = w - r1;
    for (y = 0; y < h; y++) {
        uint64_t cur = get_colors (&pix[yw]);
        uint64_t rgballsum = -radius * cur;
        uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);

        for (i = 1; i <= radius; i++) {
            uint64_t cur = get_colors (&pix[yw + i * 4]);
            rgbsum += cur * (r1 - i);
            rgballsum += cur;
        }

        x = 0;

#define update(start, middle, end)                         \
rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FF; \
\
rgballsum += get_colors (&pix[yw + (start) * 4]) -   \
2 * get_colors (&pix[yw + (middle) * 4]) +  \
get_colors (&pix[yw + (end) * 4]);      \
rgbsum += rgballsum;                                 \
x++;                                                 \

        while (x < r1) {
            update (0, x, x + r1);
        }
        while (x < we) {
            update (x - r1, x, x + r1);
        }
        while (x < w) {
            update (x - r1, x, w - 1);
        }
#undef update

        yw += stride;
    }

    const int he = h - r1;
    for (x = 0; x < w; x++) {
        uint64_t rgballsum = -radius * rgb[x];
        uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
        for (i = 1; i <= radius; i++) {
            rgbsum += rgb[i * w + x] * (r1 - i);
            rgballsum += rgb[i * w + x];
        }

        y = 0;
        int yi = x * 4;

#define update(start, middle, end)         \
int64_t res = rgbsum >> 4;           \
pix[yi] = res;                       \
pix[yi + 1] = res >> 16;             \
pix[yi + 2] = res >> 32;             \
\
rgballsum += rgb[x + (start) * w] -  \
2 * rgb[x + (middle) * w] + \
rgb[x + (end) * w];     \
rgbsum += rgballsum;                 \
y++;                                 \
yi += stride;

        while (y < r1) {
            update (0, y, y + r1);
        }
        while (y < he) {
            update (y - r1, y, y + r1);
        }
        while (y < h) {
            update (y - r1, y, h - 1);
        }
#undef update
    }
}

static void fastBlurMore (int imageWidth, int imageHeight, int imageStride, void *pixels)
{
    uint8_t *pix = (uint8_t *)pixels;
    const int w = imageWidth;
    const int h = imageHeight;
    const int stride = imageStride;
    const int radius = 7;
    const int r1 = radius + 1;
    const int div = radius * 2 + 1;

    if (radius > 15 || div >= w || div >= h)
    {
        return;
    }

    uint64_t rgb[imageStride * imageHeight];

    int x, y, i;

    int yw = 0;
    const int we = w - r1;
    for (y = 0; y < h; y++) {
        uint64_t cur = get_colors (&pix[yw]);
        uint64_t rgballsum = -radius * cur;
        uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);

        for (i = 1; i <= radius; i++) {
            uint64_t cur = get_colors (&pix[yw + i * 4]);
            rgbsum += cur * (r1 - i);
            rgballsum += cur;
        }

        x = 0;

#define update(start, middle, end)                         \
rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \
\
rgballsum += get_colors (&pix[yw + (start) * 4]) -   \
2 * get_colors (&pix[yw + (middle) * 4]) +  \
get_colors (&pix[yw + (end) * 4]);      \
rgbsum += rgballsum;                                 \
x++;                                                 \

        while (x < r1) {
            update (0, x, x + r1);
        }
        while (x < we) {
            update (x - r1, x, x + r1);
        }
        while (x < w) {
            update (x - r1, x, w - 1);
        }
#undef update

        yw += stride;
    }

    const int he = h - r1;
    for (x = 0; x < w; x++) {
        uint64_t rgballsum = -radius * rgb[x];
        uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
        for (i = 1; i <= radius; i++) {
            rgbsum += rgb[i * w + x] * (r1 - i);
            rgballsum += rgb[i * w + x];
        }

        y = 0;
        int yi = x * 4;

#define update(start, middle, end)         \
int64_t res = rgbsum >> 6;           \
pix[yi] = res;                       \
pix[yi + 1] = res >> 16;             \
pix[yi + 2] = res >> 32;             \
\
rgballsum += rgb[x + (start) * w] -  \
2 * rgb[x + (middle) * w] + \
rgb[x + (end) * w];     \
rgbsum += rgballsum;                 \
y++;                                 \
yi += stride;

        while (y < r1) {
            update (0, y, y + r1);
        }
        while (y < he) {
            update (y - r1, y, y + r1);
        }
        while (y < h) {
            update (y - r1, y, h - 1);
        }
#undef update
    }
}

static void matrixMul(CGFloat *a, CGFloat *b, CGFloat *result)
{
	for (int i = 0; i != 4; ++i)
	{
 		for (int j = 0; j != 4; ++j)
		{
			CGFloat sum = 0;
			for (int k = 0; k != 4; ++k)
			{
				sum += a[i + k * 4] * b[k + j * 4];
			}
			result[i + j * 4] = sum;
		}
	}
}

static void matrixVectorMul(CGFloat *matrix, CGFloat *vector, CGFloat *result)
{
    for (int i=0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            result[i] += (matrix[i * 4 + j] * vector[j]);
        }
    }
}

static inline CGSize fitSize(CGSize size, CGSize maxSize)
{
    if (size.width < 1)
        size.width = 1;
    if (size.height < 1)
        size.height = 1;

    if (size.width > maxSize.width)
    {
        size.height = CGFloor((size.height * maxSize.width / size.width));
        size.width = maxSize.width;
    }
    if (size.height > maxSize.height)
    {
        size.width = CGFloor((size.width * maxSize.height / size.height));
        size.height = maxSize.height;
    }
    return size;
}

static void computeImageVariance(uint8_t *memory, int width, int height, int stride, float *outVariance, float *outLuminance, float *outRealLuminance)
{
    uint32_t rnSum = 0;
    uint32_t gnSum = 0;
    uint32_t bnSum = 0;

    uint64_t rnSumSq = 0;
    uint64_t gnSumSq = 0;
    uint64_t bnSumSq = 0;

    uint32_t luminanceSum = 0;
    //uint64_t luminanceSumSq = 0;

    /*float rSum = 0.0f;
    float gSum = 0.0f;
    float bSum = 0.0f;

    float rSumSq = 0.0f;
    float gSumSq = 0.0f;
    float bSumSq = 0.0f;*/

    uint32_t histogram[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            uint32_t color = *((uint32_t *)&memory[y * stride + x * 4]);

            uint32_t r = (color >> 16) & 0xff;
            uint32_t g = (color >> 8) & 0xff;
            uint32_t b = color & 0xff;

            uint32_t pixelLuminance = (uint8_t)((r * 299 + g * 587 + b * 114) / 1000);
            histogram[(pixelLuminance * 9 / 255) % 10]++;

            luminanceSum += pixelLuminance;
            //luminanceSumSq += pixelLuminance * pixelLuminance;

            rnSum += r;
            gnSum += g;
            bnSum += b;

            rnSumSq += r * r;
            gnSumSq += g * g;
            bnSumSq += b * b;

            /*rSum += r / 255.0f;
            gSum += g / 255.0f;
            bSum += b / 255.0f;

            rSumSq += (r / 255.0f) * (r / 255.0f);
            gSumSq += (g / 255.0f) * (g / 255.0f);
            bSumSq += (b / 255.0f) * (b / 255.0f);*/
        }
    }

    int n = width * height;

    /*float rVariance = (rSumSq - (rSum * rSum) / n) / (n);
    float gVariance = (gSumSq - (gSum * gSum) / n) / (n);
    float bVariance = (bSumSq - (bSum * bSum) / n) / (n);*/

    float rnVariance = ((uint64_t)((rnSumSq / 255) - ((uint64_t)rnSum * (uint64_t)rnSum / 255) / n)) / (255.0f * n);
    float gnVariance = ((uint64_t)((gnSumSq / 255) - ((uint64_t)gnSum * (uint64_t)gnSum / 255) / n)) / (255.0f * n);
    float bnVariance = ((uint64_t)((bnSumSq / 255) - ((uint64_t)bnSum * (uint64_t)bnSum / 255) / n)) / (255.0f * n);

    float variance = rnVariance + gnVariance + bnVariance;
    if (outVariance != NULL)
        *outVariance = variance;

    //float luminanceVariance = ((uint64_t)((luminanceSumSq / 255) - ((uint64_t)luminanceSum * (uint64_t)luminanceSum / 255) / n)) / (255.0f * n);

    float floatHistogram[10];

    float norm = (float)(width * height);

    float n0 = 0.0f;
    float n1 = 0.0f;
    for (int i = 0; i < 10; i++)
    {
        floatHistogram[i] = histogram[i] / norm;

        if (i <= 6)
            n0 += floatHistogram[i];
        else
            n1 += floatHistogram[i];
    }

    //TGLog(@"histogram: [%f %f %f %f %f %f %f %f %f %f]", floatHistogram[0], floatHistogram[1], floatHistogram[2], floatHistogram[3], floatHistogram[4], floatHistogram[5], floatHistogram[6], floatHistogram[7], floatHistogram[8], floatHistogram[9]);

    if (outLuminance != NULL)
        *outLuminance = n0 < n1 ? 0.95f : 0.5f;

    if (outRealLuminance != NULL)
        *outRealLuminance = (luminanceSum / (norm * 255.0f));
}

static void fastScaleImage(uint8_t *sourceMemory, int sourceWidth, int sourceHeight, int sourceStride, uint8_t *targetMemory, int targetWidth, int targetHeight, int targetStride, CGRect sourceRectInTargetSpace)
{
    int imageX = MIN(0, (int)sourceRectInTargetSpace.origin.x);
    int imageY = MIN(0, (int)sourceRectInTargetSpace.origin.y);
    int imageWidth = (int)sourceRectInTargetSpace.size.width;
    int imageHeight = (int)sourceRectInTargetSpace.size.height;

    for (int y = 0; y < targetHeight; y++)
    {
        for (int x = 0; x < targetWidth; x++)
        {
            int sourceY = (y - imageY) * sourceHeight / imageHeight;
            int sourceX = (x - imageX) * sourceWidth / imageWidth;

            if (sourceX >= 0 && sourceY >= 0 && sourceX < sourceWidth && sourceY < sourceHeight)
            {
                uint32_t color = *((uint32_t *)&sourceMemory[sourceY * sourceStride + sourceX * 4]);
                *((uint32_t *)&targetMemory[y * targetStride + x * 4]) = color;
            }
        }
    }
}

static uint32_t TGImageAverageColor(void *memory, const unsigned int width, const unsigned int height, const unsigned int stride)
{
    int32_t av0 = 0;
    int32_t av1 = 0;
    int32_t av2 = 0;

    for (unsigned int y = 0; y < height; y++)
    {
        for (unsigned int x = 0; x < width; x++)
        {
            uint32_t pixel = *((uint32_t *)(&memory[y * stride + x * 4]));
            av0 += pixel & 0xff;
            av1 += (pixel >> 8) & 0xff;
            av2 += (pixel >> 16) & 0xff;
        }
    }

    uint32_t norm = (width * height);
    av0 = av0 / norm;
    av1 = av1 / norm;
    av2 = av2 / norm;

    return 0xff000000 | av0 | (av1 << 8) | (av2 << 16);
}

static inline uint32_t alphaComposePremultipliedPixels(uint32_t a, uint32_t b)
{
    uint32_t a0 = ((a >> 24) & 0xff);
    uint32_t a1 = ((b >> 24) & 0xff);

    uint32_t r0 = (a >> 16) & 0xff;
    uint32_t g0 = (a >> 8) & 0xff;
    uint32_t b0 = a & 0xff;

    uint32_t r1 = (b >> 16) & 0xff;
    uint32_t g1 = (b >> 8) & 0xff;
    uint32_t b1 = b & 0xff;

    uint32_t ta = ((a0 * a0) >> 8) + ((a1 * (255 - ((a0 * a0) >> 8))) >> 8);
    uint32_t tr = ((r0 * a0) >> 8) + ((r1 * (255 - ((a0 * a0) >> 8))) >> 8);
    uint32_t tg = ((g0 * a0) >> 8) + ((g1 * (255 - ((a0 * a0) >> 8))) >> 8);
    uint32_t tb = ((b0 * a0) >> 8) + ((b1 * (255 - ((a0 * a0) >> 8))) >> 8);

    return (ta << 24) | (tr << 16) | (tg << 8) | tb;
}

static inline uint32_t premultipliedPixel(uint32_t rgb, uint32_t alpha)
{
    uint32_t r = (((rgb >> 16) & 0xff) * alpha) >> 8;
    uint32_t g = (((rgb >> 8) & 0xff) * alpha) >> 8;
    uint32_t b = ((rgb & 0xff) * alpha) >> 8;

    return (alpha << 24) | (r << 16) | (g << 8) | b;
}

static void addAttachmentImageCorners(void *memory, const unsigned int width, const unsigned int height, const unsigned int stride)
{
    const int scale = TGIsRetina() ? 2 : 1;

    const int shadowSize = 1;
    const int strokeWidth = scale >= 2 ? 3 : 2;
    const int radius = 13 * scale;

    const int contextWidth = radius * 2 + shadowSize * 2 + strokeWidth * 2;
    const int contextHeight = radius * 2 + shadowSize * 2 + strokeWidth * 2;
    const int contextStride = (4 * contextWidth + 15) & (~15);

    static uint8_t *contextMemory = NULL;
    static uint8_t *alphaMemory = NULL;

    const uint32_t shadowColorRaw = 0x44000000;
    const uint32_t shadowColorArgb = premultipliedPixel(shadowColorRaw & 0xffffff, ((shadowColorRaw >> 24) & 0xff) - 30);
    static uint32_t strokeColorArgb = 0xffffffff;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        contextMemory = malloc(contextStride * contextHeight);
        memset(contextMemory, 0, contextStride * contextHeight);

        alphaMemory = malloc(contextStride * contextHeight);

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
        CGContextRef targetContext = CGBitmapContextCreate(contextMemory, contextWidth, contextHeight, 8, contextStride, colorSpace, bitmapInfo);
        CFRelease(colorSpace);

        CGContextSetFillColorWithColor(targetContext, [UIColor blackColor].CGColor);
        CGContextFillEllipseInRect(targetContext, CGRectMake(shadowSize + strokeWidth / 2.0f, shadowSize + strokeWidth / 2.0f, contextWidth - (shadowSize + strokeWidth / 2.0f) * 2.0f, contextHeight - (shadowSize + strokeWidth / 2.0f) * 2.0f));

        memcpy(alphaMemory, contextMemory, contextStride * contextHeight);

        memset(contextMemory, 0, contextStride * contextHeight);

        CGContextSetStrokeColorWithColor(targetContext, UIColorRGBA(shadowColorRaw, ((shadowColorRaw >> 24) & 0xff) / 255.0f).CGColor);
        CGContextSetLineWidth(targetContext, shadowSize);
        CGContextStrokeEllipseInRect(targetContext, CGRectMake(shadowSize / 2.0f, shadowSize / 2.0f, contextWidth - shadowSize, contextHeight - shadowSize));
        CGContextStrokeEllipseInRect(targetContext, CGRectMake(shadowSize / 2.0f + 0.5f, shadowSize / 2.0f - 0.5f, contextWidth - shadowSize, contextHeight - shadowSize));

        CGContextSetStrokeColorWithColor(targetContext, UIColorRGBA(shadowColorRaw, (((shadowColorRaw >> 24) & 0xff) / 255.0f) * 0.5f).CGColor);
        CGContextStrokeEllipseInRect(targetContext, CGRectMake(shadowSize / 2.0f - 0.2f, shadowSize / 2.0f + 0.2f, contextWidth - shadowSize, contextHeight - shadowSize));

        CGContextSetStrokeColorWithColor(targetContext, UIColorRGB(strokeColorArgb).CGColor);
        CGContextSetLineWidth(targetContext, strokeWidth);
        CGContextStrokeEllipseInRect(targetContext, CGRectMake(shadowSize + strokeWidth / 2.0f, shadowSize + strokeWidth / 2.0f, contextWidth - (shadowSize + strokeWidth / 2.0f) * 2.0f, contextHeight - (shadowSize + strokeWidth / 2.0f) * 2.0f));

        CGContextSetStrokeColorWithColor(targetContext, UIColorRGBA(strokeColorArgb, 0.4f).CGColor);
        CGContextStrokeEllipseInRect(targetContext, CGRectMake(shadowSize + strokeWidth / 2.0f + 0.5f, shadowSize + strokeWidth / 2.0f - 0.5f, contextWidth - (shadowSize + strokeWidth / 2.0f) * 2.0f, contextHeight - (shadowSize + strokeWidth / 2.0f) * 2.0f));

        CFRelease(targetContext);
    });

    const unsigned int radiusWithPadding = contextWidth / 2;
    const unsigned int rightRadius = width - radiusWithPadding;

    for (unsigned int y = 0; y < radiusWithPadding; y++)
    {
        for (unsigned int x = 0; x < radiusWithPadding; x++)
        {
            uint32_t alpha = alphaMemory[y * contextStride + x * 4 + 3];
            uint32_t pixel = *((uint32_t *)(&memory[y * stride + x * 4]));

            pixel = (alpha << 24) | (((((pixel >> 16) & 0xff) * alpha) >> 8) << 16) | (((((pixel >> 8) & 0xff) * alpha) >> 8) << 8) | (((((pixel >> 0) & 0xff) * alpha) >> 8) << 0);
            pixel = alphaComposePremultipliedPixels(*((uint32_t *)&contextMemory[y * contextStride + x * 4]), pixel);
            *((uint32_t *)(&memory[y * stride + x * 4])) = pixel;
        }
    }

    for (int y = 0; y < shadowSize; y++)
    {
        for (unsigned int x = radiusWithPadding; x < rightRadius; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = shadowColorArgb;
        }
    }

    for (int y = shadowSize; y < shadowSize + strokeWidth; y++)
    {
        for (unsigned int x = radiusWithPadding; x < rightRadius; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = strokeColorArgb;
        }
    }

    for (unsigned int y = 0; y < radiusWithPadding; y++)
    {
        for (unsigned int x = rightRadius; x < width; x++)
        {
            uint32_t alpha = alphaMemory[y * contextStride + (width - 1 - x) * 4 + 3];
            uint32_t pixel = *((uint32_t *)(&memory[y * stride + x * 4]));

            pixel = (alpha << 24) | (((((pixel >> 16) & 0xff) * alpha) >> 8) << 16) | (((((pixel >> 8) & 0xff) * alpha) >> 8) << 8) | (((((pixel >> 0) & 0xff) * alpha) >> 8) << 0);
            pixel = alphaComposePremultipliedPixels(*((uint32_t *)&contextMemory[y * contextStride + (width - 1 - x) * 4]), pixel);
            *((uint32_t *)(&memory[y * stride + x * 4])) = pixel;
        }
    }

    for (unsigned int y = radiusWithPadding; y < height - radiusWithPadding; y++)
    {
        for (int x = 0; x < shadowSize; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = shadowColorArgb;
        }

        for (int x = shadowSize; x < shadowSize + strokeWidth; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = strokeColorArgb;
        }

        for (unsigned int x = width - shadowSize - strokeWidth; x < width - shadowSize; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = strokeColorArgb;
        }

        for (unsigned int x = width - shadowSize; x < width; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = shadowColorArgb;
        }
    }

    for (unsigned int y = height - radiusWithPadding; y < height; y++)
    {
        for (unsigned int x = 0; x < radiusWithPadding; x++)
        {
            uint32_t alpha = alphaMemory[(height - 1 - y) * contextStride + x * 4 + 3];
            uint32_t pixel = *((uint32_t *)(&memory[y * stride + x * 4]));

            pixel = (alpha << 24) | (((((pixel >> 16) & 0xff) * alpha) >> 8) << 16) | (((((pixel >> 8) & 0xff) * alpha) >> 8) << 8) | (((((pixel >> 0) & 0xff) * alpha) >> 8) << 0);
            pixel = alphaComposePremultipliedPixels(*((uint32_t *)&contextMemory[(height - 1 - y) * contextStride + x * 4]), pixel);
            *((uint32_t *)(&memory[y * stride + x * 4])) = pixel;
        }
    }

    for (unsigned int y = height - shadowSize - strokeWidth; y < height - shadowSize; y++)
    {
        for (unsigned int x = radiusWithPadding; x < rightRadius; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = strokeColorArgb;
        }
    }

    for (unsigned int y = height - shadowSize; y < height; y++)
    {
        for (unsigned int x = radiusWithPadding; x < rightRadius; x++)
        {
            *((uint32_t *)(&memory[y * stride + x * 4])) = shadowColorArgb;
        }
    }

    for (unsigned int y = height - radiusWithPadding; y < height; y++)
    {
        for (unsigned int x = width - radiusWithPadding; x < width; x++)
        {
            uint32_t alpha = alphaMemory[(height - 1 - y) * contextStride + (width - 1 - x) * 4 + 3];
            uint32_t pixel = *((uint32_t *)(&memory[y * stride + x * 4]));

            pixel = (alpha << 24) | (((((pixel >> 16) & 0xff) * alpha) >> 8) << 16) | (((((pixel >> 8) & 0xff) * alpha) >> 8) << 8) | (((((pixel >> 0) & 0xff) * alpha) >> 8) << 0);
            pixel = alphaComposePremultipliedPixels(*((uint32_t *)&contextMemory[(height - 1 - y) * contextStride + (width - 1 - x) * 4]), pixel);
            *((uint32_t *)(&memory[y * stride + x * 4])) = pixel;
        }
    }
}

static int16_t *brightenMatrix(int32_t *outDivisor)
{
    static int16_t saturationMatrix[16];
    static const int32_t divisor = 256;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        CGFloat s = 2.6f;
        CGFloat offset = 0.02f;
        CGFloat factor = 1.3f;
        CGFloat satMatrix[] = {
            0.0722f + 0.9278f * s,  0.0722f - 0.0722f * s,  0.0722f - 0.0722f * s,  0,
            0.7152f - 0.7152f * s,  0.7152f + 0.2848f * s,  0.7152f - 0.7152f * s,  0,
            0.2126f - 0.2126f * s,  0.2126f - 0.2126f * s,  0.2126f + 0.7873f * s,  0,
            0.0f,                    0.0f,                    0.0f,  1,
        };
        CGFloat contrastMatrix[] = {
            factor, 0.0f, 0.0f, 0.0f,
            0.0f, factor, 0.0f, 0.0f,
            0.0f, 0.0f, factor, 0.0f,
            offset, offset, offset, 1.0f
        };
        CGFloat colorMatrix[16];
        matrixMul(satMatrix, contrastMatrix, colorMatrix);

        NSUInteger matrixSize = sizeof(colorMatrix) / sizeof(colorMatrix[0]);
        for (NSUInteger i = 0; i < matrixSize; ++i) {
            saturationMatrix[i] = (int16_t)roundf(colorMatrix[i] * divisor);
        }
    });

    if (outDivisor != NULL)
        *outDivisor = divisor;

    return saturationMatrix;
}

static int16_t *secretMatrix(int32_t *outDivisor)
{
    static int16_t saturationMatrix[16];
    static const int32_t divisor = 256;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        CGFloat s = 1.6f;
        CGFloat offset = 0.0f;
        CGFloat factor = 1.3f;
        CGFloat satMatrix[] = {
            0.0722f + 0.9278f * s,  0.0722f - 0.0722f * s,  0.0722f - 0.0722f * s,  0,
            0.7152f - 0.7152f * s,  0.7152f + 0.2848f * s,  0.7152f - 0.7152f * s,  0,
            0.2126f - 0.2126f * s,  0.2126f - 0.2126f * s,  0.2126f + 0.7873f * s,  0,
            0.0f,                    0.0f,                    0.0f,  1,
        };
        CGFloat contrastMatrix[] = {
            factor, 0.0f, 0.0f, 0.0f,
            0.0f, factor, 0.0f, 0.0f,
            0.0f, 0.0f, factor, 0.0f,
            offset, offset, offset, 1.0f
        };
        CGFloat colorMatrix[16];
        matrixMul(satMatrix, contrastMatrix, colorMatrix);

        NSUInteger matrixSize = sizeof(colorMatrix) / sizeof(colorMatrix[0]);
        for (NSUInteger i = 0; i < matrixSize; ++i) {
            saturationMatrix[i] = (int16_t)roundf(colorMatrix[i] * divisor);
        }
    });

    if (outDivisor != NULL)
        *outDivisor = divisor;

    return saturationMatrix;
}

static int16_t *brightenTimestampMatrix(int32_t *outDivisor)
{
    static int16_t saturationMatrix[16];
    static const int32_t divisor = 256;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        CGFloat s = 1.1f;
        CGFloat offset = 0.1f;
        CGFloat factor = 1.2f;
        CGFloat satMatrix[] = {
            0.0722f + 0.9278f * s,  0.0722f - 0.0722f * s,  0.0722f - 0.0722f * s,  0,
            0.7152f - 0.7152f * s,  0.7152f + 0.2848f * s,  0.7152f - 0.7152f * s,  0,
            0.2126f - 0.2126f * s,  0.2126f - 0.2126f * s,  0.2126f + 0.7873f * s,  0,
            0.0f,                    0.0f,                    0.0f,  1,
        };
        CGFloat contrastMatrix[] = {
            factor, 0.0f, 0.0f, 0.0f,
            0.0f, factor, 0.0f, 0.0f,
            0.0f, 0.0f, factor, 0.0f,
            offset, offset, offset, 1.0f
        };
        CGFloat colorMatrix[16];
        matrixMul(satMatrix, contrastMatrix, colorMatrix);

        NSUInteger matrixSize = sizeof(colorMatrix) / sizeof(colorMatrix[0]);
        for (NSUInteger i = 0; i < matrixSize; ++i) {
            saturationMatrix[i] = (int16_t)roundf(colorMatrix[i] * divisor);
        }
    });

    if (outDivisor != NULL)
        *outDivisor = divisor;

    return saturationMatrix;
}

static int16_t *darkenTimestampMatrix(int32_t *outDivisor)
{
    static int16_t saturationMatrix[16];
    static const int32_t divisor = 256;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        CGFloat s = 1.7f;
        CGFloat offset = 0.0f;
        CGFloat factor = 1.1f;
        CGFloat satMatrix[] = {
            0.0722f + 0.9278f * s,  0.0722f - 0.0722f * s,  0.0722f - 0.0722f * s,  0,
            0.7152f - 0.7152f * s,  0.7152f + 0.2848f * s,  0.7152f - 0.7152f * s,  0,
            0.2126f - 0.2126f * s,  0.2126f - 0.2126f * s,  0.2126f + 0.7873f * s,  0,
            0.0f,                    0.0f,                    0.0f,  1,
        };
        CGFloat contrastMatrix[] = {
            factor, 0.0f, 0.0f, 0.0f,
            0.0f, factor, 0.0f, 0.0f,
            0.0f, 0.0f, factor, 0.0f,
            offset, offset, offset, 1.0f
        };
        CGFloat colorMatrix[16];
        matrixMul(satMatrix, contrastMatrix, colorMatrix);

        NSUInteger matrixSize = sizeof(colorMatrix) / sizeof(colorMatrix[0]);
        for (NSUInteger i = 0; i < matrixSize; ++i) {
            saturationMatrix[i] = (int16_t)roundf(colorMatrix[i] * divisor);
        }
    });

    if (outDivisor != NULL)
        *outDivisor = divisor;

    return saturationMatrix;
}

UIImage *TGAverageColorImage(UIColor *color)
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(1.0f, 1.0f), true, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0.0f, 0.0f, 1.0f, 1.0f));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

UIImage *TGAverageColorRoundImage(UIColor *color, CGSize size)
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), true, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0.0f, 0.0f, 1.0f, 1.0f));
    CGContextFillEllipseInRect(context, CGRectMake(0.0f, 0.0f, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

UIImage *TGAverageColorAttachmentImage(UIColor *color)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize size = CGSizeMake(36.0f, 36.0f);

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale)};

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, scale, scale);

    CGColorSpaceRelease(colorSpace);

    CGContextSetBlendMode(targetContext, kCGBlendModeCopy);

    CGContextSetFillColorWithColor(targetContext, [color CGColor]);
    CGContextFillRect(targetContext, CGRectMake(0.0f, 0.0f, targetContextSize.width, targetContextSize.height));

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    UIGraphicsPopContext();

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp] stretchableImageWithLeftCapWidth:(int)(targetContextSize.width / scale / 2) topCapHeight:(int)(targetContextSize.height / scale / 2)];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    /*CGFloat matrix[16];
    int32_t divisor = 256;
    int16_t *integerMatrix = brightenMatrix(&divisor);

    for (int i = 0; i < 16; i++)
    {
        matrix[i] = integerMatrix[i] / (CGFloat)divisor;
    }

    CGFloat vector[4];
    [color getRed:&vector[3] green:&vector[2] blue:&vector[1] alpha:&vector[0]];

    CGFloat resultColor[4];
    matrixVectorMul(matrix, vector, resultColor);

    UIImage *genericCircleImage = nil;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(50.0f, 50.0f), false, scale);
    CGContextRef circleContext = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(circleContext, [[UIColor alloc] initWithRed:vector[3] green:vector[2] blue:vector[1] alpha:vector[0]].CGColor);
    CGContextFillEllipseInRect(circleContext, CGRectMake(0.0f, 0.0f, 50.0f, 50.0f));
    genericCircleImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [image setActionCircleImage:genericCircleImage];*/

    return image;
}

static void modifyAndBlurImage(void *pixels, unsigned int width, unsigned int height, unsigned int stride, bool strongBlur, int16_t *matrix)
{
    unsigned int tempWidth = width / 6;
    unsigned int tempHeight = height / 6;
    unsigned int tempStride = ((4 * tempWidth + 15) & (~15));
    void *tempPixels = malloc(tempStride * tempHeight);

    vImage_Buffer srcBuffer;
    srcBuffer.width = width;
    srcBuffer.height = height;
    srcBuffer.rowBytes = stride;
    srcBuffer.data = pixels;

    vImage_Buffer dstBuffer;
    dstBuffer.width = tempWidth;
    dstBuffer.height = tempHeight;
    dstBuffer.rowBytes = tempStride;
    dstBuffer.data = tempPixels;

    vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageDoNotTile);

    fastBlurMore(tempWidth, tempHeight, tempStride, tempPixels);
    if (strongBlur)
        fastBlur(tempWidth, tempHeight, tempStride, tempPixels);

    int32_t divisor = 256;
    vImageMatrixMultiply_ARGB8888(&dstBuffer, &dstBuffer, matrix, divisor, NULL, NULL, kvImageDoNotTile);
    vImageScale_ARGB8888(&dstBuffer, &srcBuffer, NULL, kvImageDoNotTile);

    free(tempPixels);
}

static void brightenAndBlurImage(void *pixels, unsigned int width, unsigned int height, unsigned int stride, bool strongBlur)
{
    modifyAndBlurImage(pixels, width, height, stride, strongBlur, brightenMatrix(NULL));
}

TGStaticBackdropAreaData *createImageBackdropArea(uint8_t *sourceImageMemory, int sourceImageWidth, int sourceImageHeight, int sourceImageStride, CGSize originalSize, CGRect sourceImageRect)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    const struct { int width, height; } contextSize = { (int)(sourceImageRect.size.width / 2), (int)(sourceImageRect.size.height / 2) };
    size_t bytesPerRow = ((4 * (int)contextSize.width) + 15) & (~15);

    CGFloat scalingFactor = contextSize.width / sourceImageRect.size.width;

    void *memory = malloc((int)(bytesPerRow * contextSize.height));
    memset(memory, 0x00, (int)(bytesPerRow * contextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
    CGContextRef context = CGBitmapContextCreate(memory, (int)contextSize.width, (int)contextSize.height, 8, bytesPerRow, colorSpace, bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsPushContext(context);

    CGContextTranslateCTM(context, contextSize.width / 2.0f, contextSize.height / 2.0f);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    CGContextTranslateCTM(context, -contextSize.width / 2.0f, -contextSize.height / 2.0f);

    CGRect imageRect = CGRectMake(-sourceImageRect.origin.x * scalingFactor, -sourceImageRect.origin.y * scalingFactor, originalSize.width * scalingFactor, originalSize.height * scalingFactor);

    float luminance = 0.0f;
    float realLuminance = 0.0f;
    float variance = 0.0f;

    if (sourceImageMemory != NULL)
    {
        fastScaleImage(sourceImageMemory, sourceImageWidth, sourceImageHeight, sourceImageStride, memory, contextSize.width, contextSize.height, bytesPerRow, imageRect);
    }

    /*if (luminance > 0.8f)
        modifyAndBlurImage(memory, contextSize.width, contextSize.height, bytesPerRow, false, brightenTimestampMatrix(NULL));
    else
        modifyAndBlurImage(memory, contextSize.width, contextSize.height, bytesPerRow, false, darkenTimestampMatrix(NULL));*/

    fastBlur(contextSize.width, contextSize.height, bytesPerRow, memory);
    fastBlur(contextSize.width, contextSize.height, bytesPerRow, memory);
    computeImageVariance(memory, contextSize.width, contextSize.height, bytesPerRow, &variance, &luminance, &realLuminance);

    if ((variance >= 0.009f && realLuminance > 0.7f) || variance >= 0.05f)
    {
        uint32_t color = TGImageAverageColor(memory, contextSize.width, contextSize.height, bytesPerRow);
        //color = 0xff00ffff;
        CGContextSetFillColorWithColor(context, UIColorRGBA(color, 0.7f).CGColor);
        CGContextFillRect(context, CGRectMake(0, 0, contextSize.width, contextSize.height));

        uint32_t r = (color >> 16) & 0xff;
        uint32_t g = (color >> 8) & 0xff;
        uint32_t b = color & 0xff;

        uint32_t pixelLuminance = (uint8_t)((r * 299 + g * 587 + b * 114) / 1000);
        luminance = pixelLuminance / 255.0f;
        if (luminance < 0.85f)
            luminance = 0.0f;
    }

    CGImageRef bitmapImage = CGBitmapContextCreateImage(context);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    UIGraphicsPopContext();
    CFRelease(context);
    free(memory);

    TGStaticBackdropAreaData *backdropArea = [[TGStaticBackdropAreaData alloc] initWithBackground:image mappedRect:CGRectMake(sourceImageRect.origin.x / originalSize.width, sourceImageRect.origin.y / originalSize.height, sourceImageRect.size.width / originalSize.width, sourceImageRect.size.height / originalSize.height)];
    backdropArea.luminance = luminance;

    return backdropArea;
}

TGStaticBackdropAreaData *createTimestampBackdropArea(uint8_t *sourceImageMemory, int sourceImageWidth, int sourceImageHeight, int sourceImageStride, CGSize originalSize)
{
    const int extraRadius = 0.0f;
    const struct { int width, height; } unscaledSize = { 84 + extraRadius * 2, 18 };
    const struct { int right, bottom; } padding = { 6 - extraRadius, 6 };

    return createImageBackdropArea(sourceImageMemory, sourceImageWidth, sourceImageHeight, sourceImageStride, originalSize, CGRectMake(originalSize.width - padding.right - unscaledSize.width, originalSize.height - padding.bottom - unscaledSize.height, unscaledSize.width, unscaledSize.height));
}

TGStaticBackdropAreaData *createAdditionalDataBackdropArea(uint8_t *sourceImageMemory, int sourceImageWidth, int sourceImageHeight, int sourceImageStride, CGSize originalSize)
{
    const int extraRadius = 0.0f;
    const struct { int width, height; } unscaledSize = { 160 + extraRadius * 2, 18 };
    const struct { int left, top; } padding = { 6 - extraRadius, 6 };

    return createImageBackdropArea(sourceImageMemory, sourceImageWidth, sourceImageHeight, sourceImageStride, originalSize, CGRectMake(padding.left, padding.top, unscaledSize.width, unscaledSize.height));
}

UIImage *TGBlurredAttachmentImage(UIImage *source, CGSize size, uint32_t *averageColor)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize fittedSize = fitSize(size, CGSizeMake(90, 90));

    CGFloat actionCircleDiameter = 50.0f;

    const struct { int width, height; } blurredContextSize = { (int)fittedSize.width, (int)fittedSize.height };
    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale)};
    const struct { int width, height; } actionCircleContextSize = { (int)(actionCircleDiameter * scale), (int)(actionCircleDiameter * scale) };

    size_t blurredBytesPerRow = ((4 * (int)blurredContextSize.width) + 15) & (~15);
    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);
    size_t actionCircleBytesPerRow = ((4 * (int)actionCircleContextSize.width) + 15) & (~15);

    void *blurredMemory = malloc((int)(blurredBytesPerRow * blurredContextSize.height));
    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    void *actionCircleMemory = malloc(((int)(actionCircleBytesPerRow * actionCircleContextSize.height)));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef blurredContext = CGBitmapContextCreate(blurredMemory, (int)blurredContextSize.width, (int)blurredContextSize.height, 8, blurredBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef actionCircleContext = CGBitmapContextCreate(actionCircleMemory, (int)actionCircleContextSize.width, (int)actionCircleContextSize.height, 8, actionCircleBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(blurredContext);
    CGContextTranslateCTM(blurredContext, blurredContextSize.width / 2.0f, blurredContextSize.height / 2.0f);
    CGContextScaleCTM(blurredContext, 1.0f, -1.0f);
    CGContextTranslateCTM(blurredContext, -blurredContextSize.width / 2.0f, -blurredContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(blurredContext, kCGInterpolationLow);
    [source drawInRect:CGRectMake(0, 0, blurredContextSize.width, blurredContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    fastBlur((int)blurredContextSize.width, (int)blurredContextSize.height, blurredBytesPerRow, blurredMemory);

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(blurredMemory, blurredContextSize.width, blurredContextSize.height, blurredBytesPerRow);

    vImage_Buffer srcBuffer;
    srcBuffer.width = blurredContextSize.width;
    srcBuffer.height = blurredContextSize.height;
    srcBuffer.rowBytes = blurredBytesPerRow;
    srcBuffer.data = blurredMemory;

    vImage_Buffer dstBuffer;
    dstBuffer.width = targetContextSize.width;
    dstBuffer.height = targetContextSize.height;
    dstBuffer.rowBytes = targetBytesPerRow;
    dstBuffer.data = targetMemory;

    vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageDoNotTile);

    CGContextRelease(blurredContext);
    free(blurredMemory);

    UIGraphicsPushContext(actionCircleContext);
    CGContextTranslateCTM(actionCircleContext, actionCircleContextSize.width / 2.0f, actionCircleContextSize.height / 2.0f);
    CGContextScaleCTM(actionCircleContext, 1.0f, -1.0f);
    CGContextTranslateCTM(actionCircleContext, -actionCircleContextSize.width / 2.0f, -actionCircleContextSize.height / 2.0f);

    CGContextSetInterpolationQuality(actionCircleContext, kCGInterpolationLow);
    CGContextSetBlendMode(actionCircleContext, kCGBlendModeCopy);

    [source drawInRect:CGRectMake((actionCircleContextSize.width - targetContextSize.width) / 2.0f, (actionCircleContextSize.height - targetContextSize.height) / 2.0f, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    brightenAndBlurImage(actionCircleMemory, actionCircleContextSize.width, actionCircleContextSize.height, actionCircleBytesPerRow, scale < 2);

    CGContextBeginPath(actionCircleContext);
    CGContextAddRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextAddEllipseInRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextClosePath(actionCircleContext);

    CGContextSetFillColorWithColor(actionCircleContext, [UIColor clearColor].CGColor);
    CGContextEOFillPath(actionCircleContext);

    CGImageRef actionCircleBitmapImage = CGBitmapContextCreateImage(actionCircleContext);
    UIImage *actionCircleImage = [[UIImage alloc] initWithCGImage:actionCircleBitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(actionCircleBitmapImage);

    CGContextRelease(actionCircleContext);
    free(actionCircleMemory);

    TGStaticBackdropAreaData *timestampBackdropArea = createTimestampBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));
    TGStaticBackdropAreaData *additionalDataBackdropArea = createAdditionalDataBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    TGStaticBackdropImageData *backdropData = [[TGStaticBackdropImageData alloc] init];
    [backdropData setBackdropArea:[[TGStaticBackdropAreaData alloc] initWithBackground:actionCircleImage] forKey:TGStaticBackdropMessageActionCircle];

    [backdropData setBackdropArea:timestampBackdropArea forKey:TGStaticBackdropMessageTimestamp];
    [backdropData setBackdropArea:additionalDataBackdropArea forKey:TGStaticBackdropMessageAdditionalData];

    [image setStaticBackdropImageData:backdropData];

    return image;
}

UIImage *TGSecretBlurredAttachmentImage(UIImage *source, CGSize size, uint32_t *averageColor)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize fittedSize = fitSize(size, CGSizeMake(40, 40));

    CGFloat actionCircleDiameter = 50.0f;

    const struct { int width, height; } blurredContextSize = { (int)fittedSize.width, (int)fittedSize.height };
    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale)};
    const struct { int width, height; } actionCircleContextSize = { (int)(actionCircleDiameter * scale), (int)(actionCircleDiameter * scale) };

    size_t blurredBytesPerRow = ((4 * (int)blurredContextSize.width) + 15) & (~15);
    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);
    size_t actionCircleBytesPerRow = ((4 * (int)actionCircleContextSize.width) + 15) & (~15);

    void *blurredMemory = malloc((int)(blurredBytesPerRow * blurredContextSize.height));
    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    void *actionCircleMemory = malloc(((int)(actionCircleBytesPerRow * actionCircleContextSize.height)));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef blurredContext = CGBitmapContextCreate(blurredMemory, (int)blurredContextSize.width, (int)blurredContextSize.height, 8, blurredBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef actionCircleContext = CGBitmapContextCreate(actionCircleMemory, (int)actionCircleContextSize.width, (int)actionCircleContextSize.height, 8, actionCircleBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(blurredContext);
    CGContextTranslateCTM(blurredContext, blurredContextSize.width / 2.0f, blurredContextSize.height / 2.0f);
    CGContextScaleCTM(blurredContext, 1.0f, -1.0f);
    CGContextTranslateCTM(blurredContext, -blurredContextSize.width / 2.0f, -blurredContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(blurredContext, kCGInterpolationLow);
    [source drawInRect:CGRectMake(0, 0, blurredContextSize.width, blurredContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    fastBlurMore((int)blurredContextSize.width, (int)blurredContextSize.height, blurredBytesPerRow, blurredMemory);
    fastBlurMore((int)blurredContextSize.width, (int)blurredContextSize.height, blurredBytesPerRow, blurredMemory);
    fastBlurMore((int)blurredContextSize.width, (int)blurredContextSize.height, blurredBytesPerRow, blurredMemory);

    int32_t divisor = 256;
    vImage_Buffer dstBuffer1;
    dstBuffer1.width = (int)blurredContextSize.width;
    dstBuffer1.height = (int)blurredContextSize.height;
    dstBuffer1.rowBytes = blurredBytesPerRow;
    dstBuffer1.data = blurredMemory;
    vImageMatrixMultiply_ARGB8888(&dstBuffer1, &dstBuffer1, secretMatrix(NULL), divisor, NULL, NULL, kvImageDoNotTile);

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(blurredMemory, blurredContextSize.width, blurredContextSize.height, blurredBytesPerRow);

    vImage_Buffer srcBuffer;
    srcBuffer.width = blurredContextSize.width;
    srcBuffer.height = blurredContextSize.height;
    srcBuffer.rowBytes = blurredBytesPerRow;
    srcBuffer.data = blurredMemory;

    vImage_Buffer dstBuffer;
    dstBuffer.width = targetContextSize.width;
    dstBuffer.height = targetContextSize.height;
    dstBuffer.rowBytes = targetBytesPerRow;
    dstBuffer.data = targetMemory;

    vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageDoNotTile);

    CGContextRelease(blurredContext);
    free(blurredMemory);

    UIGraphicsPushContext(actionCircleContext);
    CGContextTranslateCTM(actionCircleContext, actionCircleContextSize.width / 2.0f, actionCircleContextSize.height / 2.0f);
    CGContextScaleCTM(actionCircleContext, 1.0f, -1.0f);
    CGContextTranslateCTM(actionCircleContext, -actionCircleContextSize.width / 2.0f, -actionCircleContextSize.height / 2.0f);

    CGContextSetInterpolationQuality(actionCircleContext, kCGInterpolationLow);
    CGContextSetBlendMode(actionCircleContext, kCGBlendModeCopy);

    [source drawInRect:CGRectMake((actionCircleContextSize.width - targetContextSize.width) / 2.0f, (actionCircleContextSize.height - targetContextSize.height) / 2.0f, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    brightenAndBlurImage(actionCircleMemory, actionCircleContextSize.width, actionCircleContextSize.height, actionCircleBytesPerRow, scale < 2);

    CGContextBeginPath(actionCircleContext);
    CGContextAddRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextAddEllipseInRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextClosePath(actionCircleContext);

    CGContextSetFillColorWithColor(actionCircleContext, [UIColor clearColor].CGColor);
    CGContextEOFillPath(actionCircleContext);

    CGImageRef actionCircleBitmapImage = CGBitmapContextCreateImage(actionCircleContext);
    UIImage *actionCircleImage = [[UIImage alloc] initWithCGImage:actionCircleBitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(actionCircleBitmapImage);

    CGContextRelease(actionCircleContext);
    free(actionCircleMemory);

    TGStaticBackdropAreaData *timestampBackdropArea = createTimestampBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));
    TGStaticBackdropAreaData *additionalDataBackdropArea = createAdditionalDataBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    TGStaticBackdropImageData *backdropData = [[TGStaticBackdropImageData alloc] init];
    [backdropData setBackdropArea:[[TGStaticBackdropAreaData alloc] initWithBackground:actionCircleImage] forKey:TGStaticBackdropMessageActionCircle];

    [backdropData setBackdropArea:timestampBackdropArea forKey:TGStaticBackdropMessageTimestamp];
    [backdropData setBackdropArea:additionalDataBackdropArea forKey:TGStaticBackdropMessageAdditionalData];

    [image setStaticBackdropImageData:backdropData];

    return image;
}

UIImage *TGBlurredFileImage(UIImage *source, CGSize size, uint32_t *averageColor)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize fittedSize = fitSize(size, CGSizeMake(90, 90));

    CGFloat actionCircleDiameter = 50.0f;

    const struct { int width, height; } blurredContextSize = { (int)fittedSize.width, (int)fittedSize.height };
    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale)};
    const struct { int width, height; } actionCircleContextSize = { (int)(actionCircleDiameter * scale), (int)(actionCircleDiameter * scale) };

    size_t blurredBytesPerRow = ((4 * (int)blurredContextSize.width) + 15) & (~15);
    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);
    size_t actionCircleBytesPerRow = ((4 * (int)actionCircleContextSize.width) + 15) & (~15);

    void *blurredMemory = malloc((int)(blurredBytesPerRow * blurredContextSize.height));
    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    void *actionCircleMemory = malloc(((int)(actionCircleBytesPerRow * actionCircleContextSize.height)));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef blurredContext = CGBitmapContextCreate(blurredMemory, (int)blurredContextSize.width, (int)blurredContextSize.height, 8, blurredBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef actionCircleContext = CGBitmapContextCreate(actionCircleMemory, (int)actionCircleContextSize.width, (int)actionCircleContextSize.height, 8, actionCircleBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(blurredContext);
    CGContextTranslateCTM(blurredContext, blurredContextSize.width / 2.0f, blurredContextSize.height / 2.0f);
    CGContextScaleCTM(blurredContext, 1.0f, -1.0f);
    CGContextTranslateCTM(blurredContext, -blurredContextSize.width / 2.0f, -blurredContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(blurredContext, kCGInterpolationLow);
    [source drawInRect:CGRectMake(0, 0, blurredContextSize.width, blurredContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    fastBlur((int)blurredContextSize.width, (int)blurredContextSize.height, blurredBytesPerRow, blurredMemory);

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(blurredMemory, blurredContextSize.width, blurredContextSize.height, blurredBytesPerRow);

    vImage_Buffer srcBuffer;
    srcBuffer.width = blurredContextSize.width;
    srcBuffer.height = blurredContextSize.height;
    srcBuffer.rowBytes = blurredBytesPerRow;
    srcBuffer.data = blurredMemory;

    vImage_Buffer dstBuffer;
    dstBuffer.width = targetContextSize.width;
    dstBuffer.height = targetContextSize.height;
    dstBuffer.rowBytes = targetBytesPerRow;
    dstBuffer.data = targetMemory;

    vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageDoNotTile);

    CGContextRelease(blurredContext);
    free(blurredMemory);

    UIGraphicsPushContext(actionCircleContext);
    CGContextTranslateCTM(actionCircleContext, actionCircleContextSize.width / 2.0f, actionCircleContextSize.height / 2.0f);
    CGContextScaleCTM(actionCircleContext, 1.0f, -1.0f);
    CGContextTranslateCTM(actionCircleContext, -actionCircleContextSize.width / 2.0f, -actionCircleContextSize.height / 2.0f);

    CGContextSetInterpolationQuality(actionCircleContext, kCGInterpolationLow);
    CGContextSetBlendMode(actionCircleContext, kCGBlendModeCopy);

    [source drawInRect:CGRectMake((actionCircleContextSize.width - targetContextSize.width) / 2.0f, (actionCircleContextSize.height - targetContextSize.height) / 2.0f, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    brightenAndBlurImage(actionCircleMemory, actionCircleContextSize.width, actionCircleContextSize.height, actionCircleBytesPerRow, scale < 2);

    CGContextBeginPath(actionCircleContext);
    CGContextAddRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextAddEllipseInRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextClosePath(actionCircleContext);

    CGContextSetFillColorWithColor(actionCircleContext, [UIColor clearColor].CGColor);
    CGContextEOFillPath(actionCircleContext);

    CGImageRef actionCircleBitmapImage = CGBitmapContextCreateImage(actionCircleContext);
    UIImage *actionCircleImage = [[UIImage alloc] initWithCGImage:actionCircleBitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(actionCircleBitmapImage);

    CGContextRelease(actionCircleContext);
    free(actionCircleMemory);

    TGStaticBackdropAreaData *timestampBackdropArea = createTimestampBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));
    TGStaticBackdropAreaData *additionalDataBackdropArea = createAdditionalDataBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    TGStaticBackdropImageData *backdropData = [[TGStaticBackdropImageData alloc] init];
    [backdropData setBackdropArea:[[TGStaticBackdropAreaData alloc] initWithBackground:actionCircleImage] forKey:TGStaticBackdropMessageActionCircle];

    [backdropData setBackdropArea:timestampBackdropArea forKey:TGStaticBackdropMessageTimestamp];
    [backdropData setBackdropArea:additionalDataBackdropArea forKey:TGStaticBackdropMessageAdditionalData];

    [image setStaticBackdropImageData:backdropData];

    return image;
}

UIImage *TGBlurredAlphaImage(UIImage *source, CGSize size)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize fittedSize = fitSize(size, CGSizeMake(90, 90));

    const struct { int width, height; } blurredContextSize = { (int)fittedSize.width, (int)fittedSize.height };
    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale)};

    size_t blurredBytesPerRow = ((4 * (int)blurredContextSize.width) + 15) & (~15);
    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *blurredMemory = malloc((int)(blurredBytesPerRow * blurredContextSize.height));
    memset(blurredMemory, 0, blurredBytesPerRow * blurredContextSize.height);
    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef blurredContext = CGBitmapContextCreate(blurredMemory, (int)blurredContextSize.width, (int)blurredContextSize.height, 8, blurredBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(blurredContext);
    CGContextTranslateCTM(blurredContext, blurredContextSize.width / 2.0f, blurredContextSize.height / 2.0f);
    CGContextScaleCTM(blurredContext, 1.0f, -1.0f);
    CGContextTranslateCTM(blurredContext, -blurredContextSize.width / 2.0f, -blurredContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(blurredContext, kCGInterpolationLow);
    [source drawInRect:CGRectMake(6, 6, blurredContextSize.width - 12, blurredContextSize.height - 12) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    vImage_Buffer srcBuffer;
    srcBuffer.width = blurredContextSize.width;
    srcBuffer.height = blurredContextSize.height;
    srcBuffer.rowBytes = blurredBytesPerRow;
    srcBuffer.data = blurredMemory;

    {
        vImage_Buffer dstBuffer;
        dstBuffer.width = blurredContextSize.width;
        dstBuffer.height = blurredContextSize.height;
        dstBuffer.rowBytes = blurredBytesPerRow;
        dstBuffer.data = targetMemory;

        int boxSize = (int)(0.02f * 100);
        boxSize = boxSize - (boxSize % 2) + 1;

        vImageBoxConvolve_ARGB8888(&srcBuffer, &dstBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
        vImageBoxConvolve_ARGB8888(&dstBuffer, &srcBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    }

    vImage_Buffer dstBuffer;
    dstBuffer.width = targetContextSize.width;
    dstBuffer.height = targetContextSize.height;
    dstBuffer.rowBytes = targetBytesPerRow;
    dstBuffer.data = targetMemory;

    vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageDoNotTile);

    CGContextRelease(blurredContext);
    free(blurredMemory);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    [image setExtendedEdgeInsets:UIEdgeInsetsMake(12.0f, 12.0f, 12.0f, 12.0f)];

    return image;
}

UIImage *TGLoadedAttachmentImage(UIImage *source, CGSize size, uint32_t *averageColor)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGFloat actionCircleDiameter = 50.0f;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };
    const struct { int width, height; } actionCircleContextSize = { (int)(actionCircleDiameter * scale), (int)(actionCircleDiameter * scale) };

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);
    size_t actionCircleBytesPerRow = ((4 * (int)actionCircleContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    void *actionCircleMemory = malloc(((int)(actionCircleBytesPerRow * actionCircleContextSize.height)));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef actionCircleContext = CGBitmapContextCreate(actionCircleMemory, (int)actionCircleContextSize.width, (int)actionCircleContextSize.height, 8, actionCircleBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(actionCircleContext);
    CGContextTranslateCTM(actionCircleContext, actionCircleContextSize.width / 2.0f, actionCircleContextSize.height / 2.0f);
    CGContextScaleCTM(actionCircleContext, 1.0f, -1.0f);
    CGContextTranslateCTM(actionCircleContext, -actionCircleContextSize.width / 2.0f, -actionCircleContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(actionCircleContext, kCGInterpolationLow);
    CGContextSetBlendMode(actionCircleContext, kCGBlendModeCopy);

    [source drawInRect:CGRectMake((actionCircleContextSize.width - targetContextSize.width) / 2.0f, (actionCircleContextSize.height - targetContextSize.height) / 2.0f, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    brightenAndBlurImage(actionCircleMemory, actionCircleContextSize.width, actionCircleContextSize.height, actionCircleBytesPerRow, true);

    CGContextBeginPath(actionCircleContext);
    CGContextAddRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextAddEllipseInRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextClosePath(actionCircleContext);

    CGContextSetFillColorWithColor(actionCircleContext, [UIColor clearColor].CGColor);
    CGContextEOFillPath(actionCircleContext);

    UIGraphicsPopContext();

    CGImageRef actionCircleBitmapImage = CGBitmapContextCreateImage(actionCircleContext);
    UIImage *actionCircleImage = [[UIImage alloc] initWithCGImage:actionCircleBitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(actionCircleBitmapImage);

    CGContextRelease(actionCircleContext);
    free(actionCircleMemory);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);
    [source drawInRect:CGRectMake(0, 0, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    TGStaticBackdropAreaData *timestampBackdropArea = createTimestampBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));
    TGStaticBackdropAreaData *additionalDataBackdropArea = createAdditionalDataBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    TGStaticBackdropImageData *backdropData = [[TGStaticBackdropImageData alloc] init];
    [backdropData setBackdropArea:[[TGStaticBackdropAreaData alloc] initWithBackground:actionCircleImage] forKey:TGStaticBackdropMessageActionCircle];

    [backdropData setBackdropArea:timestampBackdropArea forKey:TGStaticBackdropMessageTimestamp];
    [backdropData setBackdropArea:additionalDataBackdropArea forKey:TGStaticBackdropMessageAdditionalData];

    [image setStaticBackdropImageData:backdropData];

    return image;
}

UIImage *TGAnimationFrameAttachmentImage(UIImage *source, CGSize size, CGSize renderSize)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    renderSize.width *= scale;
    renderSize.height *= scale;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);

    CGContextSetFillColorWithColor(targetContext, [UIColor blackColor].CGColor);
    CGContextFillRect(targetContext, CGRectMake(0, 0, targetContextSize.width, targetContextSize.height));
    CGRect imageRect = CGRectMake((targetContextSize.width - renderSize.width) / 2.0f, (targetContextSize.height - renderSize.height) / 2.0f, renderSize.width, renderSize.height);
    [source drawInRect:imageRect blendMode:kCGBlendModeNormal alpha:1.0f];
    UIGraphicsPopContext();

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    return image;
}

UIImage *TGLoadedFileImage(UIImage *source, CGSize size, uint32_t *averageColor)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGFloat actionCircleDiameter = 50.0f;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };
    const struct { int width, height; } actionCircleContextSize = { (int)(actionCircleDiameter * scale), (int)(actionCircleDiameter * scale) };

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);
    size_t actionCircleBytesPerRow = ((4 * (int)actionCircleContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    void *actionCircleMemory = malloc(((int)(actionCircleBytesPerRow * actionCircleContextSize.height)));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGContextRef actionCircleContext = CGBitmapContextCreate(actionCircleMemory, (int)actionCircleContextSize.width, (int)actionCircleContextSize.height, 8, actionCircleBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(actionCircleContext);
    CGContextTranslateCTM(actionCircleContext, actionCircleContextSize.width / 2.0f, actionCircleContextSize.height / 2.0f);
    CGContextScaleCTM(actionCircleContext, 1.0f, -1.0f);
    CGContextTranslateCTM(actionCircleContext, -actionCircleContextSize.width / 2.0f, -actionCircleContextSize.height / 2.0f);
    CGContextSetInterpolationQuality(actionCircleContext, kCGInterpolationLow);
    CGContextSetBlendMode(actionCircleContext, kCGBlendModeCopy);

    [source drawInRect:CGRectMake((actionCircleContextSize.width - targetContextSize.width) / 2.0f, (actionCircleContextSize.height - targetContextSize.height) / 2.0f, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    brightenAndBlurImage(actionCircleMemory, actionCircleContextSize.width, actionCircleContextSize.height, actionCircleBytesPerRow, true);

    CGContextBeginPath(actionCircleContext);
    CGContextAddRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextAddEllipseInRect(actionCircleContext, CGRectMake(0.0f, 0.0f, actionCircleContextSize.width, actionCircleContextSize.height));
    CGContextClosePath(actionCircleContext);

    CGContextSetFillColorWithColor(actionCircleContext, [UIColor clearColor].CGColor);
    CGContextEOFillPath(actionCircleContext);

    UIGraphicsPopContext();

    CGImageRef actionCircleBitmapImage = CGBitmapContextCreateImage(actionCircleContext);
    UIImage *actionCircleImage = [[UIImage alloc] initWithCGImage:actionCircleBitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(actionCircleBitmapImage);

    CGContextRelease(actionCircleContext);
    free(actionCircleMemory);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);
    [source drawInRect:CGRectMake(0, 0, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    TGStaticBackdropAreaData *timestampBackdropArea = createTimestampBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));
    TGStaticBackdropAreaData *additionalDataBackdropArea = createAdditionalDataBackdropArea(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, CGSizeMake(size.width, size.height));

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    TGStaticBackdropImageData *backdropData = [[TGStaticBackdropImageData alloc] init];
    [backdropData setBackdropArea:[[TGStaticBackdropAreaData alloc] initWithBackground:actionCircleImage] forKey:TGStaticBackdropMessageActionCircle];

    [backdropData setBackdropArea:timestampBackdropArea forKey:TGStaticBackdropMessageTimestamp];
    [backdropData setBackdropArea:additionalDataBackdropArea forKey:TGStaticBackdropMessageAdditionalData];

    [image setStaticBackdropImageData:backdropData];

    return image;
}

UIImage *TGReducedAttachmentImage(UIImage *source, CGSize originalSize)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    CGSize size = CGSizeMake(CGFloor(originalSize.width * 0.4f), CGFloor(originalSize.height * 0.4f));

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };
    const struct { int width, height; } targetContextOriginalSize = { (int)(originalSize.width * scale), (int)(originalSize.height * scale) };

    CGFloat padding = 32.0f;
    CGFloat scaledWidth = targetContextOriginalSize.width / ((targetContextOriginalSize.width - padding * 2.0f) / (targetContextSize.width - padding * 2.0f));
    CGFloat scaledHeight = targetContextOriginalSize.height / ((targetContextOriginalSize.height - padding * 2.0f) / (targetContextSize.height - padding * 2.0f));

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);
    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(targetContext);

    CGContextSetInterpolationQuality(targetContext, kCGInterpolationMedium);

    CGContextSetFillColorWithColor(targetContext, [UIColor grayColor].CGColor);
    CGContextFillRect(targetContext, CGRectMake(0.0f, 0.0, targetContextSize.width, targetContextSize.height));

    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(0.0f, 0.0f, padding, padding));
    [source drawInRect:CGRectMake(0, 0, targetContextOriginalSize.width, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(targetContextSize.width - padding, 0.0f, padding, padding));
    [source drawInRect:CGRectMake(targetContextSize.width - targetContextOriginalSize.width, 0, targetContextOriginalSize.width, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(0.0f, targetContextSize.height - padding, padding, padding));
    [source drawInRect:CGRectMake(0, targetContextSize.height - targetContextOriginalSize.height, targetContextOriginalSize.width, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(targetContextSize.width - padding, targetContextSize.height - padding, padding, padding));
    [source drawInRect:CGRectMake(targetContextSize.width - targetContextOriginalSize.width, targetContextSize.height - targetContextOriginalSize.height, targetContextOriginalSize.width, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(padding, 0.0f, targetContextSize.width - padding * 2, padding));
    [source drawInRect:CGRectMake((targetContextSize.width - scaledWidth) / 2.0f, 0, scaledWidth, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(padding, targetContextSize.height - padding, targetContextSize.width - padding * 2, padding));
    [source drawInRect:CGRectMake((targetContextSize.width - scaledWidth) / 2.0f, targetContextSize.height - targetContextOriginalSize.height, scaledWidth, targetContextOriginalSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(0.0f, padding, padding, targetContextSize.height - padding * 2));
    [source drawInRect:CGRectMake(0, (targetContextSize.height - scaledHeight) / 2.0f, targetContextOriginalSize.width, scaledHeight) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextSaveGState(targetContext);
    CGContextClipToRect(targetContext, CGRectMake(targetContextSize.width - padding, padding, padding, targetContextSize.height - padding * 2));
    [source drawInRect:CGRectMake(targetContextSize.width - targetContextOriginalSize.width, (targetContextSize.height - scaledHeight) / 2.0f, targetContextOriginalSize.width, scaledHeight) blendMode:kCGBlendModeCopy alpha:1.0f];
    CGContextRestoreGState(targetContext);

    CGContextClipToRect(targetContext, CGRectMake(padding, padding, targetContextSize.width - padding * 2, targetContextSize.height - padding * 2));
    [source drawInRect:CGRectMake((targetContextSize.width - scaledWidth) / 2.0f, (targetContextSize.height - scaledHeight) / 2.0f, scaledWidth, scaledHeight) blendMode:kCGBlendModeCopy alpha:1.0f];

    UIGraphicsPopContext();

    addAttachmentImageCorners(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    //return image;
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(padding / scale, padding / scale, padding / scale, padding / scale) resizingMode:UIImageResizingModeStretch];
}

UIImage *TGBlurredBackgroundImage(UIImage *source, CGSize size)
{
    CGFloat scale = source.scale;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);
    [source drawInRect:CGRectMake(0, 0, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    brightenAndBlurImage(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow, false);

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    return image;
}

UIImage *TGRoundImage(UIImage *source, CGSize size)
{
    CGFloat scale = TGIsRetina() ? 2.0f : 1.0f;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };
    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));
    memset(targetMemory, 0, (int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);

    CGContextBeginPath(targetContext);
    CGContextAddEllipseInRect(targetContext, CGRectMake(0.0f, 0.0f, targetContextSize.width, targetContextSize.height));
    CGContextClip(targetContext);

    [source drawInRect:CGRectMake(0, 0, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    CGImageRef bitmapImage = CGBitmapContextCreateImage(targetContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:bitmapImage scale:scale orientation:UIImageOrientationUp];
    CGImageRelease(bitmapImage);

    CGContextRelease(targetContext);
    free(targetMemory);

    return image;
}

void TGPlainImageAverageColor(UIImage *source, uint32_t *averageColor)
{
    CGFloat scale = source.scale;
    CGSize size = source.size;

    const struct { int width, height; } targetContextSize = { (int)(size.width * scale), (int)(size.height * scale) };

    size_t targetBytesPerRow = ((4 * (int)targetContextSize.width) + 15) & (~15);

    void *targetMemory = malloc((int)(targetBytesPerRow * targetContextSize.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

    CGContextRef targetContext = CGBitmapContextCreate(targetMemory, (int)targetContextSize.width, (int)targetContextSize.height, 8, targetBytesPerRow, colorSpace, bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    UIGraphicsPushContext(targetContext);
    CGContextTranslateCTM(targetContext, targetContextSize.width / 2.0f, targetContextSize.height / 2.0f);
    CGContextScaleCTM(targetContext, 1.0f, -1.0f);
    CGContextTranslateCTM(targetContext, -targetContextSize.width / 2.0f, -targetContextSize.height / 2.0f);
    [source drawInRect:CGRectMake(0, 0, targetContextSize.width, targetContextSize.height) blendMode:kCGBlendModeCopy alpha:1.0f];
    UIGraphicsPopContext();

    if (averageColor != NULL)
        *averageColor = TGImageAverageColor(targetMemory, targetContextSize.width, targetContextSize.height, targetBytesPerRow);

    CGContextRelease(targetContext);
    free(targetMemory);
}


text:
package it.g2.Labs.l20100111.exceptions;

/**
 * Created by gigitsu on 10/02/15.
 */
public abstract class IllegalMonomero extends IllegalArgumentException {
    public IllegalMonomero(Character symbol, String name, String monomero) {
        super("La combinazione " + symbol + " + " + name +
                " non corrisponde a nessun " + monomero + " noto");
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<PRONOM-Report xmlns="http://pronom.nationalarchives.gov.uk">
  <report_format_detail>
    <FileFormat>
      <FormatID>405</FormatID>
      <FormatName>Microsoft Word for MS-DOS Document</FormatName>
      <FormatVersion>3.0</FormatVersion>
      <FormatAliases>
      </FormatAliases>
      <FormatFamilies>
      </FormatFamilies>
      <FormatTypes>
      </FormatTypes>
      <FormatDisclosure>
      </FormatDisclosure>
      <FormatDescription>This format has been deprecated in favour of another entry. Please see x-fmt/274, for versions 1-4 of Microsoft Word for MS-DOS Document.</FormatDescription>
      <BinaryFileFormat>Text</BinaryFileFormat>
      <ByteOrders>
      </ByteOrders>
      <ReleaseDate>
      </ReleaseDate>
      <WithdrawnDate>
      </WithdrawnDate>
      <ProvenanceSourceID>1</ProvenanceSourceID>
      <ProvenanceName>Digital Preservation Department / The National Archives</ProvenanceName>
      <ProvenanceSourceDate>02 Aug 2005</ProvenanceSourceDate>
      <ProvenanceDescription>
      </ProvenanceDescription>
      <LastUpdatedDate>19 May 2022</LastUpdatedDate>
      <FormatNote>
      </FormatNote>
      <FormatRisk>
      </FormatRisk>
      <TechnicalEnvironment>
      </TechnicalEnvironment>
      <FileFormatIdentifier>
        <Identifier>x-fmt/273</Identifier>
        <IdentifierType>PUID</IdentifierType>
      </FileFormatIdentifier>
    </FileFormat>
    <SearchCriteria>Criteria</SearchCriteria>
  </report_format_detail>
</PRONOM-Report>


text:
perfmon_collection_repo_t::collections_t::collections_t(perfmon_collection_t *_parent, namespace_id_t id)
    : namespace_collection(), serializers_collection(),
      parent_membership(_parent, &namespace_collection, uuid_to_str(id)),
      namespace_membership(&namespace_collection, &serializers_collection, "serializers")
{
}

perfmon_collection_repo_t::perfmon_collection_repo_t(perfmon_collection_t *_parent)
    : parent(_parent)
{ }

perfmon_collection_repo_t::collections_t *perfmon_collection_repo_t::get_perfmon_collections_for_namespace(namespace_id_t n_id) {
    if (!std_contains(perfmon_collections, n_id)) {
        perfmon_collections.insert(
                std::make_pair(n_id, make_scoped<collections_t>(parent, n_id)));
    }
    return perfmon_collections.find(n_id)->second.get();
}


text:
const chalk = require('chalk');
const semver = require('semver');
const packageConfig = require('../package.json');
const shell = require('shelljs');
const childProcess = require('child_process');

function exec(cmd) {
  return childProcess.execSync(cmd).toString().trim();
}

const versionRequirements = [
  {
    name: 'node',
    currentVersion: semver.clean(process.version),
    versionRequirement: packageConfig.engines.node,
  },
];

if (shell.which('npm')) {
  versionRequirements.push({
    name: 'npm',
    currentVersion: exec('npm --version'),
    versionRequirement: packageConfig.engines.npm,
  });
}

module.exports = () => {
  const warnings = [];
  for (let i = 0; i < versionRequirements.length; i += 1) {
    const mod = versionRequirements[i];
    if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
      warnings.push(`${mod.name}: ${chalk.red(mod.currentVersion)} should be ${chalk.green(mod.versionRequirement)}`);
    }
  }

  if (warnings.length) {
    console.log('');
    console.log(chalk.yellow('To use this template, you must update following to modules:'));
    console.log();
    for (let i = 0; i < warnings.length; i += 1) {
      const warning = warnings[i];
      console.log(`  ${warning}`);
    }
    console.log();
    process.exit(1);
  }
};


text:
FROM gi4nks/debian
MAINTAINER gi4nks "gnlc.lupo@gmail.com"

RUN mkdir /tmp/dir1
RUN mkdir /tmp/dir2
RUN mkdir /tmp/dir3
RUN mkdir /tmp/dir4
RUN mkdir /tmp/dir5
RUN mkdir /tmp/dir6
RUN mkdir /tmp/dir7
RUN mkdir /tmp/dir8
RUN mkdir /tmp/dir9
RUN mkdir /tmp/dir10
RUN mkdir /tmp/dir11
RUN mkdir /tmp/dir12
RUN mkdir /tmp/dir13
RUN mkdir /tmp/dir14
RUN mkdir /tmp/dir15
RUN mkdir /tmp/dir16
RUN mkdir /tmp/dir17
RUN mkdir /tmp/dir18
RUN mkdir /tmp/dir19
RUN mkdir /tmp/dir20
RUN mkdir /tmp/dir21
RUN mkdir /tmp/dir22
RUN mkdir /tmp/dir23
RUN mkdir /tmp/dir24
RUN mkdir /tmp/dir25
RUN mkdir /tmp/dir26
RUN mkdir /tmp/dir27
RUN mkdir /tmp/dir28
RUN mkdir /tmp/dir29
RUN mkdir /tmp/dir30
RUN mkdir /tmp/dir31
RUN mkdir /tmp/dir32
RUN mkdir /tmp/dir33
RUN mkdir /tmp/dir34
RUN mkdir /tmp/dir35
RUN mkdir /tmp/dir36
RUN mkdir /tmp/dir37
RUN mkdir /tmp/dir38
RUN mkdir /tmp/dir39
RUN mkdir /tmp/dir40
RUN mkdir /tmp/dir41
RUN mkdir /tmp/dir42
RUN mkdir /tmp/dir43
RUN mkdir /tmp/dir44
RUN mkdir /tmp/dir45
RUN mkdir /tmp/dir46
RUN mkdir /tmp/dir47
RUN mkdir /tmp/dir48
RUN mkdir /tmp/dir49
RUN mkdir /tmp/dir50
RUN mkdir /tmp/dir51
RUN mkdir /tmp/dir52
RUN mkdir /tmp/dir53
RUN mkdir /tmp/dir54
RUN mkdir /tmp/dir55
RUN mkdir /tmp/dir56
RUN mkdir /tmp/dir57
RUN mkdir /tmp/dir58
RUN mkdir /tmp/dir59
RUN mkdir /tmp/dir60
RUN mkdir /tmp/dir61
RUN mkdir /tmp/dir62
RUN mkdir /tmp/dir63
RUN mkdir /tmp/dir64
RUN mkdir /tmp/dir65
RUN mkdir /tmp/dir66
RUN mkdir /tmp/dir67
RUN mkdir /tmp/dir68
RUN mkdir /tmp/dir69
RUN mkdir /tmp/dir70
RUN mkdir /tmp/dir71
RUN mkdir /tmp/dir72
RUN mkdir /tmp/dir73
RUN mkdir /tmp/dir74
RUN mkdir /tmp/dir75
RUN mkdir /tmp/dir76
RUN mkdir /tmp/dir77
RUN mkdir /tmp/dir78
RUN mkdir /tmp/dir79
RUN mkdir /tmp/dir80
RUN mkdir /tmp/dir81
RUN mkdir /tmp/dir82
RUN mkdir /tmp/dir83
RUN mkdir /tmp/dir84
RUN mkdir /tmp/dir85
RUN mkdir /tmp/dir86
RUN mkdir /tmp/dir87
RUN mkdir /tmp/dir88
RUN mkdir /tmp/dir89
RUN mkdir /tmp/dir90
RUN mkdir /tmp/dir91
RUN mkdir /tmp/dir92
RUN mkdir /tmp/dir93
RUN mkdir /tmp/dir94
RUN mkdir /tmp/dir95
RUN mkdir /tmp/dir96
RUN mkdir /tmp/dir97
RUN mkdir /tmp/dir98
RUN mkdir /tmp/dir99


text:
package es.caib.zkib.jxpath.xml;

import java.io.InputStream;

/**
 * Common interface for XML parsers.  We are not using JAXP because
 * some of the parsers we want to support may not be JAXP compliant.
 *
 * @author Dmitri Plotnikov
 * @version $Revision: 1.1 $ $Date: 2009-04-03 08:13:14 $
 */
public interface XMLParser {

    /**
     * Reads the supplied XML file and returns the resulting model,
     * which is not necessarily DOM.
     * @param stream InputStream
     * @return Object
     */
    Object parseXML(InputStream stream);
}